Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change the value errors in FirebaseTokenVerifier constructor to Error…
… types.
  • Loading branch information
lahirumaramba committed Mar 9, 2021
commit 913ce37bb768c1d1ea52c85c49f8161495bf44c3
49 changes: 11 additions & 38 deletions src/utils/token-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,24 @@ export class FirebaseTokenVerifier {
private readonly app: FirebaseApp) {

if (!validator.isURL(clientCertUrl)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The provided public client certificate URL is an invalid URL.',
);

throw new Error('The provided public client certificate URL is an invalid URL.');
} else if (!validator.isNonEmptyString(algorithm)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The provided JWT algorithm is an empty string.',
);
throw new Error('The provided JWT algorithm is an empty string.');
} else if (!validator.isURL(issuer)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The provided JWT issuer is an invalid URL.',
);
throw new Error('The provided JWT issuer is an invalid URL.');
} else if (!validator.isNonNullObject(tokenInfo)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The provided JWT information is not an object or null.',
);
throw new Error('The provided JWT information is not an object or null.');
} else if (!validator.isURL(tokenInfo.url)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The provided JWT verification documentation URL is invalid.',
);
throw new Error('The provided JWT verification documentation URL is invalid.');
} else if (!validator.isNonEmptyString(tokenInfo.verifyApiName)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The JWT verify API name must be a non-empty string.',
);
throw new Error('The JWT verify API name must be a non-empty string.');
} else if (!validator.isNonEmptyString(tokenInfo.jwtName)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The JWT public full name must be a non-empty string.',
);
throw new Error('The JWT public full name must be a non-empty string.');
} else if (!validator.isNonEmptyString(tokenInfo.shortName)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The JWT public short name must be a non-empty string.',
);
} else if (!validator.isNonNullObject(tokenInfo.expiredErrorCode) || !('code' in tokenInfo.expiredErrorCode)) {
throw new this.tokenInfo.errorType(
this.tokenInfo.errorCodeConfig.invalidArg,
'The JWT expiration error code must be a non-null ErrorInfo object.',
);
throw new Error('The JWT public short name must be a non-empty string.');
} else if (!validator.isNonNullObject(tokenInfo.expiredErrorCode) ||
!('code' in tokenInfo.expiredErrorCode)) {
throw new Error('The JWT expiration error code must be a non-null ErrorInfo object.');
}
this.shortNameArticle = tokenInfo.shortName.charAt(0).match(/[aeiou]/i) ? 'an' : 'a';

Expand Down
37 changes: 20 additions & 17 deletions test/unit/auth/token-verifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,24 +316,27 @@ describe('FirebaseTokenVerifier', () => {
{ type: FirebaseAuthError, code: AUTH_ERROR_CODE_CONFIG },
];
errorTypes.forEach((errorType) => {
it('should throw with the correct error type set in token info', () => {
const tokenVerifier = new verifier.FirebaseTokenVerifier(
'https://www.example.com/publicKeys',
'RS256',
'https://www.example.com/issuer/',
{
url: 'https://docs.example.com/verify-tokens',
verifyApiName: 'verifyToken()',
jwtName: 'Important Token',
shortName: 'token',
expiredErrorCode: errorType.code.invalidArg,
errorCodeConfig: errorType.code,
errorType: errorType.type,
},
app,
);
it('should throw with the correct error type and code set in token info', () => {
expect(() => {
new verifier.FirebaseTokenVerifier(
'https://www.example.com/publicKeys',
'RS256',
'https://www.example.com/issuer/',
{
url: 'https://docs.example.com/verify-tokens',
verifyApiName: 'verifyToken()',
jwtName: 'Important Token',
shortName: '',
expiredErrorCode: errorType.code.invalidArg,
errorCodeConfig: errorType.code,
errorType: errorType.type,
},
app,
);
}).to.throw(errorType.type).with.property('code', 'auth/argument-error');
(tokenVerifier as any).verifyJWT();
}).to.throw(errorType.type).with.property('code').match(
new RegExp(`(.*)/${errorType.code.invalidArg.code}`)
);
});
});

Expand Down