background image

OAuth

Let people sign in to your app or AI assistant: Normal and MCP client applications, PKCE and the endpoints.

OAuth

Introduction

An OAuth application lets people sign in to your app, or to an AI assistant, with their own Stellar DataStore account. The app never sees their password: it gets an access token that works on one project, limited to the role you give the application.

Stellar DataStore has two kinds of OAuth application. You choose the type when you add the application under Applications & API keys → OAuth applications:

Normal (OAuth 2.0) MCP client (OAuth 2.1)
Use it for your own websites, apps and services AI assistants that connect to the MCP server: Claude, VS Code, MCP Inspector, …
Client secret required on every token request optional: the app may be a public client that only sends its client ID
PKCE optional (S256 or plain), checked when sent required, S256 only
Redirect URLs the registered URL; extra query parameters are allowed when the registered URL has none exactly the registered URL, query included
resource optional sent by MCP clients; the token is then also valid for that MCP server

For both types, an http://localhost, http://127.0.0.1 or http://[::1] redirect URL accepts any port, since desktop apps listen on whichever port is free. Scheme, host and path must always match.

You can change an application's type later by editing it. Its client ID, secret and role stay the same.

Normal applications (OAuth 2.0)

The Getting Started guide walks through the whole flow with examples in every language: send the user to the sign-in page, exchange the code for tokens, and refresh them. In short:

  1. Send the browser to https://stellards.io/oauth?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE.
  2. The user signs in and is sent back to REDIRECT_URI?code=CODE&state=STATE. Check that state is the value you sent.
  3. Post grant_type=authorization_code, client_id, client_secret, redirect_uri and code to https://api.stellards.io/v1/oauth/token.

state is optional but protects against forged sign-ins; send a random value and check it when the user comes back. Apps that run in the browser or on a user's device can't keep a secret, so they should add PKCE as described below, and keep the client secret on a server.

MCP client applications

You use an MCP client application to connect an AI assistant to the hosted MCP server at https://stellards.io/mcp. You normally don't write any OAuth code for it: the assistant finds Stellar DataStore by itself and runs the sign-in. This section explains what happens, and what to send if you write your own client.

Set one up

  1. Open Applications & API keys, go to OAuth applications and click Add application.
  2. Choose MCP client (OAuth 2.1).
  3. Enter the redirect URL of every assistant that will use it, separated by ;. Common ones:
Assistant Redirect URL
Claude Code started with --callback-port 8765 http://localhost:8765/callback
Claude (claude.ai and Claude Desktop custom connectors) https://claude.ai/api/mcp/auth_callback
MCP Inspector http://localhost:6274/oauth/callback and http://localhost:6274/oauth/callback/debug
  1. Pick the role the assistant may use. The assistant can do everything that role allows, for every person who signs in, so give it only the permissions it needs.
  2. Create the application and copy the client ID. Give the client ID to the people who connect; the secret is not needed.

Stellar DataStore doesn't support dynamic client registration, so a client must let you enter the client ID.

How an assistant signs in

1. Assistant → POST https://stellards.io/mcp without a token
               ← 401, pointing at the protected resource metadata
2. Assistant → GET https://stellards.io/.well-known/oauth-protected-resource/mcp
               ← resource https://stellards.io/mcp, authorization server https://stellards.io
3. Assistant → GET https://stellards.io/.well-known/oauth-authorization-server
               ← the sign-in, token and introspection endpoints
4. Browser   → https://stellards.io/oauth?…&code_challenge=…&resource=https://stellards.io/mcp
               the user signs in; the code goes to the assistant's redirect URL
5. Assistant → POST https://stellards.io/oauth/token with the code and code_verifier
               ← access token and refresh token
6. Assistant → POST https://stellards.io/mcp with the access token
               the MCP server checks the token, then calls https://api.stellards.io with it

