Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

support pkce oauth

ahmedcoder01369a6c9617c7
diff --git a/packages/app/src/backend/middlewares/oauthStrategy.mw.ts b/packages/app/src/backend/middlewares/oauthStrategy.mw.ts
index bc06a021..0f8072e4 100644
--- a/packages/app/src/backend/middlewares/oauthStrategy.mw.ts
+++ b/packages/app/src/backend/middlewares/oauthStrategy.mw.ts
@@ -3,6 +3,24 @@ import passport from 'passport';
import { replaceTemplateVariablesOptimized } from '../routes/oauth/helper/oauthHelper';
import { strategyConfig } from '../routes/oauth/helper/strategyConfig';
+/**
+ * Hosts that require HTTP Basic auth for the token exchange instead of POST body params.
+ * Parsed via URL hostname for robust matching.
+ */
+const BASIC_AUTH_HOSTS = ['twitter.com', 'api.twitter.com', 'x.com', 'api.x.com'];
+
+/** Checks whether the token endpoint requires HTTP Basic auth by inspecting URL hostnames. */
+function requiresBasicAuthHeader(body: Record<string, string>): boolean {
+ const urlsToCheck = [body.authorizationURL, body.tokenURL].filter(Boolean);
+ return urlsToCheck.some((rawUrl) => {
+ try {
+ return BASIC_AUTH_HOSTS.includes(new URL(rawUrl).hostname);
+ } catch {
+ return false;
+ }
+ });
+}
+
export const oauthStrategyInitialization = async (req, res, next) => {
const { service, scope } = req.body;
if (!service) {
@@ -40,6 +58,28 @@ export const oauthStrategyInitialization = async (req, res, next) => {
}
});
+ if (isOAuth2) {
+ const userEnabledPkce = updatedReqBody.pkce === 'true' || updatedReqBody.pkce === true;
+ const hostRequiresBasicAuth = requiresBasicAuthHeader(updatedReqBody);
+
+ // Enable PKCE when the user toggled it on, or when the provider is known to require it
+ if (userEnabledPkce || hostRequiresBasicAuth) {
+ console.log('will use PKCE');
+ config.pkce = true;
+ } else {
+ console.log('will not use PKCE');
+ }
+
+ // Some providers (e.g. Twitter/X, Spotify) require credentials as HTTP Basic auth
+ // header on the token exchange rather than POST body params
+ if (hostRequiresBasicAuth) {
+ const basicCredentials = Buffer.from(`${config.clientID}:${config.clientSecret}`).toString(
+ 'base64',
+ );
+ config.customHeaders = { Authorization: `Basic ${basicCredentials}` };
+ }
+ }
+
// Derive callback URL when missing
if (!config['callbackURL']) {
try {
diff --git a/packages/app/src/backend/routes/oauth/router.ts b/packages/app/src/backend/routes/oauth/router.ts
index 17a8edaa..8f3e783d 100644
--- a/packages/app/src/backend/routes/oauth/router.ts
+++ b/packages/app/src/backend/routes/oauth/router.ts
@@ -2,7 +2,6 @@ import { OAuthServicesRegistry } from '@src/shared/helpers/oauth/oauth-services.
import { mapStatusCodeToMessage } from '@src/shared/helpers/oauth/oauth.utils';
import { CredentialConnection } from '@src/shared/types/credentials.types';
import axios from 'axios';
-import crypto from 'crypto';
import express from 'express';
import rateLimit from 'express-rate-limit';
import passport from 'passport';
@@ -228,48 +227,9 @@ router.post(
router.get('/:provider', async (req, res, next) => {
try {
const scopes = req?.session?.scopes?.split(' ') || [];
- const strategyOptions: any = {
- state: { beep: `${crypto.randomUUID()}` }, // Let Passport handle state internally
- };
+ const strategyOptions: Record<string, unknown> = {};
- // Check if it's Twitter OAuth
- const isTwitter = [
- req.session?.oauth_info?.authorizationURL,
- req.session?.oauth_info?.tokenURL,
- ].some((url) => url?.includes('x.com') || url?.includes('twitter.com'));
-
- if (isTwitter && req.session.strategyType === 'oauth2') {
- // Generate state for CSRF protection
- const state = crypto.randomBytes(32).toString('base64url');
-
- // Generate code verifier for PKCE
- const codeVerifier = crypto.randomBytes(32).toString('base64url');
-
- // Generate code challenge using SHA-256
- const codeChallenge = await crypto.subtle
- .digest('SHA-256', new TextEncoder().encode(codeVerifier))
- .then((buffer) => Buffer.from(buffer).toString('base64url'));
-
- // Store verifier and state in session for callback validation
- req.session.code_verifier = codeVerifier;
- req.session.oauth_state = state;
-
- // Construct Twitter-specific OAuth URL
- const twitterAuthUrl = new URL('https://x.com/i/oauth2/authorize');
- const queryParams = new URLSearchParams({
- response_type: 'code',
- client_id: req.session.oauth_info.clientID,
- redirect_uri: req.session.oauth_info.oauth2CallbackURL,
- scope: scopes.join(' '),
- state: state,
- code_challenge: codeChallenge,
- code_challenge_method: 'S256',
- });
-
- return res.redirect(`${twitterAuthUrl.toString()}?${queryParams.toString()}`);
- }
- // For other providers, use passport authentication
- else if (req.session.strategyType === 'oauth2') {
+ if (req.session.strategyType === 'oauth2') {
strategyOptions.scope = scopes;
strategyOptions.accessType = 'offline';
strategyOptions.prompt = 'consent';
@@ -284,9 +244,6 @@ router.get('/:provider', async (req, res, next) => {
console.error('Authentication failed:', info.message);
return res.status(401).send('Authentication failed.');
}
- // Authentication success
- // Handle user as needed
- // For example, redirect or respond with a message
})(req, res, next);
} catch (error) {
console.error('Error during authentication process:', error?.message);
@@ -342,127 +299,57 @@ function getCallbackOrigin(req: express.Request): string {
}
router.get('/:provider/callback', async (req, res, next) => {
- const { state, code } = req.query;
-
- // Detect Twitter/X based on the authorization URL in session
- const isTwitterAuth =
- req.session?.oauth_info?.authorizationURL?.includes('x.com') ||
- req.session?.oauth_info?.authorizationURL?.includes('twitter.com');
-
- // Handle Twitter OAuth2
- if (isTwitterAuth && req.session.strategyType === 'oauth2') {
- // Verify state parameter
- if (!state || state !== req.session.oauth_state) {
- console.error('State parameter mismatch or missing');
- return res.status(401).send(`
- <script>
- window.opener.postMessage({
- type: 'error',
- data: { message: 'Invalid state parameter. Possible CSRF attack.' }
- }, '${getCallbackOrigin(req)}');
- window.close();
- </script>`);
- }
-
- // If state is valid, exchange the code for tokens using PKCE
+ passport.authenticate(req.session.strategyType, async (err: unknown, user: unknown, info: { message?: string }) => {
try {
- const tokenResponse = await axios.post(
- 'https://api.twitter.com/2/oauth2/token',
- new URLSearchParams({
- code: code as string,
- grant_type: 'authorization_code',
- client_id: req.session.oauth_info.clientID,
- redirect_uri: req.session.oauth_info.oauth2CallbackURL,
- code_verifier: req.session.code_verifier,
- }).toString(),
- {
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- Authorization: `Basic ${Buffer.from(
- `${req.session.oauth_info.clientID}:${req.session.oauth_info.clientSecret}`,
- ).toString('base64')}`,
- },
- },
- );
+ if (!req.session || !req.session.strategyType) {
+ throw new Error('Session or strategy type is not correctly set.');
+ }
+ if (err || !user) {
+ console.error(
+ `Authentication failed for provider ${req.session.strategyType}. Error:`,
+ err,
+ `Info:`,
+ info,
+ );
+
+ let errorDescription = 'Authentication failed.';
+ let errorCode = 'unknown_error';
+
+ if (err) {
+ const oauthErr = err as { message?: string; code?: string; oauthError?: { statusCode?: number } };
+ errorDescription = oauthErr.message || errorDescription;
+ errorCode = oauthErr.code || info?.message || errorCode;
+
+ if (oauthErr.oauthError && typeof oauthErr.oauthError.statusCode === 'number') {
+ const statusCode = oauthErr.oauthError.statusCode;
+ errorDescription = mapStatusCodeToMessage(statusCode);
+ errorCode = `HTTP ${statusCode}`;
+ }
+ } else if (info && typeof info.message === 'string') {
+ errorDescription = info.message;
+ }
- // Create user object with token response
- const user = {
- accessToken: tokenResponse.data.access_token,
- refreshToken: tokenResponse.data.refresh_token,
- params: {
- expires_in: tokenResponse.data.expires_in,
- },
- };
+ throw new Error(`${errorDescription} (Error code: ${errorCode})`);
+ }
- // Store tokens and complete authentication
await handleTokenStorage(req.session, user, req);
handleSuccessfulAuthentication(req.session.strategyType, res);
} catch (error) {
- console.error('Error exchanging code for tokens:', error?.message);
+ const errorObj = error as Error;
+ console.error('Error in callback route:', errorObj.message);
+ const errorMessage = `${errorObj.message || 'Unknown error'}.`;
const origin = getCallbackOrigin(req);
- res.send(`
+ const errorScript = `
<script>
window.opener.postMessage({
type: 'error',
- data: { message: 'Failed to exchange authorization code for tokens.' }
+ data: { message: '${errorMessage.replace(/'/g, "\\'")}' }
}, '${origin}');
window.close();
- </script>`);
+ </script>`;
+ res.send(errorScript);
}
- } else {
- // Existing passport authentication for other providers
- passport.authenticate(req.session.strategyType, async (err: any, user, info) => {
- try {
- if (!req.session || !req.session.strategyType) {
- throw new Error('Session or strategy type is not correctly set.');
- }
- if (err || !user) {
- console.error(
- `Authentication failed for provider ${req.session.strategyType}. Error:`,
- err,
- `Info:`,
- info,
- );
-
- let errorDescription = 'Authentication failed.'; // Default error message
- let errorCode = 'unknown_error';
-
- if (err) {
- errorDescription = err.message || errorDescription;
- errorCode = err?.code || info?.message || errorCode;
-
- // Handling detailed OAuth errors
- if (err.oauthError && typeof err.oauthError.statusCode === 'number') {
- const statusCode = err.oauthError.statusCode;
- errorDescription = mapStatusCodeToMessage(statusCode); // Use the function to map status code to message
- errorCode = `HTTP ${statusCode}`;
- }
- } else if (info && typeof info.message === 'string') {
- // Fallback to using the info message if available
- errorDescription = info.message;
- }
-
- throw new Error(`${errorDescription} (Error code: ${errorCode})`);
- }
-
- await handleTokenStorage(req.session, user, req);
- handleSuccessfulAuthentication(req.session.strategyType, res);
- } catch (error) {
- console.error('Error in callback route:', error?.message);
- const errorMessage = `${error.message || 'Unknown error'}.`;
- const origin = getCallbackOrigin(req);
- const errorScript = `
- <script>
- window.opener.postMessage({
- type: 'error',
- data: { message: '${errorMessage.replace(/'/g, "\\'")}' }
- }, '${origin}');
- window.close();
- </script>`;
- res.send(errorScript);
- }
- })(req, res, next);
- }
+ })(req, res, next);
});
// Helper function for expiration calculation (if needed)
diff --git a/packages/app/src/react/features/credentials/components/create-credentials.modal.tsx b/packages/app/src/react/features/credentials/components/create-credentials.modal.tsx
index 5bb18338..13592d13 100644
--- a/packages/app/src/react/features/credentials/components/create-credentials.modal.tsx
+++ b/packages/app/src/react/features/credentials/components/create-credentials.modal.tsx
@@ -25,6 +25,7 @@ import {
SelectTrigger,
SelectValue,
} from '@src/react/shared/components/ui/select';
+import { Switch } from '@src/react/shared/components/ui/switch';
import React, { useEffect, useMemo, useState } from 'react';
import { credentialsClient } from '../clients/credentials.client';
import credentialsSchema from '../credentials-schema.json';
@@ -38,10 +39,11 @@ import { PasswordInput } from './password-input';
interface CredentialField {
key: string;
label: string;
- type: 'string' | 'password' | 'number' | 'email' | 'url' | 'textarea';
+ type: 'string' | 'password' | 'number' | 'email' | 'url' | 'textarea' | 'toggle';
required: boolean;
placeholder?: string;
default?: string;
+ description?: string;
}
/**
@@ -340,7 +342,7 @@ export function CreateCredentialsModal({
if (selectedProvider) {
selectedProvider.fields.forEach((field) => {
- const value = credentials[field.key] || '';
+ const value = credentials[field.key] || field.default || '';
credentialsWithMetadata[field.key] = {
value,
sensitive: field.type === 'password',
@@ -549,7 +551,30 @@ export function CreateCredentialsModal({
{/* Dynamic Fields */}
{selectedProvider.fields.map((field) => (
<div key={field.key}>
- {field.type === 'textarea' ? (
+ {field.type === 'toggle' ? (
+ <div className="flex items-center justify-between rounded-md border border-gray-200 px-3 py-2.5">
+ <div className="flex flex-col gap-0.5">
+ <label
+ htmlFor={`toggle-${field.key}`}
+ className="text-sm font-medium text-gray-700 cursor-pointer"
+ >
+ {field.label}
+ </label>
+ {field.description && (
+ <span className="text-xs text-gray-500">{field.description}</span>
+ )}
+ </div>
+ <Switch
+ id={`toggle-${field.key}`}
+ className="data-[state=checked]:bg-blue-600 data-[state=unchecked]:bg-gray-200"
+ checked={credentials[field.key] === 'true'}
+ onCheckedChange={(checked) =>
+ handleCredentialChange(field.key, String(checked))
+ }
+ disabled={isProcessing || isResolvingVaultKeys}
+ />
+ </div>
+ ) : field.type === 'textarea' ? (
<TextArea
label={field.label}
required={field.required}
diff --git a/packages/app/src/react/features/credentials/credentials-schema.json b/packages/app/src/react/features/credentials/credentials-schema.json
index 20401b01..2f49d8b1 100644
--- a/packages/app/src/react/features/credentials/credentials-schema.json
+++ b/packages/app/src/react/features/credentials/credentials-schema.json
@@ -301,6 +301,14 @@
"type": "textarea",
"required": true,
"placeholder": "Space-separated list of scopes"
+ },
+ {
+ "key": "pkce",
+ "label": "Enable PKCE",
+ "type": "toggle",
+ "required": false,
+ "default": "false",
+ "description": "Proof Key for Code Exchange — required by some providers for enhanced security"
}
]
},
@@ -351,6 +359,14 @@
"type": "textarea",
"required": true,
"placeholder": "Space-separated list of scopes"
+ },
+ {
+ "key": "pkce",
+ "label": "Enable PKCE",
+ "type": "toggle",
+ "required": false,
+ "default": "false",
+ "description": "Proof Key for Code Exchange — required by some providers for enhanced security"
}
]
},