Skip to main content

Token vault

An agent often needs to call another service for the user, for example to read the user's GitHub repositories or Google Calendar. With the token vault, Casdoor keeps the user's tokens at that service, refreshes them when they expire, and gives a current access token only to the agents the user allowed. The agent never sees the user's password or refresh token.

How it works​

  1. The agent signs the user in with Casdoor and holds the user's Casdoor access token, for example from the authorization code grant or from CIBA.
  2. The agent exchanges that token for the user's token at the service, with token exchange.
  3. If the user hasn't connected the account yet, or hasn't allowed this agent, Casdoor answers with a link. The agent shows the link to the user.
  4. The user opens the link, connects the account at the service and allows the agent. The agent then exchanges again and gets the token.

Set it up​

  1. In the OAuth provider of the service, for example GitHub, turn on Keep tokens for agents. In Scopes for agents, enter the scopes that agents need, for example repo for GitHub. Leave it empty to use the sign-in scopes.

    For Google, Casdoor asks for offline access when a user connects the account, so that Google issues a refresh token.

  2. In the application of the agent, on the OIDC/OAuth tab, select the provider in Connected account providers, and turn on Token Exchange in Grant types.

The provider doesn't have to be a sign-in provider of the application. Users who sign in with the provider, or link it on their account page, also have their tokens kept.

Exchange the token​

curl https://<casdoor-host>/api/login/oauth/access_token \
-d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
-d client_id=ClientId -d client_secret=ClientSecret \
-d subject_token=USER_ACCESS_TOKEN \
-d subject_token_type=urn:ietf:params:oauth:token-type:access_token \
-d requested_token_type=urn:casdoor:params:oauth:token-type:federated-access-token \
-d connection=github

connection is the name of the provider. The response carries the user's access token at the service:

{
"access_token": "gho_...",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": 28000,
"scope": "repo"
}

When the token expires within a minute, Casdoor refreshes it with the refresh token first. Don't store the token: exchange again when you need it.

ErrorWhen
consent_requiredThe user hasn't connected the account, or hasn't allowed this application. error_description contains the link for the user: https://<casdoor-host>/vault-connect/<organization>/<application>/<provider>.
unauthorized_clientThe provider isn't in Connected account providers of the application, or the agent is disabled or expired.
invalid_grantThe service refused to refresh the token, for example because the user revoked it there. error_description contains the link to connect again.

What users see​

The link opens a page that names the agent, the service and the scopes. The user signs in to Casdoor if needed, connects the account at the service, and Casdoor lets the agent use it. If the account is connected already, one click allows the agent.

On their account page, under Connected accounts, users see each connected account, which agents may use it and when an agent last used it. They can stop one agent, or disconnect the account, which deletes the tokens. Revoking an agent under AI apps also stops it from using all connected accounts. See AI connections.

Only the user can allow an agent, in the browser. Casdoor refuses requests that carry an access token, an access key or client credentials, so that an agent can't allow itself with the user's token.