The access token names both the Stellar DataStore API and the MCP server (https://stellards.io/mcp) as its audience, so the MCP server accepts it and can pass it on to the API.

Write your own client

If you write the client yourself, these are the requests. Every step works the same for a Normal application that wants PKCE; leave out resource when you don't call an MCP server.

1. Create a code verifier and challenge

Each sign-in needs a new random code_verifier of 43 to 128 characters. The code_challenge is its SHA-256 hash, base64url-encoded without padding:

CODE_VERIFIER=$(openssl rand -base64 48 | tr '+/' '-_' | tr -d '=\r\n')
CODE_CHALLENGE=$(printf '%s' "$CODE_VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=\r\n')
import { randomBytes, createHash } from "node:crypto";

const codeVerifier = randomBytes(48).toString("base64url");
const codeChallenge = createHash("sha256").update(codeVerifier).digest("base64url");
const state = randomBytes(16).toString("base64url");
import base64, hashlib, secrets

code_verifier = secrets.token_urlsafe(48)
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).rstrip(b"=").decode()
state = secrets.token_urlsafe(16)

Keep the verifier until the token request; never send it to the sign-in page.

2. Send the user to the sign-in page

https://stellards.io/oauth?response_type=code
    &client_id=CLIENT_ID
    &redirect_uri=http%3A%2F%2Flocalhost%3A8765%2Fcallback
    &state=STATE
    &code_challenge=CODE_CHALLENGE
    &code_challenge_method=S256
    &resource=https%3A%2F%2Fstellards.io%2Fmcp
Parameter
response_type always code
client_id the application's client ID
redirect_uri one of the registered redirect URLs, exactly
state a random value you check when the user comes back
code_challenge the challenge from step 1
code_challenge_method S256
resource the MCP server's address: https://stellards.io/mcp

After signing in, the browser goes to REDIRECT_URI?code=CODE&state=STATE. If state isn't the value you sent, stop. The code can be used once.

3. Exchange the code for tokens

curl -X POST 'https://stellards.io/oauth/token' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode 'client_id=CLIENT_ID' \
  --data-urlencode 'redirect_uri=http://localhost:8765/callback' \
  --data-urlencode 'code=AUTHORIZATION_CODE' \
  --data-urlencode "code_verifier=$CODE_VERIFIER" \
  --data-urlencode 'resource=https://stellards.io/mcp'
const response = await fetch("https://stellards.io/oauth/token", {
  method: "POST",
  headers: { Accept: "application/json" },
  body: new URLSearchParams({
    grant_type: "authorization_code",
    client_id: "CLIENT_ID",
    redirect_uri: "http://localhost:8765/callback",
    code: authorizationCode,
    code_verifier: codeVerifier,
    resource: "https://stellards.io/mcp",
  }),
});
const tokens = await response.json();
import requests

response = requests.post(
    "https://stellards.io/oauth/token",
    headers={"Accept": "application/json"},
    data={
        "grant_type": "authorization_code",
        "client_id": "CLIENT_ID",
        "redirect_uri": "http://localhost:8765/callback",
        "code": authorization_code,
        "code_verifier": code_verifier,
        "resource": "https://stellards.io/mcp",
    },
)
tokens = response.json()

redirect_uri must be the one you used on the sign-in page, port included. resource may be left out here; when you send it, it must be the same as on the sign-in page. A client that does have the secret can add client_secret, or send ID and secret as HTTP Basic credentials in the Authorization header.

The response is the same as for a Normal application:

{
  "access_token": "eyJhbGciOi...",
  "refresh_token": "eyJhbGciOi...",
  "expires_in": 1770
}

4. Refresh the access token

An access token is valid for 30 minutes and a refresh token for 30 days. Post grant_type=refresh_token, client_id and refresh_token to the same endpoint for a new pair; the new access token is valid for the same MCP server as the first one.

curl -X POST 'https://stellards.io/oauth/token' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=refresh_token' \
  --data-urlencode 'client_id=CLIENT_ID' \
  --data-urlencode 'refresh_token=REFRESH_TOKEN'

Every refresh returns a new refresh token; store it and throw the old one away. Using an old refresh token a second time ends the sign-in, and the user has to sign in again.

Endpoints

Endpoint Purpose
GET https://stellards.io/.well-known/oauth-authorization-server Metadata (RFC 8414): the endpoints and what they support.
GET https://stellards.io/oauth The sign-in page (authorization endpoint).
POST https://stellards.io/oauth/token Token endpoint. https://api.stellards.io/v1/oauth/token accepts the same requests.
POST https://stellards.io/oauth/introspect Token introspection (RFC 7662), for servers that receive tokens, such as the MCP server.
POST https://api.stellards.io/v1/oauth/revoke Ends a sign-in. JSON body with refresh_token, client_id and client_secret, so only apps that hold the secret can call it.

Token introspection

A server that receives access tokens can ask whether a token is valid. Post it as token in a form-encoded body; no client credentials are needed:

curl -X POST 'https://stellards.io/oauth/introspect' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'token=ACCESS_TOKEN'
{
  "active": true,
  "token_type": "Bearer",
  "sub": "USER_ID",
  "client_id": "CLIENT_ID",
  "project_id": "PROJECT_ID",
  "aud": ["https://api.stellards.io/", "https://stellards.io/mcp"],
  "iss": "https://stellards.io/",
  "exp": 1794931200,
  "iat": 1794929400
}

active is false for anything that isn't an unexpired OAuth access token of an application that still exists. A server that accepts tokens for itself should also check that aud contains its own address.

Errors

Error Cause
client secret is required for this application A Normal application left out its secret. Only MCP client applications may.
MCP applications must use PKCE The sign-in had no code_challenge, or used plain instead of S256.
invalid_grant: the code_verifier does not match the code_challenge The verifier isn't the one the challenge was made from. Use the verifier of this sign-in.
invalid_grant: the authorization code has already been used Codes work once. Start a new sign-in.
invalid_target The resource isn't https://stellards.io/mcp, or differs from the one on the sign-in page.
The sign-in page says the redirect URL is not allowed The redirect_uri isn't registered on the application exactly as sent.