Add OAuth 2.0 authorization server to enable ChatGPT Web integration (#413)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: samanhappy <2755122+samanhappy@users.noreply.github.com>
Co-authored-by: samanhappy <samanhappy@gmail.com>
This commit is contained in:
Copilot
2025-11-21 13:25:02 +08:00
committed by GitHub
parent 1869f283ba
commit 449e6ea4fd
34 changed files with 4930 additions and 103 deletions

42
src/utils/oauthBearer.ts Normal file
View File

@@ -0,0 +1,42 @@
import { isOAuthServerEnabled } from '../services/oauthServerService.js';
import { getToken as getOAuthStoredToken } from '../models/OAuth.js';
import { findUserByUsername } from '../models/User.js';
import { IUser } from '../types/index.js';
/**
* Resolve an MCPHub user from a raw OAuth bearer token.
*/
export const resolveOAuthUserFromToken = (token?: string): IUser | null => {
if (!token || !isOAuthServerEnabled()) {
return null;
}
const oauthToken = getOAuthStoredToken(token);
if (!oauthToken || oauthToken.accessToken !== token) {
return null;
}
const dbUser = findUserByUsername(oauthToken.username);
return {
username: oauthToken.username,
password: '',
isAdmin: dbUser?.isAdmin || false,
};
};
/**
* Resolve an MCPHub user from an Authorization header.
*/
export const resolveOAuthUserFromAuthHeader = (authHeader?: string): IUser | null => {
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return null;
}
const token = authHeader.substring(7).trim();
if (!token) {
return null;
}
return resolveOAuthUserFromToken(token);
};