Login strategies

Let identities sign in with SAML single sign-on, and users with Google, GitHub, Facebook, Okta, Auth0 or any custom OAuth 2.0 provider.

Strategies add external login methods:

TypeForProviders
samlIdentitiesAny SAML 2.0 identity provider
oauthUsersgoogle, github, facebook, okta, auth0 or custom

Manage strategies in Access Management → Strategies or with /passport/strategy (passport:strategy:* actions).

The strategies page in Access Management

OAuth for users

Create a strategy

For the built-in providers, provide your OAuth application's credentials:

Terminal
curl -X POST https://api.example.com/api/passport/strategy \
  -H "Authorization: IDENTITY <token>" -H "Content-Type: application/json" \
  -d '{
    "type": "oauth",
    "icon": "login",
    "options": {"idp": "google", "client_id": "<client id>", "client_secret": "<client secret>"}
  }'

okta and auth0 also need domain, the domain of your tenant.

In your provider's console, register this redirect URI:

Text
<public-url>/passport/user/strategy/<strategyId>/complete

Login flow

The login is completed by the API, so your client only needs the strategy id:

  1. Get the provider URL

    Terminal
    curl https://api.example.com/api/passport/user/strategy/<strategyId>/url
    JSON
    {"url": "https://accounts.google.com/o/oauth2/v2/auth?client_id=...&state=...", "state": "f3b1..."}
  2. Wait for the result and send the user to the provider

    Start a login request with the state, then open url in a popup or a new tab. The login request stays open until the user finishes, for up to 60 seconds:

    JavaScript
    const {url, state} = await fetch(`${API}/passport/user/strategy/${strategyId}/url`).then(r => r.json());
    
    const pending = fetch(`${API}/passport/login`, {
      method: "POST",
      headers: {"Content-Type": "application/json"},
      body: JSON.stringify({state})
    }).then(r => r.json());
    
    window.open(url, "_blank");
    const {token} = await pending;
  3. Use the token

    The response is a normal user login: {"scheme": "USER", "token": "..."}.

The first login creates the user. Its username is taken from the provider's response (upn, id, name_id or email), and its signUpStrategy records the strategy.

Custom OAuth providers

Describe each step of the OAuth flow:

JSON
{
  "type": "oauth",
  "name": "my-sso",
  "title": "Company SSO",
  "options": {
    "idp": "custom",
    "code": {
      "base_url": "https://sso.example.com/oauth/authorize",
      "params": {"client_id": "<client id>", "response_type": "code", "scope": "openid email"},
      "method": "get"
    },
    "access_token": {
      "base_url": "https://sso.example.com/oauth/token",
      "params": {"client_id": "<client id>", "client_secret": "<client secret>", "grant_type": "authorization_code"},
      "method": "post"
    },
    "identifier": {
      "base_url": "https://sso.example.com/oauth/userinfo",
      "method": "get"
    },
    "attributes": {"email": "email", "name": "name"}
  }
}
FieldDescription
codeWhere users authorize. Spica adds redirect_uri and state.
access_tokenWhere the code is exchanged for an access token.
identifierWhere user information is fetched with the access token.
attributesMaps user attributes to top-level fields of the user information response. They are stored, encrypted, on the user.

Each step accepts base_url, params, headers and method (get or post).

SAML for identities

SAML lets your team sign in to the panel with your company's identity provider.

JSON
{
  "type": "saml",
  "name": "okta-saml",
  "title": "Sign in with Okta",
  "icon": "login",
  "options": {
    "ip": {
      "login_url": "https://example.okta.com/app/.../sso/saml",
      "logout_url": "https://example.okta.com/login/signout",
      "certificate": "-----BEGIN CERTIFICATE-----..."
    }
  }
}
FieldDescription
options.ip.login_urlRequired. The identity provider's SSO URL.
options.ip.certificateRequired. The identity provider's signing certificate.
options.ip.logout_urlOptional logout URL.
options.spOptional service provider certificate and private_key. When left out, Spica creates a self-signed certificate valid for --passport-saml-certificate-ttl seconds.

Configure your identity provider with Spica's service provider metadata:

Text
<public-url>/passport/identity/strategy/<name>/metadata

and the assertion consumer URL:

Text
<public-url>/passport/identity/strategy/<strategyId>/complete

The panel shows SAML strategies on its login page. Programmatically, the flow mirrors OAuth: GET /passport/identity/strategy/<id>/url, then POST /passport/identify with {"state": ...}.