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

chore(js): add LogtoErrorCode.idToken.invalidToken and jest-matcher-specific-error #131

Merged
merged 5 commits into from
Jan 24, 2022
Merged
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
2 changes: 1 addition & 1 deletion packages/js/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
coveragePathIgnorePatterns: ['/node_modules/', '/lib/'],
coverageReporters: ['text-summary', 'lcov'],
moduleFileExtensions: ['ts', 'js'],
setupFilesAfterEnv: ['./jest.setup.js'],
setupFilesAfterEnv: ['./jest.setup.js', 'jest-matcher-specific-error'],
testPathIgnorePatterns: ['lib'],
testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|js)$',
transform: {
Expand Down
1 change: 1 addition & 0 deletions packages/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"codecov": "^3.8.3",
"eslint": "^8.1.0",
"jest": "^27.0.6",
"jest-matcher-specific-error": "^1.0.0",
"lint-staged": "^11.1.2",
"nock": "^13.1.3",
"node-fetch": "2.6.6",
Expand Down
5 changes: 2 additions & 3 deletions packages/js/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ type Normalize<T> = keyof T | Normalize2<T>;

const logtoErrorCodes = Object.freeze({
idToken: {
verification: {
invalidIat: 'Invalid issued at time',
},
invalidIat: 'Invalid issued at time',
invalidToken: 'Invalid token',
},
});

Expand Down
6 changes: 3 additions & 3 deletions packages/js/src/utils/id-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ describe('verifyIdToken', () => {

const jwks = createDefaultJwks();

await expect(verifyIdToken(idToken, 'qux', 'foo', jwks)).rejects.toThrowError(
new LogtoError('idToken.verification.invalidIat')
await expect(verifyIdToken(idToken, 'qux', 'foo', jwks)).rejects.toMatchError(
new LogtoError('idToken.invalidIat')
);
});
});
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('decodeIdToken', () => {
});

test('decoding invalid JWT string should throw Error', async () => {
expect(() => decodeIdToken('invalid-JWT')).toThrow('invalid token');
expect(() => decodeIdToken('invalid-JWT')).toMatchError(new LogtoError('idToken.invalidToken'));
});

test('decoding valid JWT without issuer should throw StructError', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/utils/id-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export const verifyIdToken = async (
) => {
const result = await jwtVerify(idToken, jwks, { audience: clientId, issuer });
if (Math.abs((result?.payload?.iat ?? 0) - Date.now() / 1000) > issuedAtTimeTolerance) {
throw new LogtoError('idToken.verification.invalidIat');
throw new LogtoError('idToken.invalidIat');
}
};

export const decodeIdToken = (token: string): IdTokenClaims => {
const { 1: encodedPayload } = token.split('.');
if (!encodedPayload) {
throw new Error('invalid token');
throw new LogtoError('idToken.invalidToken');
}

const json = UrlSafeBase64.decode(encodedPayload);
Expand Down
7 changes: 6 additions & 1 deletion packages/js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"extends": "@silverhand/ts-config/tsconfig.base",
"compilerOptions": {
"outDir": "lib",
"target": "es5"
"target": "es5",
"types": [
"node",
"jest",
"jest-matcher-specific-error"
]
},
"include": [
"src"
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.