mirror of
https://github.com/samanhappy/mcphub.git
synced 2026-01-01 12:18:39 -05:00
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:
42
src/utils/oauthBearer.ts
Normal file
42
src/utils/oauthBearer.ts
Normal 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);
|
||||
};
|
||||
Reference in New Issue
Block a user