Skip to content

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

feat(oauth): accept client_secret_basic on the token endpoint

Nevo Davidf22d8c82d6d9
diff --git a/apps/backend/src/api/routes/oauth.controller.ts b/apps/backend/src/api/routes/oauth.controller.ts
index 9076b75b5..da4db28de 100644
--- a/apps/backend/src/api/routes/oauth.controller.ts
+++ b/apps/backend/src/api/routes/oauth.controller.ts
@@ -17,6 +17,7 @@ import { User, Organization } from '@prisma/client';
import { AuthorizeOAuthQueryDto, ApproveOAuthDto } from '@gitroom/nestjs-libraries/dtos/oauth/authorize-oauth.dto';
import { TokenExchangeDto } from '@gitroom/nestjs-libraries/dtos/oauth/token-exchange.dto';
import { RegisterClientDto } from '@gitroom/nestjs-libraries/dtos/oauth/register-client.dto';
+import { extractBasicCredentials } from '@gitroom/nestjs-libraries/chat/oauth-types';
@ApiTags('OAuth')
@Controller('/oauth')
@@ -54,7 +55,10 @@ export class OAuthController {
}
@Post('/token')
- async token(@Body() body: TokenExchangeDto) {
+ async token(
+ @Body() body: TokenExchangeDto,
+ @Headers('authorization') authorization?: string
+ ) {
if (body.grant_type !== 'authorization_code') {
throw new HttpException(
{ error: 'unsupported_grant_type' },
@@ -62,10 +66,21 @@ export class OAuthController {
);
}
+ // client_secret_basic puts the credentials in the Authorization header,
+ // client_secret_post and public clients put them in the body
+ const basic = extractBasicCredentials(authorization);
+ const clientId = basic?.clientId || body.client_id;
+ if (!clientId) {
+ throw new HttpException(
+ { error: 'invalid_client' },
+ HttpStatus.UNAUTHORIZED
+ );
+ }
+
return this._oauthService.exchangeCodeForToken(
body.code,
- body.client_id,
- body.client_secret,
+ clientId,
+ basic?.clientSecret || body.client_secret,
body.code_verifier,
body.redirect_uri
);
diff --git a/libraries/nestjs-libraries/src/chat/oauth-types.ts b/libraries/nestjs-libraries/src/chat/oauth-types.ts
index 67b4ec448..720625c84 100644
--- a/libraries/nestjs-libraries/src/chat/oauth-types.ts
+++ b/libraries/nestjs-libraries/src/chat/oauth-types.ts
@@ -92,6 +92,36 @@ export function generateProtectedResourceMetadata(config: MCPServerOAuthConfig):
};
}
+// RFC 6749 Β§2.3.1 client_secret_basic: "Basic base64(urlencode(id):urlencode(secret))"
+export function extractBasicCredentials(
+ authHeader: string | null | undefined,
+): { clientId: string; clientSecret: string } | undefined {
+ if (!authHeader) return undefined;
+
+ const prefix = 'basic ';
+ if (authHeader.length <= prefix.length) return undefined;
+ if (authHeader.slice(0, prefix.length).toLowerCase() !== prefix) return undefined;
+
+ let decoded: string;
+ try {
+ decoded = Buffer.from(authHeader.slice(prefix.length).trim(), 'base64').toString('utf8');
+ } catch {
+ return undefined;
+ }
+
+ const separator = decoded.indexOf(':');
+ if (separator <= 0) return undefined;
+
+ try {
+ return {
+ clientId: decodeURIComponent(decoded.slice(0, separator)),
+ clientSecret: decodeURIComponent(decoded.slice(separator + 1)),
+ };
+ } catch {
+ return undefined;
+ }
+}
+
export function extractBearerToken(authHeader: string | null | undefined): string | undefined {
if (!authHeader) return undefined;
diff --git a/libraries/nestjs-libraries/src/chat/start.mcp.ts b/libraries/nestjs-libraries/src/chat/start.mcp.ts
index 889079921..74e8b6abd 100644
--- a/libraries/nestjs-libraries/src/chat/start.mcp.ts
+++ b/libraries/nestjs-libraries/src/chat/start.mcp.ts
@@ -165,7 +165,7 @@ export const startMcp = async (app: INestApplication) => {
}),
response_types_supported: ['code'],
grant_types_supported: ['authorization_code'],
- token_endpoint_auth_methods_supported: ['client_secret_post', 'none'],
+ token_endpoint_auth_methods_supported: ['client_secret_basic', 'client_secret_post', 'none'],
code_challenge_methods_supported: ['S256'],
scopes_supported: oauthScopes,
});
@@ -195,7 +195,7 @@ export const startMcp = async (app: INestApplication) => {
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', 'none'],
+ token_endpoint_auth_methods_supported: ['client_secret_basic', 'client_secret_post', 'none'],
code_challenge_methods_supported: ['S256'],
scopes_supported: oauthScopes,
subject_types_supported: ['public'],
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 19adfe0f6..3b7cffe43 100644
--- a/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/oauth/oauth.service.ts
@@ -180,6 +180,13 @@ export class OAuthService {
.catch(() => {});
const isPublicClient = dto.token_endpoint_auth_method === 'none';
+ // The token endpoint accepts the secret from either place; the stored
+ // method only mirrors back what the client asked for
+ const tokenEndpointAuthMethod = isPublicClient
+ ? 'none'
+ : dto.token_endpoint_auth_method === 'client_secret_basic'
+ ? 'client_secret_basic'
+ : 'client_secret_post';
const clientId = 'pcd_' + makeId(32);
const clientSecret = isPublicClient ? undefined : 'pcs_' + makeId(48);
@@ -189,7 +196,7 @@ export class OAuthService {
redirectUris: JSON.stringify(redirectUris),
clientId,
clientSecret: clientSecret && AuthService.fixedEncryption(clientSecret),
- tokenEndpointAuthMethod: isPublicClient ? 'none' : 'client_secret_post',
+ tokenEndpointAuthMethod,
});
return {
@@ -198,7 +205,7 @@ export class OAuthService {
client_id_issued_at: Math.floor(app.createdAt.getTime() / 1000),
client_name: app.name,
redirect_uris: redirectUris,
- token_endpoint_auth_method: isPublicClient ? 'none' : 'client_secret_post',
+ token_endpoint_auth_method: tokenEndpointAuthMethod,
grant_types: ['authorization_code'],
response_types: ['code'],
scope: 'mcp:read mcp:write',
diff --git a/libraries/nestjs-libraries/src/dtos/oauth/token-exchange.dto.ts b/libraries/nestjs-libraries/src/dtos/oauth/token-exchange.dto.ts
index 36a3c22bf..ca3b3b7fe 100644
--- a/libraries/nestjs-libraries/src/dtos/oauth/token-exchange.dto.ts
+++ b/libraries/nestjs-libraries/src/dtos/oauth/token-exchange.dto.ts
@@ -9,9 +9,12 @@ export class TokenExchangeDto {
@IsDefined()
code: string;
+ // Optional here because client_secret_basic clients send the id and secret
+ // in the Authorization header; the controller merges the two sources and
+ // the service rejects a request that ends up without a client_id
@IsString()
- @IsDefined()
- client_id: string;
+ @IsOptional()
+ client_id?: string;
// Optional to allow PKCE-only public clients (dynamic registration);
// the service still enforces it for confidential clients