|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 6 | +import { ConfigService } from '@nestjs/config'; |
| 7 | +import { MozLoggerService } from '@fxa/shared/mozlog'; |
| 8 | +import { FidoMdsService } from './fido-mds.service'; |
| 9 | + |
| 10 | +const MDS_URL = 'https://mds.fidoalliance.org/'; |
| 11 | +const CACHE_TTL_SECONDS = 7 * 24 * 60 * 60; // 7 days |
| 12 | + |
| 13 | +const mockConfigService = { |
| 14 | + get: (key: string) => { |
| 15 | + if (key === 'fidoMds') { |
| 16 | + return { |
| 17 | + url: MDS_URL, |
| 18 | + cacheTtlSeconds: CACHE_TTL_SECONDS, |
| 19 | + fetchTimeoutSeconds: 10, |
| 20 | + }; |
| 21 | + } |
| 22 | + return undefined; |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +/** Build a minimal JWT-shaped string with a base64url-encoded payload. */ |
| 27 | +function makeJwt(payload: object): string { |
| 28 | + const payloadB64 = Buffer.from(JSON.stringify(payload)).toString('base64url'); |
| 29 | + return `header.${payloadB64}.signature`; |
| 30 | +} |
| 31 | + |
| 32 | +const YUBIKEY_AAGUID = 'fa2b99dc-9e39-4257-8f92-4a30d23c4118'; |
| 33 | +const UNKNOWN_AAGUID = '00000000-0000-0000-0000-000000000000'; |
| 34 | + |
| 35 | +const MDS_ENTRIES = [ |
| 36 | + { |
| 37 | + aaguid: YUBIKEY_AAGUID, |
| 38 | + metadataStatement: { description: 'YubiKey 5 Series with NFC' }, |
| 39 | + }, |
| 40 | + { |
| 41 | + aaguid: 'cb69481e-8ff7-4039-93ec-0a2729a154a8', |
| 42 | + metadataStatement: { description: 'YubiKey 5 FIPS Series' }, |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +describe('FidoMdsService', () => { |
| 47 | + let service: FidoMdsService; |
| 48 | + let mockFetch: jest.SpyInstance; |
| 49 | + |
| 50 | + const mockLog = { |
| 51 | + info: jest.fn(), |
| 52 | + warn: jest.fn(), |
| 53 | + }; |
| 54 | + |
| 55 | + beforeEach(async () => { |
| 56 | + mockFetch = jest.spyOn(global, 'fetch').mockResolvedValue({ |
| 57 | + ok: true, |
| 58 | + text: () => Promise.resolve(makeJwt({ entries: MDS_ENTRIES })), |
| 59 | + } as unknown as Response); |
| 60 | + |
| 61 | + const module: TestingModule = await Test.createTestingModule({ |
| 62 | + providers: [ |
| 63 | + FidoMdsService, |
| 64 | + { provide: MozLoggerService, useValue: mockLog }, |
| 65 | + { provide: ConfigService, useValue: mockConfigService }, |
| 66 | + ], |
| 67 | + }).compile(); |
| 68 | + |
| 69 | + service = module.get(FidoMdsService); |
| 70 | + }); |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + jest.restoreAllMocks(); |
| 74 | + jest.useRealTimers(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should be defined', () => { |
| 78 | + expect(service).toBeDefined(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns the authenticator name for a known AAGUID', async () => { |
| 82 | + const name = await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 83 | + expect(name).toBe('YubiKey 5 Series with NFC'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('is case-insensitive for AAGUID lookup', async () => { |
| 87 | + const name = await service.getAuthenticatorName( |
| 88 | + YUBIKEY_AAGUID.toUpperCase() |
| 89 | + ); |
| 90 | + expect(name).toBe('YubiKey 5 Series with NFC'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns undefined for an unknown AAGUID', async () => { |
| 94 | + const name = await service.getAuthenticatorName(UNKNOWN_AAGUID); |
| 95 | + expect(name).toBeUndefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('fetches the MDS only once for multiple concurrent calls', async () => { |
| 99 | + await Promise.all([ |
| 100 | + service.getAuthenticatorName(YUBIKEY_AAGUID), |
| 101 | + service.getAuthenticatorName(YUBIKEY_AAGUID), |
| 102 | + service.getAuthenticatorName(YUBIKEY_AAGUID), |
| 103 | + ]); |
| 104 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | + |
| 107 | + it('does not re-fetch before TTL expires', async () => { |
| 108 | + await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 109 | + await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 110 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 111 | + }); |
| 112 | + |
| 113 | + it('re-fetches after TTL expires', async () => { |
| 114 | + jest.useFakeTimers(); |
| 115 | + jest.setSystemTime(new Date('2023-01-01T12:00:00.000Z')); |
| 116 | + |
| 117 | + await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 118 | + |
| 119 | + jest.setSystemTime(new Date('2023-01-10T12:00:00.000Z')); |
| 120 | + |
| 121 | + await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 122 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns undefined and allows retry when fetch fails', async () => { |
| 126 | + mockFetch.mockRejectedValueOnce(new Error('network error')); |
| 127 | + |
| 128 | + const name = await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 129 | + expect(name).toBeUndefined(); |
| 130 | + expect(mockLog.warn).toHaveBeenCalledWith( |
| 131 | + 'FidoMdsService: fetch/parse failed', |
| 132 | + expect.anything() |
| 133 | + ); |
| 134 | + |
| 135 | + // Next call should retry |
| 136 | + const retried = await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 137 | + expect(retried).toBe('YubiKey 5 Series with NFC'); |
| 138 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns undefined and allows retry when server returns non-ok status', async () => { |
| 142 | + mockFetch.mockResolvedValueOnce({ ok: false, status: 503 }); |
| 143 | + |
| 144 | + const name = await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 145 | + expect(name).toBeUndefined(); |
| 146 | + expect(mockLog.warn).toHaveBeenCalled(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('logs a cache-refreshed message with entry count on success', async () => { |
| 150 | + await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 151 | + expect(mockLog.info).toHaveBeenCalledWith( |
| 152 | + 'FidoMdsService: cache refreshed', |
| 153 | + { entries: MDS_ENTRIES.length } |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it('skips entries that are missing aaguid or description', async () => { |
| 158 | + mockFetch.mockResolvedValueOnce({ |
| 159 | + ok: true, |
| 160 | + text: () => |
| 161 | + Promise.resolve( |
| 162 | + makeJwt({ |
| 163 | + entries: [ |
| 164 | + { aaguid: 'aaguid-no-description' }, |
| 165 | + { metadataStatement: { description: 'no aaguid' } }, |
| 166 | + ...MDS_ENTRIES, |
| 167 | + ], |
| 168 | + }) |
| 169 | + ), |
| 170 | + }); |
| 171 | + |
| 172 | + // Only the valid MDS_ENTRIES should be in the cache |
| 173 | + const name = await service.getAuthenticatorName(YUBIKEY_AAGUID); |
| 174 | + expect(name).toBe('YubiKey 5 Series with NFC'); |
| 175 | + |
| 176 | + const missing = await service.getAuthenticatorName('aaguid-no-description'); |
| 177 | + expect(missing).toBeUndefined(); |
| 178 | + }); |
| 179 | +}); |
0 commit comments