-
Notifications
You must be signed in to change notification settings - Fork 230
chore(auth): replace axios with fetch in third-party auth routes #20729
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| import { AuthLogger, AuthRequest } from '../types'; | ||
| import { ConfigType } from '../../config'; | ||
| import { OAuth2Client } from 'google-auth-library'; | ||
| import axios from 'axios'; | ||
| import * as uuid from 'uuid'; | ||
| import * as random from '../crypto/random'; | ||
| import * as jose from 'jose'; | ||
|
|
@@ -279,16 +278,26 @@ export class LinkedAccountHandler { | |
| }; | ||
|
|
||
| try { | ||
| const res = await axios.post( | ||
| const res = await fetch( | ||
| this.config.googleAuthConfig.tokenEndpoint, | ||
| data | ||
| { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(data), | ||
| } | ||
| ); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `Google token endpoint responded with ${res.status}` | ||
| ); | ||
| } | ||
| const tokenResponse = await res.json(); | ||
| // We currently only use the `id_token` after completing the | ||
| // authorization code exchange. In the future we could store a | ||
| // refresh token to do other things like revoking sessions. | ||
| // | ||
| // See https://developers.google.com/identity/protocols/oauth2/openid-connect#exchangecode | ||
| rawIdToken = res.data['id_token']; | ||
| rawIdToken = tokenResponse['id_token']; | ||
|
|
||
| const verifiedToken = await this.googleAuthClient.verifyIdToken({ | ||
| idToken: rawIdToken, | ||
|
|
@@ -328,11 +337,20 @@ export class LinkedAccountHandler { | |
| }; | ||
|
|
||
| try { | ||
| const res = await axios.post( | ||
| this.config.appleAuthConfig.tokenEndpoint, | ||
| new URLSearchParams(data).toString() | ||
| ); | ||
| rawIdToken = res.data['id_token']; | ||
| const res = await fetch(this.config.appleAuthConfig.tokenEndpoint, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/x-www-form-urlencoded', | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping the request identical to what axios previously sent for Apple: form-encoded instead of JSON ( Note: this header added to the call is mandatory - fetch won't add automatically. Test added so we can catch it if it ever gets dropped. |
||
| }, | ||
| body: new URLSearchParams(data).toString(), | ||
| }); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `Apple token endpoint responded with ${res.status}` | ||
| ); | ||
| } | ||
| const tokenResponse = await res.json(); | ||
| rawIdToken = tokenResponse['id_token']; | ||
| idToken = jose.decodeJwt(rawIdToken); | ||
| } catch (err) { | ||
| this.log.error('linked_account.code_exchange_error', err); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import jwt from 'jsonwebtoken'; | ||
| import { getApplePublicKey, getGooglePublicKey } from './third-party-events'; | ||
|
|
||
| jest.mock('jsonwebtoken', () => ({ | ||
| decode: jest.fn(), | ||
| verify: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@fxa/shared/pem-jwk', () => ({ | ||
| jwk2pem: jest.fn(() => 'fake-pem'), | ||
| })); | ||
|
|
||
| describe('third-party-events public key fetching', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding! |
||
| let statsd: any; | ||
| let originalFetch: typeof global.fetch; | ||
|
|
||
| beforeEach(() => { | ||
| statsd = { increment: jest.fn() }; | ||
| originalFetch = global.fetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| global.fetch = originalFetch; | ||
| }); | ||
|
|
||
| describe('getApplePublicKey', () => { | ||
| it('returns the pem for the key matching the token kid', async () => { | ||
| (jwt.decode as jest.Mock).mockReturnValue({ | ||
| header: { kid: 'apple-kid' }, | ||
| }); | ||
| global.fetch = jest.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => ({ keys: [{ kid: 'apple-kid', n: 'x', e: 'AQAB' }] }), | ||
| } as unknown as Response); | ||
|
|
||
| const result = await getApplePublicKey('token', statsd); | ||
|
|
||
| expect(result).toEqual({ pem: 'fake-pem' }); | ||
| }); | ||
|
|
||
| it('throws and increments statsd when the key endpoint responds non-ok', async () => { | ||
| global.fetch = jest | ||
| .fn() | ||
| .mockResolvedValue({ ok: false, status: 503 } as unknown as Response); | ||
|
|
||
| await expect(getApplePublicKey('token', statsd)).rejects.toThrow( | ||
| 'Failed to get Apple public key' | ||
| ); | ||
| expect(statsd.increment).toHaveBeenCalledWith('getApplePublicKey.error'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getGooglePublicKey', () => { | ||
| it('returns the pem and issuer for the key matching the token kid', async () => { | ||
| (jwt.decode as jest.Mock).mockReturnValue({ | ||
| header: { kid: 'google-kid' }, | ||
| }); | ||
| global.fetch = jest | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| jwks_uri: 'https://example.com/jwks', | ||
| issuer: 'https://accounts.google.com', | ||
| }), | ||
| } as unknown as Response) | ||
| .mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| keys: [{ kid: 'google-kid', n: 'x', e: 'AQAB' }], | ||
| }), | ||
| } as unknown as Response); | ||
|
|
||
| const result = await getGooglePublicKey('token', statsd); | ||
|
|
||
| expect(result).toEqual({ | ||
| pem: 'fake-pem', | ||
| issuer: 'https://accounts.google.com', | ||
| }); | ||
| }); | ||
|
|
||
| it('throws and increments statsd when the RISC config endpoint responds non-ok', async () => { | ||
| global.fetch = jest | ||
| .fn() | ||
| .mockResolvedValue({ ok: false, status: 500 } as unknown as Response); | ||
|
|
||
| await expect(getGooglePublicKey('token', statsd)).rejects.toThrow( | ||
| 'Failed to get Google public key' | ||
| ); | ||
| expect(statsd.increment).toHaveBeenCalledWith('getGooglePublicKey.error'); | ||
| }); | ||
|
|
||
| it('throws when the jwks endpoint responds non-ok', async () => { | ||
| global.fetch = jest | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| jwks_uri: 'https://example.com/jwks', | ||
| issuer: 'https://accounts.google.com', | ||
| }), | ||
| } as unknown as Response) | ||
| .mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 502, | ||
| } as unknown as Response); | ||
|
|
||
| await expect(getGooglePublicKey('token', statsd)).rejects.toThrow( | ||
| 'Failed to get Google public key' | ||
| ); | ||
| expect(statsd.increment).toHaveBeenCalledWith('getGooglePublicKey.error'); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping the request identical to what axios previously sent: turn the plain object into a JSON body with
application/jsonheader.