-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathauth.ts
More file actions
272 lines (238 loc) · 7.66 KB
/
Copy pathauth.ts
File metadata and controls
272 lines (238 loc) · 7.66 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { SetOptions } from "@redis/client";
import crypto from "crypto";
import { redisClient } from "../redis.js";
import { McpInstallation, PendingAuthorization, TokenExchange } from "../types.js";
import { OAuthClientInformationFull, OAuthTokens } from "@modelcontextprotocol/sdk/shared/auth.js";
import { logger } from "../utils/logger.js";
export function generatePKCEChallenge(verifier: string): string {
const buffer = Buffer.from(verifier);
const hash = crypto.createHash("sha256").update(buffer);
return hash.digest("base64url");
}
export function generateToken(): string {
return crypto.randomBytes(32).toString("hex");
}
function sha256(data: string): string {
return crypto.createHash("sha256").update(data).digest("hex");
}
function encryptString({ text, key }: { text: string; key: string }): string {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(key, "hex"), iv);
let encrypted = cipher.update(text, "utf-8", "hex");
encrypted += cipher.final("hex");
return `${iv.toString("hex")}:${encrypted}`;
}
export function decryptString({
encryptedText,
key,
}: {
encryptedText: string;
key: string;
}): string {
const [ivHex, encrypted] = encryptedText.split(":");
const iv = Buffer.from(ivHex, "hex");
const decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(key, "hex"), iv);
let decrypted = decipher.update(encrypted, "hex", "utf-8");
decrypted += decipher.final("utf-8");
return decrypted;
}
const CLIENT_REGISTRATION_KEY_PREFIX = "client:";
const PENDING_AUTHORIZATION_KEY_PREFIX = "pending:";
const MCP_AUTHORIZATION_KEY_PREFIX = "mcp:";
const TOKEN_EXCHANGE_KEY_PREFIX = "exch:";
const REFRESH_TOKEN_KEY_PREFIX = "refresh:";
// Timeouts of redis keys for different stages of the OAuth flow
const REDIS_PENDING_AUTHORIZATION_EXPIRY_SEC = 10 * 60; // 10 minutes in seconds - authorization code -> PendingAuthorization
const REDIS_TOKEN_EXCHANGE_EXPIRY_SEC = 10 * 60; // 10 minutes in seconds - authorization code -> MCP access token
const REDIS_UPSTREAM_INSTALLATION_EXPIRY_SEC = 7 * 24 * 60 * 60; // 7 days in seconds - MCP access token -> UpstreamInstallation
const REDIS_REFRESH_TOKEN_EXPIRY_SEC = 7 * 24 * 60 * 60; // 7 days in seconds - MCP refresh token -> access token
// Access token expiry
const ACCESS_TOKEN_EXPIRY_SEC = 60 * 60 // 1 hour in seconds
async function saveEncrypted<T>({
prefix,
key,
data,
options,
}: {
prefix: string;
key: string;
data: T;
options?: SetOptions;
}) {
const value = encryptString({
text: JSON.stringify(data),
key: key,
});
return await redisClient.set(prefix + sha256(key), value, options);
}
async function readEncrypted<T>({
prefix,
key,
del = false,
}: {
prefix: string;
key: string;
del?: boolean;
}): Promise<T | undefined> {
const data = del
? await redisClient.getDel(prefix + sha256(key))
: await redisClient.get(prefix + sha256(key));
if (!data) {
return undefined;
}
const decoded = decryptString({
encryptedText: data,
key: key,
});
return JSON.parse(decoded);
}
export function generateMcpTokens(): OAuthTokens {
// Generate MCP access token and store both tokens
const mcpAccessToken = generateToken();
const mcpRefreshToken = generateToken();
return {
access_token: mcpAccessToken,
refresh_token: mcpRefreshToken,
expires_in: ACCESS_TOKEN_EXPIRY_SEC,
token_type: "Bearer",
}
}
export async function saveClientRegistration(
clientId: string,
registration: OAuthClientInformationFull,
) {
await redisClient.set(
CLIENT_REGISTRATION_KEY_PREFIX + clientId,
JSON.stringify(registration),
options: { EX: REDIS_PENDING_AUTHORIZATION_EXPIRY_SEC },
);
}
export async function getClientRegistration(
clientId: string,
): Promise<OAuthClientInformationFull | undefined> {
const data = await redisClient.get(CLIENT_REGISTRATION_KEY_PREFIX + clientId);
if (!data) {
return undefined;
}
return JSON.parse(data);
}
export async function savePendingAuthorization(
authorizationCode: string,
pendingAuthorization: PendingAuthorization,
) {
await saveEncrypted({
prefix: PENDING_AUTHORIZATION_KEY_PREFIX,
key: authorizationCode,
data: pendingAuthorization,
options: { EX: REDIS_PENDING_AUTHORIZATION_EXPIRY_SEC },
});
}
export async function readPendingAuthorization(
authorizationCode: string,
): Promise<PendingAuthorization | undefined> {
return readEncrypted<PendingAuthorization>({
prefix: PENDING_AUTHORIZATION_KEY_PREFIX,
key: authorizationCode,
});
}
export async function saveMcpInstallation(
mcpAccessToken: string,
installation: McpInstallation,
) {
await saveEncrypted({
prefix: MCP_AUTHORIZATION_KEY_PREFIX,
key: mcpAccessToken,
data: installation,
options: { EX: REDIS_UPSTREAM_INSTALLATION_EXPIRY_SEC },
});
}
export async function readMcpInstallation(
mcpAccessToken: string,
): Promise<McpInstallation | undefined> {
return readEncrypted<McpInstallation>({
prefix: MCP_AUTHORIZATION_KEY_PREFIX,
key: mcpAccessToken,
});
}
// This just links the refresh token to the upstream installation + mcp access token
export async function saveRefreshToken(
refreshToken: string,
mcpAccessToken: string,
) {
saveEncrypted({
prefix: REFRESH_TOKEN_KEY_PREFIX,
key: refreshToken,
data: mcpAccessToken,
options: { EX: REDIS_REFRESH_TOKEN_EXPIRY_SEC },
})
}
export async function readRefreshToken(
refreshToken: string,
): Promise<string | undefined> {
return readEncrypted<string>({
prefix: REFRESH_TOKEN_KEY_PREFIX,
key: refreshToken,
});
}
export async function revokeMcpInstallation(
mcpAccessToken: string,
): Promise<void> {
const installation = await readEncrypted<McpInstallation>({
prefix: MCP_AUTHORIZATION_KEY_PREFIX,
key: mcpAccessToken,
del: true,
});
if (!installation) {
return;
}
// Revoke upstream tokens here
}
export async function saveTokenExchange(
authorizationCode: string,
tokenExchange: TokenExchange,
) {
await saveEncrypted({
prefix: TOKEN_EXCHANGE_KEY_PREFIX,
key: authorizationCode,
data: tokenExchange,
options: { EX: REDIS_TOKEN_EXCHANGE_EXPIRY_SEC },
});
}
/**
* Exchanges a temporary authorization code for an MCP access token. Will only succeed the first time.
*/
export async function exchangeToken(
authorizationCode: string,
): Promise<TokenExchange | undefined> {
const data = await redisClient.get(TOKEN_EXCHANGE_KEY_PREFIX + sha256(authorizationCode));
if (!data) {
return undefined;
}
const decoded = decryptString({
encryptedText: data,
key: authorizationCode,
});
const tokenExchange: TokenExchange = JSON.parse(decoded);
if (tokenExchange.alreadyUsed) {
logger.error('Duplicate use of authorization code detected; revoking tokens', undefined, {
authorizationCode: authorizationCode.substring(0, 8) + '...'
});
await revokeMcpInstallation(tokenExchange.mcpAccessToken);
throw new Error("Duplicate use of authorization code detected; tokens revoked");
}
const rereadData = await saveEncrypted({
prefix: TOKEN_EXCHANGE_KEY_PREFIX,
key: authorizationCode,
data: { ...tokenExchange, alreadyUsed: true },
options: { KEEPTTL: true, GET: true },
});
if (rereadData !== data) {
// Data concurrently changed while we were updating it. This necessarily means a duplicate use.
logger.error('Duplicate use of authorization code detected (concurrent update); revoking tokens', undefined, {
authorizationCode: authorizationCode.substring(0, 8) + '...'
});
await revokeMcpInstallation(tokenExchange.mcpAccessToken);
throw new Error("Duplicate use of authorization code detected; tokens revoked");
}
return tokenExchange;
}