Skip to content

Commit 12a474b

Browse files
committed
improve validator logic
1 parent e4d34fa commit 12a474b

2 files changed

Lines changed: 36 additions & 44 deletions

File tree

src/auth/auth-config.ts

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -800,59 +800,52 @@ export class OIDCConfig implements OIDCAuthProviderConfig {
800800
);
801801
}
802802
if (validator.isNonNullObject(options.responseType) && typeof options.responseType !== 'undefined') {
803-
let idTokenType;
804-
let codeType;
805-
let setIdTokenType = false;
806-
let setCodeType = false;
807-
for (const responseTypeKey in options.responseType) {
808-
if (!(responseTypeKey in validResponseTypes)) {
803+
Object.keys(options.responseType).forEach((key) => {
804+
if (!(key in validResponseTypes)) {
809805
throw new FirebaseAuthError(
810806
AuthClientErrorCode.INVALID_CONFIG,
811-
`"${responseTypeKey}" is not a valid OAuthResponseType parameter.`,
807+
`"${key}" is not a valid OAuthResponseType parameter.`,
808+
);
809+
}
810+
});
811+
812+
const idToken = options.responseType.idToken;
813+
if (typeof idToken !== 'undefined') {
814+
if (!validator.isBoolean(idToken)) {
815+
throw new FirebaseAuthError(
816+
AuthClientErrorCode.INVALID_ARGUMENT,
817+
'"OIDCAuthProviderConfig.responseType.idToken" must be a boolean.',
812818
);
813-
} else {
814-
if (responseTypeKey === 'idToken') {
815-
if (!validator.isBoolean(options.responseType.idToken)) {
816-
throw new FirebaseAuthError(
817-
AuthClientErrorCode.INVALID_ARGUMENT,
818-
'"OIDCAuthProviderConfig.responseType.idToken" must be a boolean.',
819-
);
820-
}
821-
if (typeof options.responseType.idToken !== 'undefined') {
822-
idTokenType = options.responseType.idToken;
823-
setIdTokenType = true;
824-
}
825-
} else if (responseTypeKey === 'code') {
826-
if (!validator.isBoolean(options.responseType.code)) {
827-
throw new FirebaseAuthError(
828-
AuthClientErrorCode.INVALID_ARGUMENT,
829-
'"OIDCAuthProviderConfig.responseType.code" must be a boolean.',
830-
);
831-
}
832-
if (typeof options.responseType.code !== 'undefined') {
833-
codeType = options.responseType.code;
834-
setCodeType = true;
835-
}
836-
}
837819
}
838820
}
839-
821+
822+
const code = options.responseType.code;
823+
if (typeof code !== 'undefined') {
824+
if (!validator.isBoolean(code)) {
825+
throw new FirebaseAuthError(
826+
AuthClientErrorCode.INVALID_ARGUMENT,
827+
'"OIDCAuthProviderConfig.responseType.code" must be a boolean.',
828+
);
829+
}
830+
831+
// If code flow is enabled, client secret must be provided.
832+
if (typeof options.clientSecret === 'undefined') {
833+
throw new FirebaseAuthError(
834+
AuthClientErrorCode.MISSING_OAUTH_CLIENT_SECRET,
835+
'The OAuth configuration client secret is required to enable OIDC code flow.',
836+
);
837+
}
838+
}
839+
840+
const allKeys = Object.keys(options.responseType).length;
841+
const enabledCount = Object.values(options.responseType).filter(Boolean).length;
840842
// Only one of OAuth response types can be set to true.
841-
if ((setIdTokenType && setCodeType) &&
842-
((idTokenType && codeType) ||
843-
(!idTokenType && !codeType))) {
843+
if (allKeys > 1 && enabledCount != 1) {
844844
throw new FirebaseAuthError(
845845
AuthClientErrorCode.INVALID_OAUTH_RESPONSETYPE,
846846
'Only exactly one OAuth responseType should be set to true.',
847847
);
848848
}
849-
// If code flow is enabled, client secret must be provided.
850-
if (codeType && typeof options.clientSecret === 'undefined') {
851-
throw new FirebaseAuthError(
852-
AuthClientErrorCode.MISSING_OAUTH_CLIENT_SECRET,
853-
'The OAuth configuration client secret is required to enable OIDC code flow.',
854-
);
855-
}
856849
}
857850
}
858851

test/unit/auth/auth-config.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,7 @@ describe('OIDCConfig', () => {
958958

959959
it('should not throw when only idToken responseType is set to true', () => {
960960
const validRequest = deepCopy(clientRequest) as any;
961-
const validResponseType = { idToken: true };
962-
validRequest.responseType = validResponseType;
961+
validRequest.responseType = { idToken: true };
963962
expect(() => OIDCConfig.validate(validRequest, true)).not.to.throw();
964963
});
965964

0 commit comments

Comments
 (0)