Repository navigation
Expand file tree
/
Copy pathintrospection-validator.ts
More file actions
216 lines (181 loc) · 5.77 KB
/
Copy pathintrospection-validator.ts
File metadata and controls
216 lines (181 loc) · 5.77 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import crypto from 'crypto';
import { logger } from '../../core/Logger.js';
import { TokenClaims } from './jwt-validator.js';
export interface IntrospectionConfig {
endpoint: string;
clientId: string;
clientSecret: string;
cacheTTL?: number;
}
interface IntrospectionResponse {
active: boolean;
scope?: string;
client_id?: string;
username?: string;
token_type?: string;
exp?: number;
iat?: number;
nbf?: number;
sub?: string;
aud?: string | string[];
iss?: string;
jti?: string;
[key: string]: unknown;
}
interface CachedIntrospection {
response: IntrospectionResponse;
timestamp: number;
}
export class IntrospectionValidator {
private config: Required<IntrospectionConfig>;
private cache: Map<string, CachedIntrospection>;
constructor(config: IntrospectionConfig) {
this.config = {
cacheTTL: config.cacheTTL || 300000,
...config,
};
this.cache = new Map();
logger.debug(
`IntrospectionValidator initialized with endpoint: ${this.config.endpoint}, cacheTTL: ${this.config.cacheTTL}ms`
);
}
async validate(token: string): Promise<TokenClaims> {
try {
logger.debug('Starting token introspection');
const cached = this.getCachedIntrospection(token);
if (cached) {
logger.debug('Using cached introspection result');
return this.convertToClaims(cached);
}
const response = await this.introspectToken(token);
if (!response.active) {
logger.warn('Token is inactive');
throw new Error('Token is inactive');
}
this.cacheIntrospection(token, response);
const claims = this.convertToClaims(response);
logger.debug('Token introspection successful');
return claims;
} catch (error) {
if (error instanceof Error) {
logger.error(`Token introspection failed: ${error.message}`);
throw error;
}
throw new Error('Token introspection failed: Unknown error');
}
}
private async introspectToken(token: string): Promise<IntrospectionResponse> {
try {
logger.debug('Calling introspection endpoint');
const credentials = Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`
).toString('base64');
const response = await fetch(this.config.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${credentials}`,
},
body: new URLSearchParams({ token }),
});
if (!response.ok) {
throw new Error(
`Introspection endpoint returned ${response.status}: ${response.statusText}`
);
}
const data = (await response.json()) as IntrospectionResponse;
if (typeof data.active !== 'boolean') {
throw new Error('Invalid introspection response: missing active field');
}
logger.debug(
`Introspection response received - active: ${data.active}, sub: ${data.sub || 'N/A'}`
);
return data;
} catch (error) {
if (error instanceof Error) {
logger.error(`Introspection request failed: ${error.message}`);
throw new Error(`Introspection request failed: ${error.message}`);
}
throw new Error('Introspection request failed: Unknown error');
}
}
private getCachedIntrospection(token: string): IntrospectionResponse | null {
const tokenHash = this.hashToken(token);
const cached = this.cache.get(tokenHash);
if (!cached) {
return null;
}
const age = Date.now() - cached.timestamp;
if (age > this.config.cacheTTL) {
logger.debug('Cached introspection expired, removing from cache');
this.cache.delete(tokenHash);
return null;
}
if (cached.response.exp) {
const now = Math.floor(Date.now() / 1000);
if (now >= cached.response.exp) {
logger.debug('Cached token expired, removing from cache');
this.cache.delete(tokenHash);
return null;
}
}
return cached.response;
}
private cacheIntrospection(token: string, response: IntrospectionResponse): void {
const tokenHash = this.hashToken(token);
this.cache.set(tokenHash, {
response,
timestamp: Date.now(),
});
this.cleanupCache();
logger.debug('Introspection result cached');
}
private hashToken(token: string): string {
return crypto.createHash('sha256').update(token).digest('hex');
}
private cleanupCache(): void {
const now = Date.now();
for (const [tokenHash, cached] of this.cache.entries()) {
const age = now - cached.timestamp;
if (age > this.config.cacheTTL) {
this.cache.delete(tokenHash);
} else if (cached.response.exp) {
const nowSec = Math.floor(now / 1000);
if (nowSec >= cached.response.exp) {
this.cache.delete(tokenHash);
}
}
}
}
private convertToClaims(response: IntrospectionResponse): TokenClaims {
if (!response.sub) {
throw new Error('Introspection response missing required field: sub');
}
if (!response.iss) {
throw new Error('Introspection response missing required field: iss');
}
if (!response.aud) {
throw new Error('Introspection response missing required field: aud');
}
if (!response.exp) {
throw new Error('Introspection response missing required field: exp');
}
const now = Math.floor(Date.now() / 1000);
if (now >= response.exp) {
throw new Error('Token has expired');
}
if (response.nbf && now < response.nbf) {
throw new Error('Token not yet valid (nbf claim)');
}
return {
sub: response.sub,
iss: response.iss,
aud: response.aud,
exp: response.exp,
nbf: response.nbf,
iat: response.iat,
scope: response.scope,
...response,
};
}
}