Skip to content

OpenID Login ​

Users can sign in with an account they already have at an OpenID provider, such as Google, instead of a password for your project.

How it works ​

  1. The project owner registers an OpenID logger in the project settings on skapi.com. It tells Skapi which URL returns the user's profile for a token, and which profile attribute is the user's unique username. Each logger has an ID, for example google.
  2. Your app gets an access token from the provider, the way the provider documents it (on Android, for example, with the provider's own sign-in library).
  3. openIdLogin() sends the token and the logger ID. Skapi asks the provider for the profile and checks the account, then signs the user in, creating the account on first use.
kotlin
import com.skapi.Skapi
import com.skapi.SkapiException
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

val skapi = Skapi("<Project ID>")

val res = skapi.openIdLogin(accessToken, "google")
println(res["userProfile"]!!.jsonObject["user_id"]!!.jsonPrimitive.content)
println(res["openid"])  // the profile attributes the provider returned
java
import com.skapi.java.SkapiJava;
import java.util.Map;

SkapiJava skapi = new SkapiJava("<Project ID>");

Map<String, Object> res = skapi.openIdLogin(accessToken, "google").get();
System.out.println(((Map<?, ?>) res.get("userProfile")).get("user_id"));
System.out.println(res.get("openid"));  // the profile attributes the provider returned

It returns a JsonObject with userProfile (the user's profile) and openid (the attributes from the provider).

No password reaches your app. Skapi answers with a short-lived sign-in proof, valid for 5 minutes and tied to that one account, and openIdLogin() uses it at once to answer Cognito's sign-in challenge, so the login tokens come straight to your app. The proof is never stored. The two exceptions, accounts with two-step login and apps with record encryption, are below.

If the proof is refused or has expired, for example because the device clock or a slow network delayed it past 5 minutes, openIdLogin() throws INVALID_REQUEST (The OpenID sign-in could not be completed. Please try again.). Call it again.

If the account changes while the sign-in is being completed, for example because a password reset finishes at the same moment, openIdLogin() throws INVALID_REQUEST (The account changed during the OpenID sign-in. Please try again.). Signing in again works.

Blocked, disabled and unconfirmed accounts are refused as with login().

A new account made by an OpenID sign-in from 2.8.0 gets its e-mail marked verified only when the provider says the e-mail is verified, or does not say otherwise.

If the OpenID logger identifies users by e-mail and the provider says the user's e-mail is not verified, openIdLogin() from 2.8.0 throws INVALID_REQUEST (The OpenID provider has not verified this e-mail.). The user verifies the e-mail at the provider, then signs in again.

To send one of your project's uploaded templates as the welcome e-mail for a new account, pass template = jsonOf("welcome" to "<message_id>").

Existing accounts ​

Existing OpenID accounts, including ones created by earlier SDK versions, move to the new sign-in on their first OpenID sign-in from 2.8.0. That one time, the account is signed out on its other devices. An authenticator app the user set up stays on.

Only an account taken over through a merge, or one that was not proven to belong to the user, gets its authenticator app turned off. An account merged through 2.7.0 or older counts as taken over through a merge.

Two-step login ​

If the account has two-step login on, openIdLogin() throws MFA_REQUIRED and verifyMfa() finishes the login. The sign-in proof would skip the authenticator code, so for these accounts Skapi keeps the sign-in from before 2.8.0, which asks for it.

An account that has both a password of its own and an authenticator app cannot sign in with OpenID. openIdLogin() throws INVALID_REQUEST (This account signs in with its password and an authenticator app code. Sign in with the password.): the user signs in with login() and the code.

Merging with an existing account ​

If the user already signed up with a password using the same login ID, openIdLogin() throws ACCOUNT_EXISTS (The account already exists.). Ask the user, then merge the OpenID login into that account with merge:

  • merge = JsonPrimitive(true) merges the accounts and copies no attributes.
  • merge = jsonArrayOf("name") merges and also copies the listed OpenID attributes into the profile.
kotlin
import com.skapi.jsonArrayOf
import kotlinx.serialization.json.JsonPrimitive

val res = try {
    skapi.openIdLogin(accessToken, "google")
} catch (err: SkapiException) {
    if (err.code != "ACCOUNT_EXISTS") throw err
    if (!askUser("An account already exists. Merge?")) return
    skapi.openIdLogin(accessToken, "google", merge = JsonPrimitive(true))
}
java
import com.skapi.SkapiException;
import java.util.concurrent.ExecutionException;

Map<String, Object> res;
try {
    res = skapi.openIdLogin(accessToken, "google").get();
} catch (ExecutionException e) {
    SkapiException err = (SkapiException) e.getCause();
    if (!"ACCOUNT_EXISTS".equals(err.getCode())) throw err;
    if (!askUser("An account already exists. Merge?")) return;
    res = skapi.openIdLogin(accessToken, "google", true).get();
}

A merge replaces the account's password, as before, and also:

  • signs the account out on every device,
  • turns off an authenticator app left on the account; the user can set one up again once signed in,
  • stops counting the account's e-mail as verified if it is not the e-mail the OpenID provider shares; the user can verify it again once signed in.

This locks out a password, or an authenticator app, someone else may have set on an account made with that e-mail. The same happens when an OpenID sign-in takes over an account that was not proven to belong to the user. The user signs in with OpenID from then on, and can give the account a password of their own with a password reset.

An account that was merged through an SDK older than 2.8.0 goes through this once, on its first OpenID sign-in from 2.8.0: the user's other devices are signed out that one time and an authenticator app left on it is turned off. Other existing OpenID accounts keep their authenticator app (see Existing accounts).

DANGER

After a merge, the old password stops working at once. This cannot be undone.

With record encryption on, an account that already had encryption set up (it had signed in with its password while encryption was on) has its encrypted records locked with the old password, so after a merge the user needs their encryption recovery code once. The merge sign-in succeeds with encryption locked; in that session, unlockEncryptionWithRecoveryCode(code) (or unlockEncryption() with the old password) opens the records, and from then on OpenID sign-ins on any device open them by themselves. An account that never signed in while encryption was on needs no code: the merge sign-in sets encryption up. See When the recovery code is needed.

An existing account that has not confirmed its signup yet cannot be merged: openIdLogin() throws INVALID_REQUEST (The account needs to be confirmed.).

A password of their own ​

A user who signs in with OpenID, including after a merge, can also set a password of their own with the normal password reset: forgotPassword() with the account's e-mail, then resetPassword(). The reset code goes to the account's verified e-mail. login() with the e-mail and the new password then works.

In an app with encryption off, a password reset whose code went to the e-mail the OpenID provider shares keeps working next to OpenID sign-in. With record encryption on, openIdLogin() is refused after any password reset and the user signs in with login() (see below).

After a password reset whose code went to another e-mail, the account's existing sessions stop being renewed, so the user signs in again. In an app with encryption off, the next OpenID sign-in from 2.8.0 takes the account over again: it replaces that password and signs the account out on every device. If that sign-in throws ACCOUNT_EXISTS, the takeover happens when it is called again with merge.

The reset finds the account by its e-mail as login ID, so it works for an account whose login ID is its e-mail: one merged into an e-mail account, or one made by an OpenID logger whose username attribute is the e-mail.

If the account also has two-step login on, it then signs in with the password and the code only.

With record encryption on, the user signs in with login() and that password from then on, and unlocks once with their encryption recovery code and the new password. See When the recovery code is needed.

Record encryption ​

With record encryption on (Skapi("<Project ID>", encryption = EncryptionOptions())), openIdLogin() signs in the way it did before 2.8.0, with the password Skapi makes for the account, and nothing changes for these apps. Encryption works for OpenID users with nothing extra to set up: the SDK sets up and unlocks the user's keyring with that password, exactly as for a password login, and the first OpenID sign-in creates the encryption recovery code, so show it to OpenID users too. See OpenID users. In such an app a merge replaces the password as before, and an account that went through a password reset cannot sign in with OpenID: the user signs in with login(). openIdLogin() usually throws ACCOUNT_EXISTS (The account already exists.), and calling again with merge does not help; it throws INVALID_REQUEST (This account has its own password. Update the app to the latest Skapi SDK to sign in with OpenID, or sign in with the password.) only when the account had signed in with OpenID through a 2.8.0 app with encryption off and the reset code went to the e-mail the provider shares.

A normal OpenID sign-in never needs the encryption recovery code. It is needed once after a password reset, after a merge when the account already had encryption set up (it had signed in with its password while encryption was on), after the OpenID logger is deleted and registered again (rare), and once after each change of the server key Skapi uses for OpenID sign-in (rare). See When the recovery code is needed.

Older SDK versions ​

Apps on skapi-kotlin or skapi-java 2.7.0 or older keep signing in with OpenID as before, except on an account whose reset password the server marked as its own (see above). There openIdLogin() throws INVALID_REQUEST (This account has its own password. Update the app to the latest Skapi SDK to sign in with OpenID, or sign in with the password.). Update the dependency to 2.8.0 (see Version History).

skapi-kotlin 2.7.0