|
| 1 | +import { cimdDocumentToApp, isCimdClientId } from '$lib/helpers/oauth2-cimd'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +describe('isCimdClientId', () => { |
| 5 | + it('accepts https URLs', () => { |
| 6 | + expect(isCimdClientId('https://example.com/oauth/client-metadata.json')).toBe(true); |
| 7 | + }); |
| 8 | + |
| 9 | + it('accepts http only for loopback', () => { |
| 10 | + expect(isCimdClientId('http://localhost:3000/client.json')).toBe(true); |
| 11 | + expect(isCimdClientId('http://127.0.0.1/client.json')).toBe(true); |
| 12 | + expect(isCimdClientId('http://example.com/client.json')).toBe(false); |
| 13 | + }); |
| 14 | + |
| 15 | + it('rejects plain app IDs and non-http schemes', () => { |
| 16 | + expect(isCimdClientId('my-app_1.0')).toBe(false); |
| 17 | + expect(isCimdClientId('64f1e2a9b3c4d5e6f7a8')).toBe(false); |
| 18 | + expect(isCimdClientId('javascript:alert(1)')).toBe(false); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('cimdDocumentToApp', () => { |
| 23 | + const clientId = 'https://example.com/oauth/client-metadata.json'; |
| 24 | + |
| 25 | + it('maps RFC 7591 metadata onto the App model', () => { |
| 26 | + const app = cimdDocumentToApp(clientId, { |
| 27 | + client_id: clientId, |
| 28 | + client_name: 'Example App', |
| 29 | + client_uri: 'https://example.com', |
| 30 | + logo_uri: 'https://example.com/logo.png', |
| 31 | + policy_uri: 'https://example.com/privacy', |
| 32 | + tos_uri: 'https://example.com/terms', |
| 33 | + contacts: ['support@example.com'], |
| 34 | + redirect_uris: ['https://example.com/callback'], |
| 35 | + token_endpoint_auth_method: 'none', |
| 36 | + grant_types: ['authorization_code', 'urn:ietf:params:oauth:grant-type:device_code'] |
| 37 | + }); |
| 38 | + |
| 39 | + expect(app.$id).toBe(clientId); |
| 40 | + expect(app.name).toBe('Example App'); |
| 41 | + expect(app.clientUri).toBe('https://example.com'); |
| 42 | + expect(app.logoUri).toBe('https://example.com/logo.png'); |
| 43 | + expect(app.privacyPolicyUrl).toBe('https://example.com/privacy'); |
| 44 | + expect(app.termsUrl).toBe('https://example.com/terms'); |
| 45 | + expect(app.contacts).toEqual(['support@example.com']); |
| 46 | + expect(app.redirectUris).toEqual(['https://example.com/callback']); |
| 47 | + expect(app.type).toBe('public'); |
| 48 | + expect(app.deviceFlow).toBe(true); |
| 49 | + expect(app.enabled).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('falls back to the hostname when client_name is missing', () => { |
| 53 | + const app = cimdDocumentToApp(clientId, { client_id: clientId }); |
| 54 | + expect(app.name).toBe('example.com'); |
| 55 | + expect(app.deviceFlow).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('rejects a document whose client_id does not match its URL', () => { |
| 59 | + expect(() => |
| 60 | + cimdDocumentToApp(clientId, { client_id: 'https://evil.example/other.json' }) |
| 61 | + ).toThrow(); |
| 62 | + expect(() => cimdDocumentToApp(clientId, 'not an object')).toThrow(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('drops unrenderable URI values', () => { |
| 66 | + const app = cimdDocumentToApp(clientId, { |
| 67 | + client_id: clientId, |
| 68 | + logo_uri: 'javascript:alert(1)', |
| 69 | + client_uri: 'not a url', |
| 70 | + contacts: ['ok', 42] |
| 71 | + }); |
| 72 | + expect(app.logoUri).toBe(''); |
| 73 | + expect(app.clientUri).toBe(''); |
| 74 | + expect(app.contacts).toEqual(['ok']); |
| 75 | + }); |
| 76 | +}); |
0 commit comments