Skip to main content

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​

  1. Turn on Google. Send a PATCH request to /v1/settings with {"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 with 422 and parameter_invalid, naming sign_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​

  1. Get the token. The customer signs in with Google on your page. Google gives your page an ID token for your client ID.

  2. Send it to Black. From the customer's page or app, send a POST request to /v1/client/auth/google with the publishable key in Black-Publishable-Key and the ID token as token, at most 8192 characters. A page sends no Authorization header. See Sign a customer in with a Google ID token.

  3. Read the session. The response is 201 and holds the customer's session: its 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. 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.com or https://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 sub of 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.

  • 403 sign_in_method_disabled. Google is off, or on with no client ID. Turn on sign_in.google with sign_in.google_client_id, then send the call again.
  • 401 google_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.
  • 503 google_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.
  • 409 blocked. The customer is blocked. No session is made. Unblock the customer first: see Block a customer.
  • 429 rate_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 seconds Retry-After gives, then send the call again.
  • 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.

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​