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
-
Set the three settings. Send a
PATCHrequest to/v1/settingswith asign_inobject that holdsidentity_keys_url,identity_issuerandidentity_audience.identity_keys_urlis the HTTPS address of your key set.identity_issuerandidentity_audienceare the two texts that your tokens carry inissandaud, 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 with422andparameter_invalid, naming the one that is missing, assign_in.identity_issuer. The response holds all the settings as they are after the change. See Change an environment's settings. -
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.
-
Send the token to Black. From the customer's page or app, send a
POSTrequest to/v1/client/auth/identity-tokenwith the publishable key inBlack-Publishable-Keyand the token intoken, at most 8192 characters. A page sends noAuthorizationheader. See Exchange an identity token for a customer session. -
Read the session. The response is
201, holdsCache-Control: no-storeand holds the session'sid, itscustomer, itsapp,access_tokenandaccess_token_expires_at,refresh_tokenandexpires_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 atexpires_at, by default 30 days after the sign-in, and thecustomer_session_dayssetting 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. -
Find the customer. Black matches the customer whose
external_referenceis the token'ssuband signs that customer in. A deleted customer does not match. When no customer matches, Black creates one with thatexternal_reference, and recordscustomer.createdwith the actor typeapp, naming the app. Your own customer reference is therefore the key between your sign-in and Black: send the samesubfor 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.
| Part | What it must hold |
|---|---|
| Algorithm | RS256 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. |
kid | The ID of a key of your key set. A kid that the set does not hold is refused. |
iss | Exactly your identity_issuer. |
aud | Exactly your identity_audience, or a list that holds it. |
sub | Your own reference for the customer, 1 to 255 characters. It is the customer's external_reference. |
iat and exp | Numbers, 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. |
jti | A text of 1 to 255 characters that is new for each token. |
crit | Absent. 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
200within 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 akid, Black keeps the first. The set holds at least one such key. - Coordinates. Serve each coordinate of a P-256 key,
xandy, 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 answers503with the codeidentity_keys_unavailable: fix the set rather than retrying. - Keeping. Black keeps the set for the
max-ageof the answer'sCache-Control, held between 5 minutes and 24 hours, and for 5 minutes when the answer names none. When a token names akidthat 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:
403sign_in_method_disabled. The environment does not name all three settings, so the method is off. Setidentity_keys_url,identity_issuerandidentity_audience, then send the same call again.401identity_token_signature_invalid. The token is not signed with RS256 or ES256, names akidthat 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.401identity_token_expired. The token'sexphas 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.401identity_token_audience_invalid. The token'saudis not youridentity_audience. Sign it for that audience: see Use a different audience for each environment.401identity_token_invalid. The token is malformed, names another issuer, lackssub,jti,iatorexp, is not valid yet, lasts more than 10 minutes, or carries acritheader. Correct the token against the table above, and sign a new one.401identity_token_replayed. Black already took a token with thisjti. Sign a new token with a newjti. If your page sent the call twice, use the session the first call made.503identity_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.409blocked. The customer is blocked. No session is made. Unblock the customer first: see Block a customer.403origin_not_allowedand401publishable_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.429rate_limited. Black counts the calls that sign a customer in, 30 a minute for one address and one key. Wait for the secondsRetry-Aftergives, 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
- Read the call's fields and answers in Exchange an identity token for a customer session.
- Read how your server signs a customer in with a one-time token instead in Exchange a sign-in token for a customer session.
- Read how to change settings, and which client may change which, in Change account settings.
- Read how a customer's page reads their own data in Read the customer's own data.
- Read every error code in Errors.