Skip to main content

Sign customers in with your own sign-in

What you will build​

You let a customer who is already signed in to your app get a Black customer session without a second sign-in. Your sign-in signs a short token that says who the customer is, which this guide calls an identity token. Your page or app sends it to Black, and Black answers with a session: an access token and a refresh token that the customer's page or app uses for the calls of the Client API.

You set three settings that tell Black how to check your tokens, build the token your sign-in signs, serve the keys that check it, make the call, and read each refusal. You also learn how to stop new sign-ins and how to keep the test and the live environments apart.

Before you start​

You need an app with a publishable key, in the environment where you sign customers in. Create the app with Register an app and its key with Create a publishable key. A publishable key is an identifier, not a secret: your page or app sends it in the Black-Publishable-Key header. A web app is called from a page whose origin is one of its allowed origins.

Your sign-in must be able to sign a token with a private key and to publish the matching public keys. Black never holds your private key.

An environment signs customers in with identity tokens only while its three settings are set. The settings are changed by the account's own API client, or by its vendor with Black-Account, as every other setting is: see Change account settings.

Steps​

  1. Set the three settings. Send a PATCH request to /v1/settings with a sign_in object that holds identity_keys_url, identity_issuer and identity_audience. identity_keys_url is the HTTPS address of your key set. identity_issuer and identity_audience are the two texts that your tokens carry in iss and aud, each at most 255 characters with no space. Send the three together: a request that sets one or two of them while the others are empty is refused with 422 and parameter_invalid, naming the one that is missing, as sign_in.identity_issuer. The response holds all the settings as they are after the change. See Change an environment's settings.

  2. Sign a token for the customer in your own sign-in. The next section lists what it carries. The token is made by your server, never by the customer's page or app.

  3. Send the token to Black. From the customer's page or app, send a POST request to /v1/client/auth/identity-token with the publishable key in Black-Publishable-Key and the token in token, at most 8192 characters. A page sends no Authorization header. See Exchange an identity token for a customer session.

  4. Read the session. The response is 201, holds Cache-Control: no-store and holds the session's id, its customer, its app, access_token and access_token_expires_at, refresh_token and expires_at. Both tokens are shown once: keep them where the customer's page or app keeps its session, and never in a log. The access token lasts an hour. The session ends at expires_at, by default 30 days after the sign-in, and the customer_session_days setting changes the length from 1 to 90 days. Use the access token for the calls of the Client API that read the customer's own data, and renew the pair before the hour ends with Refresh a customer session.

  5. Find the customer. Black matches the customer whose external_reference is the token's sub and signs that customer in. A deleted customer does not match. When no customer matches, Black creates one with that external_reference, and records customer.created with the actor type app, naming the app. Your own customer reference is therefore the key between your sign-in and Black: send the same sub for the same person every time, and a different one for a different person.

Build the token your sign-in signs​

Black takes a token only when every line below holds. The token is signed, so its first two parts are JSON objects and its third is the signature.

PartWhat it must hold
AlgorithmRS256 or ES256, the algorithm of the key that signs it. A token with alg set to none, to HS256 or to any other algorithm is refused.
kidThe ID of a key of your key set. A kid that the set does not hold is refused.
issExactly your identity_issuer.
audExactly your identity_audience, or a list that holds it.
subYour own reference for the customer, 1 to 255 characters. It is the customer's external_reference.
iat and expNumbers, in Unix seconds. exp is at most 10 minutes after iat, and iat is not in the future. Black allows 60 seconds of difference between your clock and its own.
jtiA text of 1 to 255 characters that is new for each token.
critAbsent. A token that carries a crit header is refused.

Sign a token for the moment the customer asks for a session, and send it at once. Black accepts each token once: it keeps a hash of the jti, never the token, until the token's end, and refuses a second token with the same jti, even when the rest differs.

Serve the key set​

Publish the public keys that check your tokens as a key set, a JSON document with a keys list, at the address you gave in identity_keys_url.

  • Address. The address is HTTPS and public. Black fetches it from the internet, so an address that resolves to a private or local network address is refused, and so is any redirect. It answers 200 within 3 seconds, with a body of at most 64 KB.
  • Keys. An entry is a key for Black when it is an RSA key (RS256) or a P-256 key (ES256) for signing, with a kid. Black passes over any other entry. When two entries share a kid, Black keeps the first. The set holds at least one such key.
  • Coordinates. Serve each coordinate of a P-256 key, x and y, at its full 32 bytes, leading zero bytes included. Black accepts a shorter coordinate by padding it on the left with zero bytes, which changes nothing the key means. An entry with a longer coordinate, an empty one or one that is not base64url is passed over like any other entry that is not a key for Black. When the set holds no usable key, the exchange answers 503 with the code identity_keys_unavailable: fix the set rather than retrying.
  • Keeping. Black keeps the set for the max-age of the answer's Cache-Control, held between 5 minutes and 24 hours, and for 5 minutes when the answer names none. When a token names a kid that the kept set does not hold, Black fetches the set again, at most once a minute for each environment and address.

