Skip to content

Commit

Permalink
Expose raw id_token in the getIdTokenClaims method (#175)
Browse files Browse the repository at this point in the history
* Expose raw id_token in the getIdTokenClaims method

* add snippet
  • Loading branch information
luisrudge authored Aug 26, 2019
1 parent c190d11 commit 3c627c3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
16 changes: 12 additions & 4 deletions __tests__/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('cache', () => {
access_token: 'accesstoken',
expires_in: 1,
decodedToken: {
claims: { exp: 1, name: 'Test' },
claims: { __raw: 'idtoken', exp: 1, name: 'Test' },
user: { name: 'Test' }
}
});
Expand All @@ -32,7 +32,7 @@ describe('cache', () => {
access_token: 'accesstoken',
expires_in: 1,
decodedToken: {
claims: { name: 'Test' },
claims: { __raw: 'idtoken', name: 'Test' },
user: { name: 'Test' }
}
});
Expand All @@ -46,7 +46,11 @@ describe('cache', () => {
access_token: 'accesstoken',
expires_in: 1,
decodedToken: {
claims: { name: 'Test', exp: new Date().getTime() / 1000 + 2 },
claims: {
__raw: 'idtoken',
name: 'Test',
exp: new Date().getTime() / 1000 + 2
},
user: { name: 'Test' }
}
});
Expand All @@ -61,7 +65,11 @@ describe('cache', () => {
access_token: 'accesstoken',
expires_in: 2,
decodedToken: {
claims: { name: 'Test', exp: new Date().getTime() / 1000 + 1 },
claims: {
__raw: 'idtoken',
name: 'Test',
exp: new Date().getTime() / 1000 + 1
},
user: { name: 'Test' }
}
});
Expand Down
1 change: 1 addition & 0 deletions __tests__/jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('jwt', async () => {
payload: true
},
claims: {
__raw: id_token,
aud: 'k5u3o2fiAA8XweXEEX604KCwCjzjtMU6',
exp: 1551365990,
iat: 1551362390,
Expand Down
1 change: 1 addition & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ interface JWTVerifyOptions {
* @ignore
*/
interface IdToken {
__raw: string;
name?: string;
given_name?: string;
family_name?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const idTokendecoded = [
export const decode = (token: string) => {
const [header, payload, signature] = token.split('.');
const payloadJSON = JSON.parse(urlDecodeB64(payload));
const claims: IdToken = {};
const claims: IdToken = { __raw: token };
const user = {};
Object.keys(payloadJSON).forEach(k => {
claims[k] = payloadJSON[k];
Expand Down
2 changes: 2 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
$('#getIdTokenClaims').click(function() {
auth0.getIdTokenClaims().then(function(claims) {
console.log(claims);
//if you need the raw id_token, you can access it in the __raw property
const id_token = claims.__raw;
});
});
$('#getToken_audience').click(function() {
Expand Down

0 comments on commit 3c627c3

Please sign in to comment.