Skip to content

[Auth] Fix OAuth credential issue with nonce fields #5574

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

Merged
merged 2 commits into from
Oct 5, 2021
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
5 changes: 5 additions & 0 deletions .changeset/itchy-snails-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/auth": patch
---

Fix bug in the `OAuthProvider.prototype.credential` method that was preventing the `rawNonce` field from being populated in the returned `OAuthCredential`.
2 changes: 0 additions & 2 deletions common/api-review/auth.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,6 @@ export class OAuthCredential extends AuthCredential {
idToken?: string;
// @internal (undocumented)
_linkToIdToken(auth: AuthInternal, idToken: string): Promise<IdTokenResponse>;
// @internal (undocumented)
nonce?: string;
secret?: string;
toJSON(): object;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/core/credentials/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('core/credentials/oauth', () => {
nonce: 'nonce'
});

expect(cred.nonce).to.eq('nonce');
expect((cred.toJSON() as {nonce: string}).nonce).to.eq('nonce');
});

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

expect(cred.nonce).to.be.undefined;
expect((cred.toJSON() as {nonce?: string}).nonce).to.be.undefined;
});

it('handles oauth1 and oauth with token secret', () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/auth/src/core/credentials/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export class OAuthCredential extends AuthCredential {
* @readonly
*/
secret?: string;
/** @internal */
nonce?: string;

private nonce?: string;
private pendingToken: string | null = null;

/** @internal */
Expand Down Expand Up @@ -136,13 +136,17 @@ export class OAuthCredential extends AuthCredential {
*/
static fromJSON(json: string | object): OAuthCredential | null {
const obj = typeof json === 'string' ? JSON.parse(json) : json;
const { providerId, signInMethod, ...rest }: Partial<OAuthCredential> = obj;
const { providerId, signInMethod, ...rest }: OAuthCredentialParams = obj;
if (!providerId || !signInMethod) {
return null;
}

const cred = new OAuthCredential(providerId, signInMethod);
Object.assign(cred, rest);
cred.idToken = rest.idToken || undefined;
cred.accessToken = rest.accessToken || undefined;
cred.secret = rest.secret;
cred.nonce = rest.nonce;
cred.pendingToken = rest.pendingToken || null;
return cred;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/auth/src/core/providers/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,16 @@ describe('core/providers/oauth', () => {
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
});

it('credential generates the cred with the correct fields', () => {
const provider = new OAuthProvider('foo.test');
const cred = provider.credential({
idToken: 'foo',
rawNonce: 'i-am-a-nonce',
});
expect(cred.idToken).to.eq('foo');
expect(cred.providerId).to.eq('foo.test');
expect(cred.signInMethod).to.eq('foo.test');
expect((cred.toJSON() as {nonce: string}).nonce).to.eq('i-am-a-nonce');
});
});
6 changes: 3 additions & 3 deletions packages/auth/src/core/providers/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ export class OAuthProvider extends BaseOAuthProvider {
* or the ID token string.
*/
credential(params: OAuthCredentialOptions): OAuthCredential {
return this._credential(params);
return this._credential({...params, nonce: params.rawNonce});
}

/** An internal credential method that accepts more permissive options */
private _credential(
params: OAuthCredentialOptions | OAuthCredentialParams
params: Omit<OAuthCredentialParams, 'signInMethod' | 'providerId'>
): OAuthCredential {
_assert(params.idToken || params.accessToken, AuthErrorCode.ARGUMENT_ERROR);
// For OAuthCredential, sign in method is same as providerId.
Expand Down Expand Up @@ -236,7 +236,7 @@ export class OAuthProvider extends BaseOAuthProvider {
return new OAuthProvider(providerId)._credential({
idToken: oauthIdToken,
accessToken: oauthAccessToken,
rawNonce: nonce,
nonce,
pendingToken
});
} catch (e) {
Expand Down