To rotate a key, add the new key to the set first, wait at least the set's max-age, then sign with it and remove the old key once its tokens have ended. A key that you remove stops checking tokens when Black next fetches the set.

Handle each refusal​

Each refusal is an error with a code: see Errors for every code. For the call above:

  • 403 sign_in_method_disabled. The environment does not name all three settings, so the method is off. Set identity_keys_url, identity_issuer and identity_audience, then send the same call again.
  • 401 identity_token_signature_invalid. The token is not signed with RS256 or ES256, names a kid that your key set does not hold, or its signature does not match the key. Sign it with a key of the set, and publish that key before you use it.
  • 401 identity_token_expired. The token's exp has passed, beyond the 60 seconds Black allows. Sign a new token for the customer and send it at once. Check your server's clock when it happens for every token.
  • 401 identity_token_audience_invalid. The token's aud is not your identity_audience. Sign it for that audience: see Use a different audience for each environment.
  • 401 identity_token_invalid. The token is malformed, names another issuer, lacks sub, jti, iat or exp, is not valid yet, lasts more than 10 minutes, or carries a crit header. Correct the token against the table above, and sign a new one.
  • 401 identity_token_replayed. Black already took a token with this jti. Sign a new token with a new jti. If your page sent the call twice, use the session the first call made.
  • 503 identity_keys_unavailable. Black could not fetch your key set with certainty: the address did not answer in time, answered with a redirect or an error, or held no usable key. Black made no session and used no token, so the same token may be sent again once your key set answers.
  • 409 blocked. The customer is blocked. No session is made. Unblock the customer first: see Block a customer.
  • 403 origin_not_allowed and 401 publishable_key_invalid. The request does not come from one of the app's allowed origins, or the key is missing, unknown or revoked. Send the call from the app's page with the app's current key.
  • 429 rate_limited. Black counts the calls that sign a customer in, 30 a minute for one address and one key. Wait for the seconds Retry-After gives, then send the call again.

Use a different audience for each environment​

Give the test environment and the live environment different values for identity_audience, and sign a test token with the test audience only. A token made for one environment then fails in the other with identity_token_audience_invalid, so a token from a test sign-in cannot sign anyone in to the live environment. Give each environment its own key set address as well when your test sign-in uses its own keys.

Stop signing customers in with it​

To stop new sign-ins, send a PATCH request to /v1/settings with identity_keys_url, identity_issuer and identity_audience set to null, inside sign_in, all three together. The method is off from then on, and the next exchange answers 403 with sign_in_method_disabled.

The sessions that customers already hold go on until they end or are refreshed. To end them, send a POST request to /v1/customers/{id}/end-sessions: see End a customer's sessions.

A runnable sample​

A key set must be served from a public HTTPS address, and a sample cannot give one, so this sample does not make the exchange that signs a customer in. It registers an app and its publishable key, sends the exchange while the three settings are empty and reads the refusal sign_in_method_disabled, sets the three settings, and clears them again. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, reads the app's ID and the publishable key from the responses, and needs sed. A test environment takes five writes a second, so the sample waits a second after each write.

TOKEN=PLACEHOLDER_ACCESS_TOKEN

APP=$(curl --silent --request POST https://api.black.example/v1/apps \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Identity Demo","kind":"web","allowed_origins":["https://app.example.com"]}' \
| sed -n 's/.*"id":"\(app_[0-9a-z]*\)".*/\1/p')

KEY=$(curl --silent --request POST "https://api.black.example/v1/apps/$APP/publishable-keys" \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"\(pk_test_[0-9A-Za-z]*\)".*/\1/p')

curl --silent --request POST https://api.black.example/v1/client/auth/identity-token \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data '{"token":"PLACEHOLDER_IDENTITY_TOKEN"}'

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"sign_in":{"identity_keys_url":"https://auth.example.com/.well-known/jwks.json","identity_issuer":"https://auth.example.com","identity_audience":"black-test"}}'

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"sign_in":{"identity_keys_url":null,"identity_issuer":null,"identity_audience":null}}'

The first two requests answer 201: the app, then its key. The exchange answers 403 with the code sign_in_method_disabled, because the environment names no key set, issuer and audience. The first settings change answers 200, and its sign_in holds the three values. The last answers 200 and holds them as null again, which switches the method off. In your own run, send the token that your sign-in signs in place of PLACEHOLDER_IDENTITY_TOKEN, and keep the three settings set.

What to do next​