|
| 1 | +import { ConfigReader } from '@backstage/config'; |
| 2 | +import type { Request } from 'express'; |
| 3 | +import * as jose from 'jose'; |
| 4 | +import { datumGatewayAuthenticator } from './authModuleDatumGatewayProvider'; |
| 5 | + |
| 6 | +jest.mock('jose', () => { |
| 7 | + const actual = jest.requireActual('jose'); |
| 8 | + return { __esModule: true, ...actual, createRemoteJWKSet: jest.fn() }; |
| 9 | +}); |
| 10 | + |
| 11 | +const ISSUER = 'https://accounts.google.com'; |
| 12 | +const AUDIENCE = 'test-client-id.apps.googleusercontent.com'; |
| 13 | +const HEADER = 'x-auth-request-id-token'; |
| 14 | +const KID = 'test-key'; |
| 15 | + |
| 16 | +let privateKey: jose.KeyLike; |
| 17 | + |
| 18 | +const reqWith = (token?: string): Request => |
| 19 | + ({ |
| 20 | + header: (name: string) => |
| 21 | + name.toLowerCase() === HEADER ? token : undefined, |
| 22 | + } as unknown as Request); |
| 23 | + |
| 24 | +const sign = ( |
| 25 | + claims: jose.JWTPayload, |
| 26 | + opts: { issuer?: string; audience?: string; expSeconds?: number } = {}, |
| 27 | +) => |
| 28 | + new jose.SignJWT(claims) |
| 29 | + .setProtectedHeader({ alg: 'RS256', kid: KID }) |
| 30 | + .setIssuer(opts.issuer ?? ISSUER) |
| 31 | + .setAudience(opts.audience ?? AUDIENCE) |
| 32 | + .setIssuedAt() |
| 33 | + .setExpirationTime(`${opts.expSeconds ?? 3600}s`) |
| 34 | + .sign(privateKey); |
| 35 | + |
| 36 | +const initialize = () => |
| 37 | + datumGatewayAuthenticator.initialize({ |
| 38 | + config: new ConfigReader({ |
| 39 | + header: 'X-Auth-Request-Id-Token', |
| 40 | + issuer: ISSUER, |
| 41 | + audience: AUDIENCE, |
| 42 | + jwksUrl: 'https://www.googleapis.com/oauth2/v3/certs', |
| 43 | + }), |
| 44 | + }); |
| 45 | + |
| 46 | +beforeAll(async () => { |
| 47 | + const pair = await jose.generateKeyPair('RS256'); |
| 48 | + privateKey = pair.privateKey; |
| 49 | + const publicJwk = await jose.exportJWK(pair.publicKey); |
| 50 | + const localJwks = jest |
| 51 | + .requireActual('jose') |
| 52 | + .createLocalJWKSet({ keys: [{ ...publicJwk, kid: KID, alg: 'RS256' }] }); |
| 53 | + (jose.createRemoteJWKSet as jest.Mock).mockReturnValue(localJwks); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('datumGatewayAuthenticator', () => { |
| 57 | + it('accepts a valid Google ID token and exposes its claims', async () => { |
| 58 | + const ctx = initialize(); |
| 59 | + const token = await sign({ email: 'alice@datum.net', name: 'Alice' }); |
| 60 | + |
| 61 | + const { result } = await datumGatewayAuthenticator.authenticate( |
| 62 | + { req: reqWith(token) }, |
| 63 | + ctx, |
| 64 | + ); |
| 65 | + |
| 66 | + expect(result.claims.email).toBe('alice@datum.net'); |
| 67 | + expect(result.claims.aud).toBe(AUDIENCE); |
| 68 | + }); |
| 69 | + |
| 70 | + it('maps claims to a profile with email and display name', async () => { |
| 71 | + const ctx = initialize(); |
| 72 | + const token = await sign({ email: 'alice@datum.net', name: 'Alice' }); |
| 73 | + const { result } = await datumGatewayAuthenticator.authenticate( |
| 74 | + { req: reqWith(token) }, |
| 75 | + ctx, |
| 76 | + ); |
| 77 | + |
| 78 | + const { profile } = await datumGatewayAuthenticator.defaultProfileTransform( |
| 79 | + result, |
| 80 | + {} as any, |
| 81 | + ); |
| 82 | + |
| 83 | + expect(profile).toEqual({ |
| 84 | + email: 'alice@datum.net', |
| 85 | + displayName: 'Alice', |
| 86 | + picture: undefined, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('rejects when the header is missing', async () => { |
| 91 | + const ctx = initialize(); |
| 92 | + await expect( |
| 93 | + datumGatewayAuthenticator.authenticate({ req: reqWith() }, ctx), |
| 94 | + ).rejects.toThrow(/Missing/); |
| 95 | + }); |
| 96 | + |
| 97 | + it('rejects a token with the wrong audience', async () => { |
| 98 | + const ctx = initialize(); |
| 99 | + const token = await sign( |
| 100 | + { email: 'alice@datum.net' }, |
| 101 | + { audience: 'someone-else' }, |
| 102 | + ); |
| 103 | + await expect( |
| 104 | + datumGatewayAuthenticator.authenticate({ req: reqWith(token) }, ctx), |
| 105 | + ).rejects.toThrow(/verification failed/); |
| 106 | + }); |
| 107 | + |
| 108 | + it('rejects a token with the wrong issuer', async () => { |
| 109 | + const ctx = initialize(); |
| 110 | + const token = await sign( |
| 111 | + { email: 'alice@datum.net' }, |
| 112 | + { issuer: 'https://evil.example.com' }, |
| 113 | + ); |
| 114 | + await expect( |
| 115 | + datumGatewayAuthenticator.authenticate({ req: reqWith(token) }, ctx), |
| 116 | + ).rejects.toThrow(/verification failed/); |
| 117 | + }); |
| 118 | + |
| 119 | + it('rejects an expired token', async () => { |
| 120 | + const ctx = initialize(); |
| 121 | + const token = await sign({ email: 'alice@datum.net' }, { expSeconds: -60 }); |
| 122 | + await expect( |
| 123 | + datumGatewayAuthenticator.authenticate({ req: reqWith(token) }, ctx), |
| 124 | + ).rejects.toThrow(/verification failed/); |
| 125 | + }); |
| 126 | +}); |
0 commit comments