Skip to content

Commit

Permalink
Merge pull request #32 from DanielRivers/main
Browse files Browse the repository at this point in the history
fix: don't store the decoded payload and decode when needed
  • Loading branch information
coel authored Nov 24, 2023
2 parents 699fe0b + d7186bd commit 154084b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 22 deletions.
4 changes: 0 additions & 4 deletions lib/__tests__/sdk/utilities/feature-flags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ describe('feature-flags', () => {

beforeEach(async () => {
mockAccessToken = mocks.getMockAccessToken();
await sessionManager.setSessionItem(
'access_token_payload',
mockAccessToken.payload
);
await sessionManager.setSessionItem('access_token', mockAccessToken.token);
});

Expand Down
10 changes: 1 addition & 9 deletions lib/__tests__/sdk/utilities/token-claims.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ describe('token-claims', () => {
beforeEach(async () => {
mockAccessToken = mocks.getMockAccessToken();
mockIdToken = mocks.getMockIdToken();
await sessionManager.setSessionItem(
'access_token_payload',
mockAccessToken.payload
);
await sessionManager.setSessionItem(
'id_token_payload',
mockIdToken.payload
);
await sessionManager.setSessionItem('access_token', mockAccessToken.token);
await sessionManager.setSessionItem('id_token', mockIdToken.token);
});
Expand All @@ -38,7 +30,7 @@ describe('token-claims', () => {
Object.keys(mockAccessToken.payload).forEach(async (name: string) => {
const claimValue = await getClaimValue(sessionManager, name);
const tokenPayload = mockAccessToken.payload as Record<string, unknown>;
expect(claimValue).toBe(tokenPayload[name]);
expect(claimValue).toStrictEqual(tokenPayload[name]);
});
});

Expand Down
8 changes: 5 additions & 3 deletions lib/sdk/utilities/token-claims.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type SessionManager } from '../session-managers/index.js';
import { isTokenExpired } from './token-utils.js';
import { type ClaimTokenType } from './types.js';
import { jwtDecode } from 'jwt-decode';

/**
* Method extracts the provided claim from the provided token type in the
Expand All @@ -15,9 +16,10 @@ export const getClaimValue = async (
claim: string,
type: ClaimTokenType = 'access_token'
): Promise<unknown | null> => {
const tokenPayload = (await sessionManager.getSessionItem(
`${type}_payload`
)) as Record<string, unknown>;
const token = (await sessionManager.getSessionItem(
`${type}`
)) as string;
const tokenPayload: Record<string, unknown> = jwtDecode(token);
return tokenPayload[claim] ?? null;
};

Expand Down
7 changes: 1 addition & 6 deletions lib/sdk/utilities/token-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ export const commitTokenToMemory = async (
type: TokenType
): Promise<void> => {
await sessionManager.setSessionItem(type, token);
if (type === 'access_token') {
const tokenPayload = jwtDecode(token);
await sessionManager.setSessionItem('access_token_payload', tokenPayload);
} else if (type === 'id_token') {
const tokenPayload = jwtDecode(token);
await sessionManager.setSessionItem('id_token_payload', tokenPayload);
if (type === 'id_token') {
await commitUserToMemoryFromToken(sessionManager, token);
}
};
Expand Down

0 comments on commit 154084b

Please sign in to comment.