Skip to content

Commit e1d551d

Browse files
authored
[Auth] Fix OAuth credential issue with nonce fields (#5574)
* Fix oauth credential issue with nonce fields * Add changeset
1 parent e2c4ab2 commit e1d551d

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

.changeset/itchy-snails-give.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/auth": patch
3+
---
4+
5+
Fix bug in the `OAuthProvider.prototype.credential` method that was preventing the `rawNonce` field from being populated in the returned `OAuthCredential`.

common/api-review/auth.api.md

-2
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,6 @@ export class OAuthCredential extends AuthCredential {
493493
idToken?: string;
494494
// @internal (undocumented)
495495
_linkToIdToken(auth: AuthInternal, idToken: string): Promise<IdTokenResponse>;
496-
// @internal (undocumented)
497-
nonce?: string;
498496
secret?: string;
499497
toJSON(): object;
500498
}

packages/auth/src/core/credentials/oauth.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('core/credentials/oauth', () => {
6969
nonce: 'nonce'
7070
});
7171

72-
expect(cred.nonce).to.eq('nonce');
72+
expect((cred.toJSON() as {nonce: string}).nonce).to.eq('nonce');
7373
});
7474

7575
it('ignores the nonce if pendingToken set', () => {
@@ -81,7 +81,7 @@ describe('core/credentials/oauth', () => {
8181
pendingToken: 'pending-token'
8282
});
8383

84-
expect(cred.nonce).to.be.undefined;
84+
expect((cred.toJSON() as {nonce?: string}).nonce).to.be.undefined;
8585
});
8686

8787
it('handles oauth1 and oauth with token secret', () => {

packages/auth/src/core/credentials/oauth.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ export class OAuthCredential extends AuthCredential {
7575
* @readonly
7676
*/
7777
secret?: string;
78-
/** @internal */
79-
nonce?: string;
78+
79+
private nonce?: string;
8080
private pendingToken: string | null = null;
8181

8282
/** @internal */
@@ -136,13 +136,17 @@ export class OAuthCredential extends AuthCredential {
136136
*/
137137
static fromJSON(json: string | object): OAuthCredential | null {
138138
const obj = typeof json === 'string' ? JSON.parse(json) : json;
139-
const { providerId, signInMethod, ...rest }: Partial<OAuthCredential> = obj;
139+
const { providerId, signInMethod, ...rest }: OAuthCredentialParams = obj;
140140
if (!providerId || !signInMethod) {
141141
return null;
142142
}
143143

144144
const cred = new OAuthCredential(providerId, signInMethod);
145-
Object.assign(cred, rest);
145+
cred.idToken = rest.idToken || undefined;
146+
cred.accessToken = rest.accessToken || undefined;
147+
cred.secret = rest.secret;
148+
cred.nonce = rest.nonce;
149+
cred.pendingToken = rest.pendingToken || null;
146150
return cred;
147151
}
148152

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

+12
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,16 @@ describe('core/providers/oauth', () => {
117117
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
118118
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
119119
});
120+
121+
it('credential generates the cred with the correct fields', () => {
122+
const provider = new OAuthProvider('foo.test');
123+
const cred = provider.credential({
124+
idToken: 'foo',
125+
rawNonce: 'i-am-a-nonce',
126+
});
127+
expect(cred.idToken).to.eq('foo');
128+
expect(cred.providerId).to.eq('foo.test');
129+
expect(cred.signInMethod).to.eq('foo.test');
130+
expect((cred.toJSON() as {nonce: string}).nonce).to.eq('i-am-a-nonce');
131+
});
120132
});

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ export class OAuthProvider extends BaseOAuthProvider {
164164
* or the ID token string.
165165
*/
166166
credential(params: OAuthCredentialOptions): OAuthCredential {
167-
return this._credential(params);
167+
return this._credential({...params, nonce: params.rawNonce});
168168
}
169169

170170
/** An internal credential method that accepts more permissive options */
171171
private _credential(
172-
params: OAuthCredentialOptions | OAuthCredentialParams
172+
params: Omit<OAuthCredentialParams, 'signInMethod' | 'providerId'>
173173
): OAuthCredential {
174174
_assert(params.idToken || params.accessToken, AuthErrorCode.ARGUMENT_ERROR);
175175
// For OAuthCredential, sign in method is same as providerId.
@@ -236,7 +236,7 @@ export class OAuthProvider extends BaseOAuthProvider {
236236
return new OAuthProvider(providerId)._credential({
237237
idToken: oauthIdToken,
238238
accessToken: oauthAccessToken,
239-
rawNonce: nonce,
239+
nonce,
240240
pendingToken
241241
});
242242
} catch (e) {

0 commit comments

Comments
 (0)