Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: restrict mcp email

Nevo David117e945ab794
diff --git a/.env.example b/.env.example
index dba96b8df..7a5128488 100644
--- a/.env.example
+++ b/.env.example
@@ -100,6 +100,9 @@ EXTENSION_ID=""
# Misc Settings
OPENAI_API_KEY=""
+# OAuth client configured for the ChatGPT app. Only this client can receive
+# verified user email claims for ChatGPT Enterprise domain restrictions.
+OPENAI_OAUTH_CLIENT_ID=""
NEXT_PUBLIC_DISCORD_SUPPORT=""
NEXT_PUBLIC_POLOTNO=""
# NOT_SECURED=false
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 4da707699..36193c7f0 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -91,6 +91,8 @@ services:
# === Misc Settings
OPENAI_API_KEY: ''
+ # Only this OAuth client can receive verified user email claims via OIDC.
+ OPENAI_OAUTH_CLIENT_ID: ''
NEXT_PUBLIC_DISCORD_SUPPORT: ''
NEXT_PUBLIC_POLOTNO: ''
API_LIMIT: 30
diff --git a/libraries/nestjs-libraries/src/chat/start.mcp.ts b/libraries/nestjs-libraries/src/chat/start.mcp.ts
index 3167866b3..0090ffb7f 100644
--- a/libraries/nestjs-libraries/src/chat/start.mcp.ts
+++ b/libraries/nestjs-libraries/src/chat/start.mcp.ts
@@ -17,7 +17,13 @@ const fixAcceptHeader = (req: Request) => {
}
};
-const oauthScopes = ['openid', 'email', 'mcp:read', 'mcp:write'];
+const openAiOAuthClientId = process.env.OPENAI_OAUTH_CLIENT_ID?.trim();
+const enableOidcEmailClaims = Boolean(openAiOAuthClientId);
+const oauthScopes = [
+ ...(enableOidcEmailClaims ? ['openid', 'email'] : []),
+ 'mcp:read',
+ 'mcp:write',
+];
export const startMcp = async (app: INestApplication) => {
const mastraService = app.get(MastraService, { strict: false });
@@ -152,7 +158,9 @@ export const startMcp = async (app: INestApplication) => {
issuer: oauthResource,
authorization_endpoint: `${process.env.FRONTEND_URL}/oauth/authorize`,
token_endpoint: `${process.env.NEXT_PUBLIC_OVERRIDE_BACKEND_URL || process.env.NEXT_PUBLIC_BACKEND_URL}/oauth/token`,
- userinfo_endpoint: `${process.env.NEXT_PUBLIC_OVERRIDE_BACKEND_URL || process.env.NEXT_PUBLIC_BACKEND_URL}/oauth/userinfo`,
+ ...(enableOidcEmailClaims && {
+ userinfo_endpoint: `${process.env.NEXT_PUBLIC_OVERRIDE_BACKEND_URL || process.env.NEXT_PUBLIC_BACKEND_URL}/oauth/userinfo`,
+ }),
response_types_supported: ['code'],
grant_types_supported: ['authorization_code'],
token_endpoint_auth_methods_supported: ['client_secret_post'],
@@ -162,7 +170,7 @@ export const startMcp = async (app: INestApplication) => {
});
app.use('/.well-known/openid-configuration', async (req: Request, res: Response, next: () => void) => {
- if (req.path !== '/mcp-oauth') {
+ if (req.path !== '/mcp-oauth' || !enableOidcEmailClaims) {
next();
return;
}
@@ -188,7 +196,7 @@ export const startMcp = async (app: INestApplication) => {
code_challenge_methods_supported: ['S256'],
scopes_supported: oauthScopes,
subject_types_supported: ['public'],
- claims_supported: ['sub', 'email', 'email_verified', 'name'],
+ claims_supported: ['sub', 'email', 'email_verified'],
});
});
diff --git a/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.repository.ts b/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.repository.ts
index ec899cdba..0d20110ee 100644
--- a/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.repository.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.repository.ts
@@ -188,6 +188,11 @@ export class OAuthRepository {
revokedAt: null,
},
include: {
+ oauthApp: {
+ select: {
+ clientId: true,
+ },
+ },
organization: {
include: {
subscription: {
@@ -203,8 +208,6 @@ export class OAuthRepository {
select: {
id: true,
email: true,
- name: true,
- lastName: true,
activated: true,
},
},
diff --git a/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.service.ts b/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.service.ts
index 1dd52b1e3..fbb0c29df 100644
--- a/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.service.ts
@@ -6,6 +6,18 @@ import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { AuthService } from '@gitroom/helpers/auth/auth.service';
import { extractBearerToken } from '@gitroom/nestjs-libraries/chat/oauth-types';
+const openAiOAuthClientId = () =>
+ process.env.OPENAI_OAUTH_CLIENT_ID?.trim();
+
+const enableOidcEmailClaims = () => Boolean(openAiOAuthClientId());
+
+const oauthScope = (clientId: string) =>
+ [
+ ...(clientId === openAiOAuthClientId() ? ['openid', 'email'] : []),
+ 'mcp:read',
+ 'mcp:write',
+ ].join(' ');
+
@Injectable()
export class OAuthService {
constructor(private _oauthRepository: OAuthRepository) {}
@@ -152,7 +164,7 @@ export class OAuthService {
cus: paymentId,
access_token: token,
token_type: 'bearer',
- scope: 'openid email mcp:read mcp:write',
+ scope: oauthScope(clientId),
};
}
@@ -162,6 +174,16 @@ export class OAuthService {
}
async getUserInfo(authorization?: string) {
+ if (!enableOidcEmailClaims()) {
+ throw new HttpException(
+ {
+ error: 'not_found',
+ error_description: 'OIDC email claims are not enabled',
+ },
+ HttpStatus.NOT_FOUND
+ );
+ }
+
const token = extractBearerToken(authorization);
if (!token) {
throw new HttpException(
@@ -178,14 +200,22 @@ export class OAuthService {
);
}
+ if (authorizationRecord.oauthApp.clientId !== openAiOAuthClientId()) {
+ throw new HttpException(
+ {
+ error: 'insufficient_scope',
+ error_description:
+ 'This OAuth client is not authorized to access email claims',
+ },
+ HttpStatus.FORBIDDEN
+ );
+ }
+
const { user } = authorizationRecord;
return {
sub: user.id,
email: user.email,
email_verified: user.activated,
- ...(user.name || user.lastName
- ? { name: [user.name, user.lastName].filter(Boolean).join(' ') }
- : {}),
};
}