Skip to content

Commit ef42419

Browse files
nickytonlineclaude
andcommitted
refactor(oauth): remove hardcoded demo values and improve user ID generation
- Replace hardcoded "demo-user" fallbacks with proper random user ID generation - Update variable names from "demoClient" to "configuredClient" for clarity - Generate unique user IDs using randomBytes for better security - Update comments to reflect production considerations - Add generateUserId() method to OAuthProvider for consistent ID generation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 14459ac commit ef42419

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/auth/oauth-model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Client = OAuth2Server.Client;
99
type Token = OAuth2Server.Token;
1010
type User = OAuth2Server.User;
1111

12-
// In-memory storage for demo purposes
12+
// In-memory storage (use persistent storage in production)
1313
// In production, use a proper database
1414
const clients = new Map<string, Client>();
1515
const users = new Map<string, User>();
@@ -18,15 +18,15 @@ const tokens = new Map<string, Token>();
1818

1919
// Get client configuration from environment
2020
const config = getConfig();
21-
const demoClient: Client = {
21+
const configuredClient: Client = {
2222
id: config.OAUTH_CLIENT_ID,
2323
clientSecret: config.OAUTH_CLIENT_SECRET,
2424
redirectUris: ['http://localhost:3000/callback', 'vscode://ms-vscode.claude-dev'],
2525
grants: ['authorization_code']
2626
};
2727

2828
// Initialize client data
29-
clients.set(demoClient.id, demoClient);
29+
clients.set(configuredClient.id, configuredClient);
3030

3131
export const oauthModel: AuthorizationCodeModel = {
3232
/**
@@ -214,7 +214,7 @@ export const oauthModel: AuthorizationCodeModel = {
214214
scope
215215
});
216216

217-
// For demo purposes, allow all requested scopes
217+
// Simplified scope validation - implement proper scope checking
218218
// In production, implement proper scope validation
219219
const allowedScopes = ['read', 'write', 'mcp'];
220220
const validScopes = scope.filter(s => allowedScopes.includes(s));

src/auth/oauth-provider.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class OAuthProvider {
160160
const expiresIn = 3600;
161161

162162
// Store access token with user info from external tokens
163-
const userId = codeData.userId || codeData.externalTokens?.accessToken.substring(0, 8) || "demo-user";
163+
const userId = codeData.userId || this.generateUserId();
164164
this.#accessTokens.set(accessToken, {
165165
userId,
166166
scope: codeData.scope,
@@ -215,6 +215,13 @@ export class OAuthProvider {
215215
};
216216
}
217217

218+
/**
219+
* Generate a unique user ID
220+
*/
221+
private generateUserId(): string {
222+
return `user-${randomBytes(16).toString('hex')}`;
223+
}
224+
218225
/**
219226
* Clean up expired codes and tokens
220227
*/

src/auth/oauth-server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import OAuth2Server from "oauth2-server";
22
import { generateChallenge, verifyChallenge } from "pkce-challenge";
3+
import { randomBytes } from "node:crypto";
34
import { logger } from "../logger.ts";
45

56
interface Client {
@@ -139,9 +140,11 @@ export class ManagedOAuthServer {
139140
return token;
140141
},
141142

142-
// User verification (simplified for demo)
143+
// User verification - should be replaced with real authentication
143144
getUser: async () => {
144-
return { id: "demo-user" };
145+
// Generate a unique user ID for each session
146+
const userId = `user-${randomBytes(8).toString('hex')}`;
147+
return { id: userId };
145148
},
146149

147150
// Scope verification

src/auth/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export function createCallbackHandler(oauthProvider: OAuthProvider) {
236236
expiresIn: tokenResponse.expires_in,
237237
scope: tokenResponse.scope
238238
},
239-
`external-user-${requestId.substring(0, 8)}` // Simple user ID for demo
239+
`external-user-${randomBytes(8).toString('hex')}` // Generate unique user ID
240240
);
241241

242242
logger.info("Token exchange completed, MCP auth code generated", {

0 commit comments

Comments
 (0)