Source code for mindroot.coreplugins.admin.oauth_callback_router
"""OAuth callback router for MCP servers.This router contains public routes that do not require authentication,as external OAuth providers need to be able to redirect to these endpoints."""fromfastapiimportAPIRouter,Requestfromfastapi.responsesimportHTMLResponsefromlib.route_decoratorsimportpublic_route# Create router without dependencies - routes will be publicrouter=APIRouter()
[docs]@router.get("/mcp_oauth_cb")@public_route()asyncdefmcp_oauth_callback(request:Request):"""Handle OAuth callback for MCP servers. This endpoint must be publicly accessible as external OAuth providers will redirect to it without any authentication. """try:# Get query parameterscode=request.query_params.get('code')state=request.query_params.get('state')error=request.query_params.get('error')iferror:# OAuth error occurred - avoid f-string issueserror_html=("<html><body>""<h2>OAuth Authorization Failed</h2>"f"<p>Error: {error}</p>""<p>You can close this window.</p>""<script>window.close();</script>""</body></html>")returnHTMLResponse(error_html)ifcode:# Success - show completion pagestate_value=stateor""# Build HTML without f-string to avoid escaping issuessuccess_html=("<html><body>""<h2>OAuth Authorization Successful</h2>""<p>Authorization code received. You can close this window.</p>""<script>""if (window.opener) {""window.opener.postMessage({""type: 'oauth_callback',"f"code: '{code}',"f"state: '{state_value}'""}, '*');""}""setTimeout(() => window.close(), 2000);""</script>""</body></html>")returnHTMLResponse(success_html)# No code or error - invalid callbackinvalid_html=("<html><body>""<h2>Invalid OAuth Callback</h2>""<p>Missing authorization code or error parameter.</p>""<p>You can close this window.</p>""<script>window.close();</script>""</body></html>")returnHTMLResponse(invalid_html)exceptExceptionase:# Error handling - escape special characterserror_message=str(e).replace('"','"').replace("'","'")error_html=("<html><body>""<h2>OAuth Callback Error</h2>"f"<p>An error occurred processing the OAuth callback: {error_message}</p>""<p>You can close this window.</p>""<script>window.close();</script>""</body></html>")returnHTMLResponse(error_html)