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

[#88] Validate Google ID token #106

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
9 changes: 9 additions & 0 deletions api/src/core/entities/auth.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ export interface AppIdToken {
email: string;
accessLevel: AccessLevel;
}

export interface GoogleIdToken {
iss: string;
aud: string;
sub: string;
idp: Idp;
email: string;
accessLevel: AccessLevel;
}
20 changes: 20 additions & 0 deletions api/src/core/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { GoogleIdToken } from '@src/core/entities/auth.entity';

export function validateGoogleIdToken(token: unknown): token is GoogleIdToken {
return (
typeof token === 'object' &&
token !== null &&
'iss' in token &&
typeof token.iss === 'string' &&
'aud' in token &&
typeof token.aud === 'string' &&
'sub' in token &&
typeof token.sub === 'string' &&
'email' in token &&
typeof token.email === 'string' &&
'iat' in token &&
typeof token.iat === 'number' &&
'exp' in token &&
typeof token.exp === 'number'
);
}
28 changes: 28 additions & 0 deletions api/test/core/validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { validateGoogleIdToken } from '@src/core/validator';

describe('Test validator', () => {
it('should return true when given ID token is valid', () => {
const givenIdToken = {
iss: 'https://accounts.google.com',
aud: '89988810438-h3st2janmqb7db8hhrem5hbj7pcptjot.apps.googleusercontent.com',
sub: '103395839580300821622',
email: '[email protected]',
iat: 1704250140,
exp: 1704253740
};

expect(validateGoogleIdToken(givenIdToken)).toEqual(true);
});

it('should return false when given ID token does not include an email', () => {
const givenIdToken = {
iss: 'https://accounts.google.com',
aud: '89988810438-h3st2janmqb7db8hhrem5hbj7pcptjot.apps.googleusercontent.com',
sub: '103395839580300821622',
iat: 1704250140,
exp: 1704253740
};

expect(validateGoogleIdToken(givenIdToken)).toEqual(false);
});
});