Sign customers in with Google
What you will build
You let a customer sign in to your app with their Google account. Your page gets an ID token from Google's own sign-in, sends it to Black, and receives a customer session: an access token and a refresh token that the page or app uses for the calls of the Client API.
You turn the method on, send the token, learn what Black checks and when it counts the customer's email as proved, and read each refusal. Black holds no Google client and no Google secret: the client ID is yours.
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, and a web page is called from one of the app's allowed origins.
You need a Google client ID for your app, made in Google's own console, and Google's sign-in on your page, which gives the customer's ID token for that client ID.
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
Turn the method on
- Turn on Google. Send a
PATCHrequest to/v1/settingswith{"sign_in":{"google":true,"google_client_id":"<your client ID>"}}. The response holds all the settings as they are after the change. See Change an environment's settings. Turning Google on without a client ID is refused with422andparameter_invalid, namingsign_in.google_client_id. Turning Google off keeps the client ID.
While Google is off, or on with no client ID, the sign-in answers 403 with sign_in_method_disabled, before Black reads the request. Turning the method off does not end the sessions it already made.
Send Google's ID token
-
Get the token. The customer signs in with Google on your page. Google gives your page an ID token for your client ID.
-
Send it to Black. From the customer's page or app, send a
POSTrequest to/v1/client/auth/googlewith the publishable key inBlack-Publishable-Keyand the ID token astoken, at most 8192 characters. A page sends noAuthorizationheader. See Sign a customer in with a Google ID token. -
Read the session. The response is
201and holds the customer's session: itsid, 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. Renew the pair before the hour ends with Refresh a customer session.
What Black checks
Black accepts the token only when every one of these holds:
- Google signed it with RS256, with a key from Google's published keys.
- Its issuer is
accounts.google.comorhttps://accounts.google.com. - Its audience is the environment's
google_client_id. - It has not ended. Black allows no leeway on the end, and 60 seconds on a start time that is in the future.
- It names the customer with a
subof 1 to 255 characters.
Black matches the customer by Google's sub, never by the email. A customer who signs in with Google a second time is the same customer.
When the email counts as proved
Black counts the email of the token as proved only where Google is the authority for it: the token's email_verified is true and the address ends in @gmail.com, or the token carries a non-empty hd, the domain of a Google Workspace, which issues addresses only on domains it has verified. A proved email marks the customer's Google sign-in as proved, and links the sign-in to a customer who already holds that email proved, so one person is one customer however they sign in.
For any other token the sign-in is made with no email, and a new customer has none. An email longer than 255 characters counts as not proved too.
Handle each refusal
Each refusal is an error with a code: see Errors for every code.
403sign_in_method_disabled. Google is off, or on with no client ID. Turn onsign_in.googlewithsign_in.google_client_id, then send the call again.401google_token_invalid. The token fails a check above, or is empty or malformed, as when the customer cancels Google's sign-in. Ask the customer to sign in with Google again.503google_keys_unavailable. Black cannot fetch Google's keys with certainty, so it does not judge the token. Send the same call again in a moment.409blocked. The customer is blocked. No session is made. Unblock the customer first: see Block a customer.429rate_limited. The limit of the call is reached: 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.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.
A runnable sample
An ID token comes from Google and cannot be made on your machine, so this sample does not complete a sign-in. It registers an app and its publishable key, sends a made-up token while Google is off, which is refused with 403 and sign_in_method_disabled, turns Google on with a sample client ID, and sends the made-up token again, which is refused with 401 and google_token_invalid. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, and needs sed.
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":"Google 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/google \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data '{"token":"not-a-real-id-token"}'
curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"sign_in":{"google":true,"google_client_id":"42-sample.apps.example.com"}}'
curl --silent --request POST https://api.black.example/v1/client/auth/google \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data '{"token":"not-a-real-id-token"}'
The app and its key answer 201. The first sign-in answers 403 with sign_in_method_disabled, the settings change answers 200 and holds sign_in.google as true, and the second sign-in answers 401 with google_token_invalid.
What to do next
- Read the call's fields and answers in Sign a customer in with a Google ID token.
- Read how a customer signs in with a password in Sign customers in with email and password.
- Read how a customer signs in with a link in Sign customers in with an email link.
- Read how to change settings in Change account settings.
- Read every error code in Errors.