Lock a login to one person
Use login_hint so that BankID accepts only the person whose personnummer you already know
When you already know who should sign in, such as a person who confirms a sensitive change in their own account, you can lock the login to that person's personnummer. BankID then refuses anyone else's BankID.
This works with OpenID Connect only. The Session API has no login_hint.
Before you begin
- The client has the Personnummer scope (
nin), under Uppgifter klienten får hämta on its Inställningar tab. The lock itself works without it, but you need theninclaim to confirm the result. - You have the person's personnummer. Use the 12-digit form without a separator, such as
191212121212, so you can compare it with theninclaim as it is.
1. Add login_hint to the authorization request
Add login_hint=nin:<personnummer>, and ask for the nin scope:
const expectedNin = "191212121212"; // The person you expect, from your own records.
url.search = new URLSearchParams({
response_type: "code",
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
scope: "openid nin",
state,
nonce,
code_challenge: codeChallenge,
code_challenge_method: "S256",
login_hint: `nin:${expectedNin}`,
}).toString();Store expectedNin with the login's state, the same way as the PKCE verifier.
If the personnummer is malformed or has a wrong check digit, sajn redirects to your redirect_uri with error=invalid_request. The error_description doesn't repeat the number.
2. Compare nin after the token exchange
After you have verified the id_token, compare its nin claim with the number you sent:
const { payload } = await jwtVerify(tokens.id_token, jwks, { issuer: ISSUER, audience: CLIENT_ID });
if (payload.nonce !== login.nonce) throw new Error("Wrong nonce");
if (payload.nin !== login.expectedNin) throw new Error("A different person signed in");BankID enforces the lock, but your own check is what proves the result to your app. It also catches a request where the login_hint was lost or changed on the way.
Things to know
- The personnummer is part of the authorization URL, so it appears in the person's browser history and in any log of that URL on your side. sajn doesn't store it in its logs or database.
- In a sandbox organization, BankID is simulated and ignores the lock. Test the lock with a test client and two test BankIDs. See Test your integration.
- A
login_hintthat doesn't start withnin:is ignored. - In the 10-digit form,
+marks a person aged 100 or more. Encode it as%2Bin the URL.URLSearchParamsdoes that for you.
Was this page helpful?
Last updated on