Gathering detailed insights and metrics for middy-middleware-jwt-auth
Gathering detailed insights and metrics for middy-middleware-jwt-auth
Gathering detailed insights and metrics for middy-middleware-jwt-auth
Gathering detailed insights and metrics for middy-middleware-jwt-auth
@middy/util
🛵 The stylish Node.js middleware engine for AWS Lambda (util package)
@middy/http-json-body-parser
Http JSON body parser middleware for the middy framework
@middy/http-error-handler
Http error handler middleware for the middy framework
@middy/http-cors
CORS (Cross-Origin Resource Sharing) middleware for the middy framework
npm install middy-middleware-jwt-auth
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
25 Stars
685 Commits
8 Forks
3 Watching
4 Branches
6 Contributors
Updated on 18 Oct 2024
Minified
Minified + Gzipped
TypeScript (94.88%)
JavaScript (5.12%)
Cumulative downloads
Total Downloads
Last day
88.2%
352
Compared to previous day
Last week
42.1%
1,242
Compared to previous week
Last month
-5.3%
3,668
Compared to previous month
Last year
38.4%
70,983
Compared to previous year
4
1
40
A middy JSON web token authorization middleware inspired by express-jwt.
Download node at nodejs.org and install it, if you haven't already.
1npm install middy-middleware-jwt-auth --save
There is additional documentation.
1import createHttpError from "http-errors"; 2import middy from "@middy/core"; 3import httpErrorHandler from "@middy/http-error-handler"; 4import httpHeaderNormalizer from "@middy/http-header-normalizer"; 5import JWTAuthMiddleware, { 6 EncryptionAlgorithms, 7 IAuthorizedEvent, 8} from "middy-middleware-jwt-auth"; 9 10// Optionally define the token payload you expect to receive 11interface ITokenPayload { 12 permissions: string[]; 13} 14 15// Optionally define a type guard for the token payload 16function isTokenPayload(token: any): token is ITokenPayload { 17 return ( 18 token != null && 19 Array.isArray(token.permissions) && 20 token.permissions.every((permission: any) => typeof permission === "string") 21 ); 22} 23 24// This is your AWS handler 25const helloWorld = async (event: IAuthorizedEvent<ITokenPayload>) => { 26 // The middleware adds auth information if a valid token was added 27 // If no auth was found and credentialsRequired is set to true, a 401 will be thrown. If auth exists you 28 // have to check that it has the expected form. 29 if (event.auth!.payload.permissions.indexOf("helloWorld") === -1) { 30 throw createHttpError( 31 403, 32 `User not authorized for helloWorld, only found permissions [${event.auth!.permissions.join(", ")}]`, 33 { 34 type: "NotAuthorized", 35 }, 36 ); 37 } 38 39 return { 40 body: JSON.stringify({ 41 data: `Hello world! Here's your token: ${event.auth!.token}`, 42 }), 43 statusCode: 200, 44 }; 45}; 46 47// Let's "middyfy" our handler, then we will be able to attach middlewares to it 48export const handler = middy(helloWorld) 49 .use(httpHeaderNormalizer()) // Make sure authorization header is saved in lower case 50 .use(httpErrorHandler()) // This middleware is needed do handle the errors thrown by the JWTAuthMiddleware 51 .use( 52 JWTAuthMiddleware({ 53 /** Algorithm to verify JSON web token signature */ 54 algorithm: EncryptionAlgorithms.HS256, 55 /** An optional boolean that enables making authorization mandatory */ 56 credentialsRequired: true, 57 /** An optional function that checks whether the token payload is formatted correctly */ 58 isPayload: isTokenPayload, 59 /** A string or buffer containing either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA */ 60 secretOrPublicKey: "secret", 61 /** 62 * An optional function used to search for a token e. g. in a query string. By default, and as a fall back, 63 * event.headers.authorization and event.headers.Authorization are used. 64 */ 65 tokenSource: (event: any) => event.queryStringParameters.token, 66 }), 67 );
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
9 existing vulnerabilities detected
Details
Reason
Found 0/6 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More