|
| 1 | +import * as accessTokenVerifier from '@solid/access-token-verifier'; |
| 2 | +import { JWTPayload } from 'jose'; |
| 3 | +import * as jose from 'jose'; |
| 4 | +import { MockInstance } from 'vitest'; |
| 5 | +import { Credential } from '../../../../src/credentials/Credential'; |
| 6 | +import { OidcVerifier } from '../../../../src/credentials/verify/OidcVerifier'; |
| 7 | + |
| 8 | +vi.mock('jose', () => ({ |
| 9 | + createRemoteJWKSet: vi.fn(), |
| 10 | + decodeJwt: vi.fn(), |
| 11 | + jwtVerify: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('OidcVerifier', (): void => { |
| 15 | + const issuer = 'http://example.org/issuer'; |
| 16 | + const baseUrl = 'http://example.com/uma'; |
| 17 | + const credential: Credential = { |
| 18 | + format: 'http://openid.net/specs/openid-connect-core-1_0.html#IDToken', |
| 19 | + token: 'token', |
| 20 | + }; |
| 21 | + |
| 22 | + const decodedToken: JWTPayload = { |
| 23 | + sub: 'sub', |
| 24 | + iss: issuer, |
| 25 | + aud: baseUrl, |
| 26 | + }; |
| 27 | + const remoteKeySet = 'remoteKeySet'; |
| 28 | + const decodeJwt = vi.spyOn(jose, 'decodeJwt'); |
| 29 | + const jwtVerify = vi.spyOn(jose, 'jwtVerify'); |
| 30 | + const createRemoteJWKSet = vi.spyOn(jose, 'createRemoteJWKSet'); |
| 31 | + const verifierMock = vi.fn(); |
| 32 | + vi.spyOn(accessTokenVerifier, 'createSolidTokenVerifier').mockReturnValue(verifierMock); |
| 33 | + let verifier: OidcVerifier; |
| 34 | + |
| 35 | + beforeEach(async(): Promise<void> => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + decodeJwt.mockReturnValue(decodedToken); |
| 38 | + jwtVerify.mockResolvedValue({ payload: decodedToken } as any); |
| 39 | + createRemoteJWKSet.mockReturnValue(remoteKeySet as any); |
| 40 | + |
| 41 | + verifierMock.mockResolvedValue({ |
| 42 | + webid: 'webId', |
| 43 | + client_id: 'clientId' |
| 44 | + }); |
| 45 | + |
| 46 | + verifier = new OidcVerifier(baseUrl) |
| 47 | + }); |
| 48 | + |
| 49 | + it('errors on non-OIDC credentials.', async(): Promise<void> => { |
| 50 | + await expect(verifier.verify({ format: 'wrong', token: 'token' })).rejects |
| 51 | + .toThrow("Token format wrong does not match this processor's format."); |
| 52 | + }); |
| 53 | + |
| 54 | + it('errors if the server is not part of the audience.', async(): Promise<void> => { |
| 55 | + decodeJwt.mockReturnValue({ ...decodedToken, aud: 'wrong' }); |
| 56 | + await expect(verifier.verify(credential)).rejects.toThrow('This server is not valid audience for the token'); |
| 57 | + |
| 58 | + decodeJwt.mockReturnValue({ ...decodedToken, aud: undefined }); |
| 59 | + await expect(verifier.verify(credential)).rejects.toThrow('This server is not valid audience for the token'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('errors if the issuer is not allowed.', async(): Promise<void> => { |
| 63 | + verifier = new OidcVerifier(baseUrl, [ 'otherIssuer' ]); |
| 64 | + await expect(verifier.verify(credential)).rejects.toThrow('Unsupported issuer'); |
| 65 | + |
| 66 | + verifier = new OidcVerifier(baseUrl, [ issuer ]); |
| 67 | + await expect(verifier.verify(credential)).resolves.toEqual({ |
| 68 | + ['urn:solidlab:uma:claims:types:webid']: 'sub', |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('parsing a Solid OIDC token', (): void => { |
| 73 | + beforeEach(async(): Promise<void> => { |
| 74 | + decodeJwt.mockReturnValue({ ...decodedToken, aud: [ baseUrl, 'solid' ] }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns the extracted WebID.', async(): Promise<void> => { |
| 78 | + await expect(verifier.verify(credential)).resolves.toEqual({ |
| 79 | + ['urn:solidlab:uma:claims:types:webid']: 'webId', |
| 80 | + ['urn:solidlab:uma:claims:types:clientid']: 'clientId', |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('throws an error if the token could not be verified.', async(): Promise<void> => { |
| 85 | + verifierMock.mockRejectedValueOnce(new Error('bad data')); |
| 86 | + await expect(verifier.verify(credential)).rejects.toThrow('Error verifying OIDC ID Token: bad data'); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('parsing a standard OIDC token', (): void => { |
| 91 | + it('errors if the sub claim is missing', async(): Promise<void> => { |
| 92 | + jwtVerify.mockResolvedValue({ payload: { ...decodedToken, sub: undefined } } as any); |
| 93 | + await expect(verifier.verify(credential)).rejects.toThrow('Invalid OIDC token: missing `sub` claim'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns the extracted identity.', async(): Promise<void> => { |
| 97 | + await expect(verifier.verify(credential)).resolves.toEqual({ |
| 98 | + ['urn:solidlab:uma:claims:types:webid']: 'sub', |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns the extracted client identifier.', async(): Promise<void> => { |
| 103 | + jwtVerify.mockResolvedValue({ payload: { ...decodedToken, azp: 'client' } } as any); |
| 104 | + |
| 105 | + await expect(verifier.verify(credential)).resolves.toEqual({ |
| 106 | + ['urn:solidlab:uma:claims:types:webid']: 'sub', |
| 107 | + ['urn:solidlab:uma:claims:types:clientid']: 'client', |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments