Skip to content

Sign in with Certilia

Certilia IDP lets users sign in to your application with Certilia and, with the user’s consent, gives your application verified personal data about them (name, OIB, e-mail, address…). You do not have to manage passwords or verify user identities yourself.

The integration follows the OpenID Connect (OIDC) standard, an identity layer on top of OAuth 2.0, and uses the Authorization Code Flow (OpenID Connect Core 1.0, section 3.1). In short: your application sends the user to Certilia to sign in, Certilia sends the user back with a one-time authorization code, and your server exchanges that code – directly and together with its Client Secret – for an ID token: a signed token that identifies the user and already contains the user data they agreed to share. Tokens and personal data never pass through the user’s browser.

  1. In the portal, under Certilia sign-in, click New application and register your application in two steps (registration is free of charge). Every new application gets a Client ID and a client secret (Client Secret), and your server finishes the sign-in; besides the secret, every sign-in also uses PKCE. An application in the browser and a mobile application sign in through their own server (see Browser and mobile applications); for SaaS, see Certilia sign-in in a SaaS product.

    In the first step you enter:

    • Name – users see it when they sign in. It cannot be changed later.
    • Purpose – a one-sentence description of what the application is for.
    • Redirect URIs – the addresses on your server to which Certilia returns the user after sign-in, at most ten. Only HTTPS is allowed, and HTTP only for localhost during development. Every request must send exactly one of them as redirect_uri (same scheme, host, port, path and trailing slash).

    In the second step:

    • Data about the person – the personal data your application asks for. The user approves it on the Certilia consent screen. Data marked as required must be present and approved, otherwise the sign-in fails, so that list should be as short as possible. A new application always gets the OIB: it is the user’s identifier (sub). The data that can be requested is listed in the section Data about the person.
    • Advanced – open it only if the default settings do not suit you:
      • Back-channel logout URL (optional) – an endpoint on your server that Certilia calls when the user’s Certilia session ends (see Logout).
      • Refresh tokens – enable the refresh_token grant only if your application needs new access tokens without asking the user to sign in again. The application always has the authorization_code grant.
      • Token lifetimes – lifetimes of the access, ID and refresh tokens (see Tokens and their lifetimes).
  2. Right after creating the application the portal shows the client secret. The secret is not sent by e-mail; you can view it later on the application’s page with Show secret (every view is recorded). If the secret may have been exposed, issue a new one there; the previous one then stops working immediately.

  3. The application’s page (Certilia sign-in → the application’s name → the Integration tab) shows the Client ID, the OpenID Discovery URL and all endpoints, and Quick start with your values shows the sign-in address and the token request with your Client ID and first redirect URI. You change the settings on the Settings tab.

  4. The OpenID Discovery URL returns a JSON document with everything else your application needs: issuer, jwks_uri (public keys for verifying token signatures) and end_session_endpoint (logout).

  5. Store the Client Secret on your server only (configuration or secret store). Treat it like a password: never put it into browser JavaScript, a mobile app or a source code repository.

The client secret must never be in code that runs on the user’s device, because it can be extracted without difficulty from JavaScript in the browser and from a mobile app’s installation package. For this reason, such an application also finishes the sign-in on its own server:

  • An application in the browser (SPA) uses the Backend for Frontend (BFF) pattern, which the IETF recommends in OAuth 2.0 for Browser-Based Applications. Your server performs the sign-in as in the examples, keeps the tokens, and gives the browser only a session cookie (HttpOnly, Secure, SameSite). To sign in, the SPA opens your server’s sign-in address, and it asks your server for the user’s data and everything else.
  • A mobile application opens the sign-in in the system browser, never in a WebView: Custom Tabs on Android (for example through AppAuth-Android), ASWebAuthenticationSession on iOS. It opens your server’s sign-in address, and the redirect URI is an address on your server. The server exchanges the code for tokens, validates the ID token and returns the user to the app through an Android App Link or an iOS Universal Link with a one-time, short-lived value, which the app exchanges at your server for its own session. The portal does not accept custom schemes (com.example.app:/callback).

Either way, Certilia’s tokens and the client secret stay on your server.

OpenID Connect Authorization Code Flow with Certilia IDP

  1. The user clicks Login with Certilia in your application.
  2. Your application redirects the browser to the Certilia Authorize URL (/oauth2/authorize).
  3. Certilia asks the user to sign in and to approve sharing the requested data (consent).
  4. Certilia redirects the browser back to your redirect URI with a single-use authorization code and your state.
  5. Your application checks that state is the value it sent in step 2.
  6. Your server sends the code, together with its Client ID and Client Secret, to the Token endpoint (/oauth2/token).
  7. Certilia verifies the code, the client credentials and the redirect_uri and returns an ID token with the user data, an access token and, if enabled, a refresh token.
  8. Your application validates the ID token and reads the user data from it.
  9. Your application creates its own session – the user is signed in.

In the examples below replace <idp-host> with the host of the URLs shown on the application’s page, https://app.example.com/callback with your redirect URI, and the values in angle brackets with your own values.

Step 1: The user clicks “Sign in with Certilia”

Section titled “Step 1: The user clicks “Sign in with Certilia””

When the user clicks the login button (see Login buttons), generate three random, unguessable values: state, nonce and the PKCE code_verifier (43 to 128 characters). Compute code_challenge = BASE64URL(SHA-256(code_verifier)) and store all three in the user’s session.

Redirect the browser (HTTP 302) to the Authorize URL (a single URL, split into lines here for readability):

https://<idp-host>/oauth2/authorize
?response_type=code
&client_id=<Client ID>
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&scope=openid
&state=<random state>
&nonce=<random nonce>
&code_challenge=<code challenge>
&code_challenge_method=S256
Parameter Required Description
response_type Yes Always code.
client_id Yes The Client ID from the application’s page.
redirect_uri Yes One of your redirect URIs, URL-encoded. It must match it exactly.
scope Yes Must contain openid (see Scope on the application’s page). You can also add profile: openid profile, separated by a space (%20 in the URL).
state Recommended A random value generated for every sign-in. Certilia returns it unchanged in step 4, which protects you against CSRF attacks.
nonce Recommended A random value generated for every sign-in. Certilia puts it into the ID token, which protects you against replayed tokens.
code_challenge Yes PKCE (RFC 7636): BASE64URL(SHA-256(code_verifier)). Applications created in the new portal and public clients cannot sign in without it.
code_challenge_method Yes Always S256.
prompt Optional login forces the user to sign in again even if they already have an active Certilia session.
Section titled “Step 3: The user signs in and gives consent”

Certilia shows its sign-in page, the user signs in and then sees a consent screen listing the data your application requested. Your application does not take part in this step and never sees the user’s credentials.

Step 4: Certilia sends the user back to your redirect URI

Section titled “Step 4: Certilia sends the user back to your redirect URI”

After a successful sign-in Certilia redirects the browser to your redirect URI:

https://app.example.com/callback?code=<authorization code>&state=<random state>

If the sign-in did not succeed (for example, the user cancelled it or did not approve mandatory data), you receive an error instead of a code:

https://app.example.com/callback?error=access_denied&error_description=<description>&state=<random state>

Your callback handler should:

  • compare state with the value stored in step 1 and stop if they differ (protection against CSRF);
  • if error is present, show the user a clear message and offer to try again;
  • if neither code nor error is present (for example, when the user returns after logout), show your application’s start page;
  • exchange the code for tokens immediately – it is valid only for a short time and can be used only once.

From your server – never from the browser – send a POST request to the Token endpoint. Your application authenticates with its Client ID and Client Secret using HTTP Basic authentication (-u in curl). The redirect_uri must be identical to the one sent in step 2, and code_verifier is the value from step 1. An older application that is a public client (created for a browser or a mobile device before the new portal) has no secret: instead of -u it sends the client_id parameter.

Prozor terminala
curl -X POST "https://<idp-host>/oauth2/token" \
-u "<Client ID>:<Client Secret>" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "code=<authorization code>" \
--data-urlencode "redirect_uri=https://app.example.com/callback" \
-d "code_verifier=<code verifier>"

If your library sends the credentials as form parameters (client_id and client_secret) instead of the Authorization header, check that this method is listed in token_endpoint_auth_methods_supported of the Discovery document.

Certilia checks the code, the client credentials and the redirect_uri, and answers:

{
"access_token": "<access token>",
"refresh_token": "<refresh token>",
"id_token": "<ID token (JWT)>",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "openid"
}

Step 8: Validate the ID token and read the user data

Section titled “Step 8: Validate the ID token and read the user data”

The id_token is a signed JSON Web Token (JWT). It identifies the signed-in user and contains the user data the user agreed to share, so your application needs no further request to Certilia. A JWT consists of three Base64URL-encoded parts separated by dots (header.payload.signature); the payload is a JSON object with the claims.

Before you trust the token and use its data, check that:

  • the signature is valid – verify it with the public keys published at jwks_uri;
  • iss equals the issuer from the Discovery document;
  • aud contains your Client ID;
  • exp is in the future (the token has not expired);
  • nonce equals the value stored in step 1.

Decoded payload (example – the user data claims depend on the data selected in your application’s settings and approved by the user):

{
"iss": "<issuer from the Discovery document>",
"sub": "12345678901",
"aud": "<Client ID>",
"iat": 1790000000,
"exp": 1790003600,
"nonce": "<random nonce>",
"email": "ivan.horvat@example.com",
"mobile": "+385911234567",
"country": "HR"
}

OpenID Connect libraries perform these checks automatically and return the claims. Optional data the user did not approve is not included, so your application must handle missing claims correctly.

The sub claim contains the user’s OIB and is the unique identifier of the user. Exceptionally, for foreign Certilia users who do not have an OIB, sub contains the number of the identification document with which the user was identified – so do not assume that sub is always a valid OIB (11 digits with a check digit). Use sub to link the Certilia user with the account in your application. Do not use the name or e-mail address for that, because they can change.

The data an application can ask for (names as in the portal):

Data Can be required
OIB always sent (the identifier sub)
First name, Last name yes
E-mail, Mobile, Address yes
Country, Date of birth, Gender yes
Photo no
Document data (issuing country, number, type, expiry date) no
Organisations (business certificates) no

