This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 802
Create decode-verify-jwt.js #125
Open
naphtul
wants to merge
2
commits into
awslabs:master
Choose a base branch
from
naphtul:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const _ = require('lodash'); | ||
const Axios = require('axios'); | ||
const jose = require('jose'); | ||
|
||
// Input validations | ||
const input = ['COGNITO_POOL_ID', 'COGNITO_REGION', 'COGNITO_CLIENT_ID']; | ||
for (const variable of input) { | ||
if (!process.env.hasOwnProperty(variable) || process.env[variable].length === 0) { | ||
throw new Error(`Environment variable is required for ${variable}.`); | ||
} | ||
} | ||
|
||
// Global variables declaration | ||
const cognitoClientId = process.env.COGNITO_CLIENT_ID; | ||
const cognitoIssuer = `https://cognito-idp.${process.env.COGNITO_REGION}.amazonaws.com/${process.env.COGNITO_POOL_ID}`; | ||
let keyStore; | ||
|
||
const extractToken = async (event) => { | ||
const authorization = _.get(event, ['headers', 'Authorization'], ''); | ||
if (authorization === '') return authorization; | ||
return authorization.split(' ')[1]; // Cut 'Bearer' out | ||
}; | ||
|
||
const getPublicKeys = async () => { | ||
const url = `${cognitoIssuer}/.well-known/jwks.json`; | ||
const res = await Axios.default.get(url); | ||
return res.data; | ||
}; | ||
|
||
const getPublicKeysIfNotCached = async (keyStore) => { | ||
if (!keyStore) { | ||
const keys = await getPublicKeys(); | ||
keyStore = new jose.JWKS.asKeyStore(keys); | ||
} | ||
return keyStore; | ||
}; | ||
|
||
const verifyIdToken = async (token, keyStore, issuer, audience) => { | ||
return jose.JWT.IdToken.verify( | ||
token, | ||
keyStore, | ||
{ | ||
issuer, | ||
audience, | ||
algorithms: ['RS256'] | ||
} | ||
); | ||
}; | ||
|
||
const isAuthorized = async (decryptedToken) => { | ||
// Add authorization logic here | ||
return true; | ||
}; | ||
|
||
const redirectTo = async (decryptedToken) => { | ||
// Add redirection logic here | ||
return 'redirectPath'; | ||
}; | ||
|
||
module.exports.handler = async (event) => { | ||
console.log('event', JSON.stringify(event)); | ||
let decryptedToken; | ||
try { | ||
const token = await extractToken(event); | ||
keyStore = await getPublicKeysIfNotCached(keyStore); | ||
const decryptedToken = await verifyIdToken(token, keyStore, cognitoIssuer, cognitoClientId); | ||
console.log('decryptedToken', JSON.stringify(decryptedToken)); | ||
} catch (e) { | ||
console.error(e); | ||
return {statusCode: 401, body: 'Unauthorized'}; | ||
} | ||
try { | ||
const authorized = await isAuthorized(decryptedToken); | ||
if (authorized) { | ||
return {statusCode: 302, headers: {Location: '/' + await redirectTo(decryptedToken)}}; | ||
} | ||
return {statusCode: 403, body: 'Forbidden'}; | ||
} catch (e) { | ||
console.error(e); | ||
return {statusCode: 403, body: 'Forbidden'}; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello!
You have
decryptedToken
redefined here.const
should be removed, to unblock the initial definition on line 62.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Done.