Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow verifying access tokens #5

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
src/
test/
demo/
build/test/
build/test/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"test": "node build/test/test-agent.js",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p .",
"compile": "npx -p typescript@^2.6.1 tsc -p .",
"fix": "gts fix",
"prepare": "npm run compile",
"pretest": "npm run compile",
"posttest": "npm run check",
"prepublish": "npm run prepare"
"prepublish": "npm run prepare",
"prepack": "npm run prepare"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Validator {
*
*/
constructor(
private iss: string, private aud: string, private fakeAuth = false) {}
private iss: string, private aud: string, private token_use: string = 'id', private fakeAuth = false) {}

/**
*
Expand All @@ -41,7 +41,7 @@ export class Validator {
debug('PEMs generated from JWKs.');
}
tokenPayload =
await validateIdToken(token, this.pems, this.iss, this.aud);
await validateIdToken(token, this.pems, this.iss, this.aud, this.token_use);
}

debug('JWT token validated.');
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {JWK, JWT, PemDictionary, PolicyDocument, PolicyStatement} from './models


export const validateIdToken =
async (jwtToken: string, pems: PemDictionary, iss: string, aud: string) => {
async (jwtToken: string, pems: PemDictionary, iss: string, aud: string, token_use: string) => {
const decodedJwt = jwt.decode(jwtToken, {complete: true}) as JWT;
// Fail if the token is not jwt
if (!decodedJwt) {
Expand All @@ -19,12 +19,12 @@ export const validateIdToken =
}

// Reject the jwt if it's not an id token
if (!(decodedJwt.payload.token_use === 'id')) {
if (!(decodedJwt.payload.token_use === token_use)) {
throw new Error('Invalid token_use: ' + decodedJwt.payload.token_use);
}

// Fail if token audience is invalid
if (decodedJwt.payload.aud !== aud) {
// Fail if token audience is invalid and using id token
if (decodedJwt.payload.aud !== aud && token_use === 'id') {
throw new Error('Invalid aud: ' + decodedJwt.payload.aud);
}

Expand Down
2 changes: 1 addition & 1 deletion test/test-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function testToken(iss: string, aud: string, rl: readline.ReadLine) {
});
})
.then((token) => {
const validator = new Validator(iss, aud);
const validator = new Validator(iss, aud, 'id');
return validator.validate(token);
});
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"allowUnusedLabels": false,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es2015"],
"lib": ["es2015","esnext"],
"noFallthroughCasesInSwitch": true,
"noEmitOnError": true,
"noImplicitReturns": true,
Expand Down