The user must have and approve required data, otherwise the sign-in fails; optional data the user can refuse. The OIB is in the sub claim, and the first name, last name and e-mail address come as given_name, family_name and email. The names of the other claims can be seen in the token from your first test sign-in.

Create your own application session (for example a Secure, HttpOnly session cookie) and keep the tokens on your server. From this point on your application manages the session itself. Keep the ID token, as it is required for logout.

Certilia also provides the standard OpenID Connect UserInfo endpoint (userinfo_endpoint in the Discovery document), which returns the user data for a valid access token: a GET request with the header Authorization: Bearer <access token>. It is not part of the sign-in flow – all the user data is already in the ID token.

Token What it is for Lifetime
Authorization code One-time code from step 4, exchanged for tokens in step 6. Short-lived, single use.
ID token Proves who signed in, when and to which application, and contains the user data. The “ID token” token lifetime setting (10–3600 s).
Access token Authorizes calls to Certilia on behalf of the user (for example, the optional UserInfo endpoint). Not needed for the sign-in itself. The “Access token” setting (10–3600 s), also returned as expires_in.
Refresh token Gets a new access token without a new sign-in. The “Refresh token” setting (10–86400 s). Issued only when refresh tokens are enabled.

If the refresh_token grant type is enabled, your server can get a new access token when the old one expires, without asking the user to sign in again:

Prozor terminala
curl -X POST "https://<idp-host>/oauth2/token" \
-u "<Client ID>:<Client Secret>" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "refresh_token=<refresh token>"

The response has the same format as in step 7. It may contain a new refresh token – always keep the most recent one. When the refresh token expires, the user has to sign in again.

When the user signs out of your application, end your own session and then redirect the browser to the logout endpoint (end_session_endpoint in the Discovery document), so that the user’s Certilia session ends as well:

https://<idp-host>/oidc/logout
?id_token_hint=<ID token>
&post_logout_redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback

id_token_hint is the ID token received at sign-in. post_logout_redirect_uri must match one of the registered redirect URIs. After logout Certilia redirects the browser there without a code, so your callback handler should show the start page in that case.

If you entered a back-channel logout URL, Certilia notifies your server directly when the user’s Certilia session ends – for example, when the user signs out in another application that uses Certilia. Certilia sends a POST request with a signed logout_token (OpenID Connect Back-Channel Logout 1.0):

POST /<your back channel path> HTTP/1.1
Host: app.example.com
Content-Type: application/x-www-form-urlencoded
logout_token=<signed JWT>

Your endpoint should:

  • validate logout_token like an ID token (signature, iss, aud, iat) – it contains no nonce;
  • check that its events claim contains http://schemas.openid.net/event/backchannel-logout;
  • end the sessions of the user identified by sid (the session ID from the ID token – store it at sign-in) or by sub;
  • respond with HTTP 200.

The back-channel logout URL must use HTTPS and be reachable from the internet.

Symptom Cause and solution
Certilia shows an error page instead of returning the user to your application. The redirect_uri does not exactly match any registered redirect URI, or the client_id is wrong. For security reasons Certilia never redirects to an address that is not registered.
The callback receives error=access_denied. The user cancelled the sign-in or did not approve mandatory user data. Let the user try again.
The state in the callback does not match. The user’s session was lost between steps 1 and 4 (for example, the session cookie is set with SameSite=Strict or for a different domain), or the request was forged. Start a new sign-in.
The Token endpoint returns invalid_grant. The code has already been used or has expired, or the redirect_uri differs from the one sent in step 2. Exchange the code immediately and only once.
The Token endpoint returns invalid_client (HTTP 401). Wrong Client ID or Client Secret, or the credentials are sent in an unsupported way.
The Token endpoint returns unauthorized_client or unsupported_grant_type. The grant type (for example refresh_token) is not enabled for your application. Enable it in the application’s settings.
ID token validation fails. Signature: use the current keys from jwks_uri – keys can change, so do not hard-code them. aud: the token was issued for a different Client ID. exp / iat: make sure your server clock is synchronized (NTP).
Some user data is missing in the ID token. The data is not selected in the application’s settings, or the user did not approve it (optional data).
  • Exchange the code on your server and keep the Client Secret only there.
  • Send a new random state and nonce with every sign-in and always verify them.
  • Validate every ID token (signature, issuer, audience, expiry, nonce) before you trust it or use the user data in it.
  • Use HTTPS for your redirect URIs, your back-channel logout URL and the whole application.
  • Keep tokens on the server – never put them into URLs, logs or the browser’s local storage.
  • Identify users by the sub claim (the user’s OIB or, for foreign users without an OIB, their identification document number), not by name or e-mail address.
  • Request only the user data you actually need, and mark as mandatory only what your service cannot work without.
  • Use PKCE (RFC 7636) with the S256 method for every sign-in. Applications created in the new portal cannot sign in without it, and you can switch it on for older ones in the application’s settings.

Use the official Certilia buttons for the login link in your application. Download the button assets for your application.

Prijava sa Certilia Login with Certilia Certilia icon button Certilia logo button