Skip to content

Commit 02c1761

Browse files
authored
Fix the OAuth credential logic, and add support for OIDC (#4512)
* Fix oauth credential and test for OIDC * Formatting * Formatting
1 parent bc9f841 commit 02c1761

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

packages-exp/auth-exp/src/core/providers/oauth.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ describe('core/providers/oauth', () => {
7878
expect(OAuthProvider.credentialFromResult(userCred)).to.be.null;
7979
});
8080

81+
it('credentialFromResult works for oidc', async () => {
82+
const auth = await testAuth();
83+
const userCred = new UserCredentialImpl({
84+
user: testUser(auth, 'uid'),
85+
providerId: ProviderId.GOOGLE,
86+
_tokenResponse: {
87+
...TEST_ID_TOKEN_RESPONSE,
88+
pendingToken: 'pending-token',
89+
oauthIdToken: 'id-token',
90+
providerId: 'oidc.oidctest'
91+
},
92+
operationType: OperationType.SIGN_IN
93+
});
94+
const cred = OAuthProvider.credentialFromResult(userCred)!;
95+
expect(cred.idToken).to.eq('id-token');
96+
expect(cred.providerId).to.eq('oidc.oidctest');
97+
expect(cred.signInMethod).to.eq('oidc.oidctest');
98+
expect((cred.toJSON() as Record<string, string>).pendingToken).to.eq(
99+
'pending-token'
100+
);
101+
});
102+
81103
it('credentialFromError creates the cred from a tagged error', () => {
82104
const error = _createError(AuthErrorCode.NEED_CONFIRMATION, {
83105
appName: 'foo'

packages-exp/auth-exp/src/core/providers/oauth.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { AuthProvider, UserCredential } from '../../model/public_types';
2020
import { _assert } from '../util/assert';
2121
import { AuthErrorCode } from '../errors';
2222

23-
import { OAuthCredential } from '../credentials/oauth';
23+
import { OAuthCredential, OAuthCredentialParams } from '../credentials/oauth';
2424
import { UserCredentialInternal } from '../../model/user';
2525
import { FirebaseError } from '@firebase/util';
2626
import { TaggedWithTokenResponse } from '../../model/id_token';
@@ -146,12 +146,19 @@ export class OAuthProvider implements AuthProvider {
146146
* or the ID token string.
147147
*/
148148
credential(params: OAuthCredentialOptions): OAuthCredential {
149-
_assert(params.idToken && params.accessToken, AuthErrorCode.ARGUMENT_ERROR);
149+
return this._credential(params);
150+
}
151+
152+
/** An internal credential method that accepts more permissive options */
153+
private _credential(
154+
params: OAuthCredentialOptions | OAuthCredentialParams
155+
): OAuthCredential {
156+
_assert(params.idToken || params.accessToken, AuthErrorCode.ARGUMENT_ERROR);
150157
// For OAuthCredential, sign in method is same as providerId.
151158
return OAuthCredential._fromParams({
159+
...params,
152160
providerId: this.providerId,
153-
signInMethod: this.providerId,
154-
...params
161+
signInMethod: this.providerId
155162
});
156163
}
157164

@@ -261,10 +268,11 @@ export class OAuthProvider implements AuthProvider {
261268
}
262269

263270
try {
264-
return new OAuthProvider(providerId).credential({
271+
return new OAuthProvider(providerId)._credential({
265272
idToken: oauthIdToken,
266273
accessToken: oauthAccessToken,
267-
rawNonce: nonce
274+
rawNonce: nonce,
275+
pendingToken
268276
});
269277
} catch (e) {
270278
return null;

packages-exp/auth-exp/src/core/user/additional_user_info.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function _fromIdTokenResponse(
8282
class GenericAdditionalUserInfo implements AdditionalUserInfo {
8383
constructor(
8484
readonly isNewUser: boolean,
85-
readonly providerId: ProviderId | null,
85+
readonly providerId: ProviderId | string | null,
8686
readonly profile: Record<string, unknown> = {}
8787
) {}
8888
}

packages-exp/auth-exp/src/core/user/user_credential_impl.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ import { AuthInternal } from '../../model/auth';
2525

2626
interface UserCredentialParams {
2727
readonly user: UserInternal;
28-
readonly providerId: ProviderId | null;
28+
readonly providerId: ProviderId | string | null;
2929
readonly _tokenResponse?: PhoneOrOauthTokenResponse;
3030
readonly operationType: OperationType;
3131
}
3232

3333
export class UserCredentialImpl
3434
implements UserCredentialInternal, UserCredentialParams {
3535
readonly user: UserInternal;
36-
readonly providerId: ProviderId | null;
36+
readonly providerId: ProviderId | string | null;
3737
readonly _tokenResponse: PhoneOrOauthTokenResponse | undefined;
3838
readonly operationType: OperationType;
3939

@@ -81,7 +81,9 @@ export class UserCredentialImpl
8181
}
8282
}
8383

84-
function providerIdForResponse(response: IdTokenResponse): ProviderId | null {
84+
function providerIdForResponse(
85+
response: IdTokenResponse
86+
): ProviderId | string | null {
8587
if (response.providerId) {
8688
return response.providerId;
8789
}

packages-exp/auth-exp/src/model/id_token.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface IdTokenResponse {
5555
idToken?: IdToken;
5656
refreshToken?: string;
5757
expiresIn?: string;
58-
providerId?: ProviderId;
58+
providerId?: ProviderId | string;
5959

6060
// Used in AdditionalUserInfo
6161
displayName?: string | null;

0 commit comments

Comments
 (0)