@colearn/microservices-kit
Advanced tools
Comparing version 0.0.37 to 0.0.38
/// <reference types="node" /> | ||
import type { NextFunction, Response, Request } from 'express'; | ||
export declare function jwtMiddleware(secret: string | undefined, required?: boolean): (req: Request, res: Response, next: NextFunction) => Promise<void | NodeJS.Immediate>; | ||
import { Params } from 'express-jwt'; | ||
export declare function extractToken(req: Request): string | undefined; | ||
export declare function jwtMiddleware({ secret, algorithms, credentialsRequired, requestProperty, ...rest }: Params): (req: Request, res: Response, next: NextFunction) => Promise<void | NodeJS.Immediate>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.jwtMiddleware = void 0; | ||
exports.jwtMiddleware = exports.extractToken = void 0; | ||
const express_jwt_1 = require("express-jwt"); | ||
const errors_1 = require("./errors"); | ||
function jwtMiddleware(secret, required = false) { | ||
function extractBearerToken(req) { | ||
if (!req.headers.authorization) { | ||
return undefined; | ||
} | ||
const parts = req.headers.authorization.split(' '); | ||
if (parts.length === 2) { | ||
const [scheme, credentials] = parts; | ||
if (/^Bearer$/i.test(scheme)) { | ||
return credentials; | ||
} | ||
} | ||
throw new errors_1.UnauthorizedError('Format is "Authorization: Bearer [token]"'); | ||
} | ||
function extractTokenFromCookie(req, cookieName) { | ||
if (!cookieName) { | ||
return undefined; | ||
} | ||
return req.cookies[cookieName]; | ||
} | ||
function extractToken(req) { | ||
return extractBearerToken(req) || extractTokenFromCookie(req); | ||
} | ||
exports.extractToken = extractToken; | ||
function jwtMiddleware({ secret, algorithms = ['HS256'], credentialsRequired = false, requestProperty = 'user', ...rest }) { | ||
if (!secret) { | ||
@@ -12,5 +35,6 @@ throw new TypeError('jwt secret is not specified'); | ||
secret, | ||
algorithms: ['HS256'], | ||
credentialsRequired: required, | ||
requestProperty: 'user', | ||
algorithms, | ||
credentialsRequired, | ||
requestProperty, | ||
...rest, | ||
}); | ||
@@ -17,0 +41,0 @@ return (req, res, next) => middleware(req, res, (err) => { |
{ | ||
"name": "@colearn/microservices-kit", | ||
"version": "0.0.37", | ||
"version": "0.0.38", | ||
"description": "colearn microservice kit", | ||
@@ -5,0 +5,0 @@ "author": "Colearn", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55505
922