UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.76 kB
import{o as getPluginRuntimeGatewayRequestScope}from"./gateway-request-scope-7deHmsb_.mjs";import{o as getGatewayPluginMetadataSnapshot}from"./plugin-metadata-lifecycle-Bdt91XXL.mjs";import{Wi as validateMcpAuthLoginParams}from"./src-BDZMFmKa.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{n as isTransientNetworkError}from"./retryable-network-errors-DtxRNm1C.mjs";import{t as WizardSession}from"./session-CN7tnCR5.mjs";import{i as createProviderBrowserAuthSession,n as ProviderBrowserSignInError,r as ProviderBrowserSignInUnavailableError}from"./provider-browser-auth-r1C9K1AJ.mjs";import{t as assertValidParams}from"./validation-CM41YclJ.mjs";import{i as operatorMcpOAuthIdentity,t as mcpOAuthCliLoginSteps}from"./mcp-oauth-identity-DSHLyzOx.mjs";import{f as startMcpOAuthAuthorization,o as completeOAuthCallback,p as McpOAuthCredentialsRetainedError,t as cancelMcpOAuthAuthorization}from"./mcp-oauth-Df1IQZXw.mjs";import{i as resolveOperatorMcpOAuthConnection}from"./mcp-auth-status-B1hcrK-U.mjs";import{a as startGatewayWizard}from"./setup-admission-7MXhXxj9.mjs";import{isDeepStrictEqual}from"node:util"; //!src/gateway/server-methods/mcp-auth-login.ts const mcpAuthLoginHandlers={"mcp.authLogin":async e=>{const{params:t,respond:n,context:r,client:i}=e;if(!assertValidParams(t,validateMcpAuthLoginParams,`mcp.authLogin`,n))return;const reject=e=>n(false,void 0,errorShape(ErrorCodes.INVALID_REQUEST,e));if(!i||!i.connect.scopes?.includes(`operator.admin`)){reject(`Sign-in requires an administrator connection.`);return}const a=getGatewayPluginMetadataSnapshot();const resolveConnection=()=>resolveOperatorMcpOAuthConnection({config:r.getRuntimeConfig(),metadata:a,serverName:t.serverName});const o=resolveConnection();const s=r.getGatewayMethodRegistry?.();const isOperatorOwned=()=>getGatewayPluginMetadataSnapshot()===a&&getPluginRuntimeGatewayRequestScope()?.pluginRegistry===s?.pluginRegistry;if(!o||!isOperatorOwned()){reject(`This MCP server cannot use operator browser sign-in. Check its existing account settings.`);return}const{config:c}=o;const l=operatorMcpOAuthIdentity(t.serverName,c.url,r.getRuntimeConfig());const u=mcpOAuthCliLoginSteps(l);const d=u?`MCP server`:`plugin`;if(!i.browserOrigin){reject(u?`Open Settings on this Gateway to sign in, or sign in from its terminal: ${u}.`:`Open Settings on this Gateway to sign in.`);return}const f=structuredClone(o.server);const assertServerCurrent=()=>{if(i.invalidated||!i.connect.scopes?.includes(`operator.admin`))throw new Error(`Sign-in authority is no longer active.`);if(r.getGatewayMethodRegistry?.()!==s||!isDeepStrictEqual(f,resolveConnection()?.server)||!isOperatorOwned())throw new Error(`This MCP server changed. Close this dialog and review its settings before signing in again.`)};await startGatewayWizard({request:e,sessionId:t.sessionId,connectionBound:true,assertCurrent:assertServerCurrent,createSession:e=>new WizardSession(async(n,a,o)=>{let s;let f=false;const p=createProviderBrowserAuthSession({signal:AbortSignal.any([a,...i.connectionSignal?[i.connectionSignal]:[]]),browserOrigin:i.browserOrigin,authPolicy:i.authPolicy,openUrl:async e=>{m.assertCurrent();await n.openUrl?.(e);m.assertCurrent()}});const m={signal:p.signal,assertCurrent:()=>{a.throwIfAborted();e();p.assertCurrent()},onAuthorizationPublished:e=>{s=e},beforeTokensSaved:()=>o.lockCancellation(),onTokensSaved:()=>{f=true}};const failure=()=>/* @__PURE__ */ new Error(f?`Authentication saved, but sign-in cleanup did not finish. Close this dialog and check the ${d} before trying again.`:u?`Sign-in did not finish. Check the MCP server settings and try again, or sign in from this Gateway's terminal: ${u}.`:`Sign-in did not finish. Check the plugin settings, then select Connect to try again.`);try{try{const e=await p.authorizePrepared({timeoutMs:6e5,prepare:async e=>{const t=await startMcpOAuthAuthorization(l,c,{redirectUrl:e,login:m});return t.status===`authorized`?{result:`authorized`}:t}});if(e!==`authorized`&&await completeOAuthCallback(l,c,e,m)!==`authorized`)throw new ProviderBrowserSignInError(`expired`)}finally{p.close();if(s)await cancelMcpOAuthAuthorization(l,s)}}catch(e){if(f)throw failure();if(e instanceof ProviderBrowserSignInUnavailableError)throw e;const{AuthorizationServerMismatchError:n,IssuerMismatchError:i,OAuthError:a,RegistrationRejectedError:o}=await import(`@modelcontextprotocol/client`);let s=`unexpected`;let c;if(e instanceof i||e instanceof n){s=`provider-mismatch`;c=`OpenClaw could not verify the sign-in provider. Close this dialog, review the connection settings, and try again.`}else if(e instanceof ProviderBrowserSignInError){s=e.reason;c=e.reason===`expired`?`This sign-in attempt expired. Close this dialog and start sign-in again.`:`The provider did not approve sign-in. Close this dialog and try again.`}else if(e instanceof McpOAuthCredentialsRetainedError){s=`credentials-retained`;c=`The sign-in service rejected this attempt. Existing credentials were not changed. Close this dialog, review the connection settings, and try again.`}else if(e instanceof o){s=`registration-rejected`;c=`The sign-in service rejected this connection's settings. Close this dialog and review the connection settings before trying again.`}else if(e instanceof a){s=`provider-rejected`;c=`The sign-in service rejected this attempt. Close this dialog and try again.`}else if(isTransientNetworkError(e)){s=`network`;c=`OpenClaw could not reach the sign-in service. Check this Gateway's network connection, then close this dialog and try again.`}r.logGateway.warn(`MCP sign-in failed for server "${t.serverName}": ${s}`);throw c?new Error(c):failure()}})})}};export{mcpAuthLoginHandlers};