-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathauthorization-server-metadata.ts
More file actions
164 lines (140 loc) · 4.89 KB
/
authorization-server-metadata.ts
File metadata and controls
164 lines (140 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Authorization server metadata endpoint test scenarios for MCP authorization servers
*/
import {
ClientScenarioForAuthorizationServer,
ConformanceCheck,
SpecVersion
} from '../../types';
import { request } from 'undici';
type Status = 'SUCCESS' | 'FAILURE';
export class AuthorizationServerMetadataEndpointScenario implements ClientScenarioForAuthorizationServer {
name = 'authorization-server-metadata-endpoint';
specVersions: SpecVersion[] = ['2025-03-26', '2025-06-18', '2025-11-25'];
description = `Test authorization server metadata endpoint.
**Authorization Server Implementation Requirements:**
**Endpoint**: \`authorization server metadata\`
**Requirements**:
- HTTP response status code MUST be 200 OK
- Content-Type header MUST be application/json
- Return a JSON response including issuer, authorization_endpoint, token_endpoint and response_types_supported
- The issuer value MUST match the URI obtained by removing the well-known URI string from the authorization server metadata URI.`;
async run(serverUrl: string): Promise<ConformanceCheck[]> {
let status: Status = 'SUCCESS';
let errorMessage: string | undefined;
let details: any;
let response: any | null = null;
try {
const wellKnownUrls = this.createWellKnownUrl(serverUrl);
for (const url of wellKnownUrls) {
try {
const checkResponse = await request(url, { method: 'GET' });
if (checkResponse.statusCode === 200) {
response = checkResponse;
break;
}
} catch {
// Ignore the error and proceed to the next loop.
}
}
if (!response) {
throw new Error(
'All authorization server metadata endpoints returned invalid status code.'
);
}
this.validateContentType(response.headers['content-type']);
const body = await this.parseJson(response);
const errors: string[] = [];
this.validateMetadataBody(body, serverUrl, errors);
if (errors.length > 0) {
status = 'FAILURE';
errorMessage = errors.join(', ');
}
details = {
contentType: response.headers['content-type'],
body
};
} catch (error) {
status = 'FAILURE';
errorMessage = error instanceof Error ? error.message : String(error);
}
return [
{
id: 'authorization-server-metadata',
name: 'AuthorizationServerMetadata',
description: 'Valid authorization server metadata response',
status,
timestamp: new Date().toISOString(),
errorMessage,
specReferences: [
{
id: 'Authorization-Server-Metadata',
url: 'https://datatracker.ietf.org/doc/html/rfc8414'
}
],
...(details ? { details } : {})
}
];
}
private createWellKnownUrl(serverUrl: string): string[] {
const base = new URL(serverUrl);
const origin = base.origin;
const path = base.pathname.replace(/\/$/, '');
const urls = new Set<string>();
urls.add(`${origin}/.well-known/oauth-authorization-server${path}`);
urls.add(`${origin}/.well-known/openid-configuration${path}`);
urls.add(`${origin}${path}/.well-known/openid-configuration`);
return Array.from(urls);
}
private validateContentType(contentType?: string | string[]): void {
const valid =
typeof contentType === 'string' &&
contentType.toLowerCase().includes('application/json');
if (!valid) {
throw new Error(`Invalid Content-Type: ${contentType ?? '(missing)'}`);
}
}
private async parseJson(response: any): Promise<Record<string, any>> {
const body = await response.body.json();
if (typeof body !== 'object' || body === null) {
throw new Error('Response body is not an object');
}
return body;
}
private validateMetadataBody(
body: Record<string, any>,
serverUrl: string,
errors: string[]
): void {
this.assertString(
body.authorization_endpoint,
'authorization_endpoint',
errors
);
this.assertString(body.token_endpoint, 'token_endpoint', errors);
if (
!Array.isArray(body.response_types_supported) ||
!body.response_types_supported.includes('code')
) {
errors.push(
'Response body does not include valid "response_types_supported" claim'
);
}
if (
!Array.isArray(body.code_challenge_methods_supported) ||
!body.code_challenge_methods_supported.includes('S256')
) {
errors.push(
'Response body does not include valid "code_challenge_methods_supported" claim'
);
}
if (body.issuer !== serverUrl) {
errors.push(`Invalid issuer: ${body.issuer ?? '(missing)'}`);
}
}
private assertString(value: unknown, name: string, errors: string[]): void {
if (typeof value !== 'string' || value.length === 0) {
errors.push(`Response body does not include valid "${name}" claim`);
}
}
}