OAuth 2.0 vs. OIDC for Access, ID, and Refresh Token roles
Compare OAuth 2.0 authorization and OIDC authentication using real Keycloak 26.8.2 Access, ID, and Refresh Tokens with PyJWT JWKS verification.
An OAuth 2.0 Access Token proves delegated API authorization to a resource server, not user authentication to your app. This post inspects all three tokens issued in a single Keycloak 26.8.2 login and verifies ID Tokens with JWKS in PyJWT to draw clear boundaries between Access, ID, and Refresh Tokens.
All token payloads and verification outputs below come from an isolated local Docker run of Keycloak
26.8.2(start-devover self-signed HTTPS) tested with Python3.14.7andPyJWT 2.15.1, referencing RFC 6749 and OpenID Connect Core 1.0.
Why OAuth 2.0 alone is not authentication
OAuth 2.0 and OpenID Connect (OIDC) share the same Authorization Code redirect flow, so teams often treat them as interchangeable. They answer two different questions.
- OAuth 2.0 (RFC 6749) handles authorization: can this client call a specific API on the user’s behalf? Its primary artifact is the
access_token. - OIDC (OpenID Connect Core 1.0) adds an authentication layer on top of OAuth 2.0. When the client includes
openidinscope, the identity provider (IdP) returns a signedid_tokenJWT stating who signed in, when, and for which client.
Treating an Access Token as login proof breaks down in three concrete ways. First, RFC 6749 doesn’t require Access Tokens to be JWTs—if an IdP switches to opaque strings, any client code decoding the token fails immediately. Second, an Access Token’s audience (aud) targets the resource server, not your client app. Accepting a leaked token minted for another client leaves you open to token substitution. Third, Access Tokens don’t guarantee a login-bound nonce or a fresh auth_time claim tied to the current authentication request.
Token roles and who verifies what
After an OIDC Authorization Code exchange, the /token endpoint typically returns three tokens in one JSON response. Even though they arrive together, each token belongs to a different verifier.
flowchart TD
IDP["IdP (Keycloak 26.8.2)"] -->|"id_token (RS256)"| RP["Client App (RP)"]
IDP -->|"access_token (RS256)"| API["Resource Server (API)"]
IDP -->|"refresh_token (HS512)"| TOKEN["IdP /token Endpoint"]
RP -->|"Verify iss, aud, nonce via JWKS"| SESSION["Create Local Session"]
API -->|"Verify aud, scope, roles via JWKS"| RES["Serve Protected API"]
Here is how the three tokens split responsibilities—and where each one should never be used.
| Property | id_token | access_token | refresh_token |
|---|---|---|---|
| Primary role | User authentication proof (OIDC) | API authorization grant (OAuth 2.0) | Obtain fresh Access Tokens |
| Intended verifier | Client app (Relying Party) | Resource Server (API) | IdP /token endpoint only |
Expected aud | Client’s own client_id | Target API ID (Keycloak default: account) | IdP issuer URL |
Keycloak default alg | RS256 (asymmetric, JWKS) | RS256 (asymmetric, JWKS) | HS512 (symmetric, IdP-internal) |
| Key claims | iss, sub, aud, nonce, at_hash | aud, scope, realm_access, resource_access | iss, sub, aud, sid, scope |
Observed TTL (exp - iat) | 300s (5 min) | 300s (5 min) | 1800s (30 min) |
| Do not use for | Authorization: Bearer API calls | Establishing a client login session | Storing in localStorage or client-side JWKS validation |
Inspecting all three tokens from one Keycloak 26.8.2 login
To see these differences in practice, I ran Keycloak 26.8.2 with a demo-realm and a public client (app1-client) enforcing PKCE S256. A single login for test user alice with scope=openid profile email and nonce=demo-nonce-20261203 produced all three tokens below.
| Observed field | id_token | access_token | refresh_token |
|---|---|---|---|
Header alg | RS256 | RS256 | HS512 |
Header kid | b44vG5nL... (Realm RSA key) | b44vG5nL... (same RSA key) | 77499da8-... (internal HMAC key) |
Payload typ | "ID" | "Bearer" | "Refresh" |
Payload aud | "app1-client" | "account" | "https://127.0.0.1:18443/realms/demo-realm" |
Payload nonce / at_hash | Both present | Absent | Absent |
Payload scope / roles | Absent | scope, realm_access, resource_access | scope only (no role claims) |
TTL (exp - iat) | 300s | 300s | 1800s |
Comparing the decoded headers and payloads side by side (with raw signatures masked) makes the role separation obvious. For a broader look at registered vs. private JWT claims across real services, see my earlier comparison of JWT payloads.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{
"id_token": {
"header": { "alg": "RS256", "typ": "JWT", "kid": "b44vG5nLsjpADU1ewl_5Y6laaS-22x6ejCqfcee_4Os" },
"payload": {
"exp": 1796308157,
"iat": 1796307857,
"auth_time": 1796307857,
"iss": "https://127.0.0.1:18443/realms/demo-realm",
"aud": "app1-client",
"sub": "d7f25ecb-41ec-44a7-bcad-d6d986cb566f",
"typ": "ID",
"azp": "app1-client",
"nonce": "demo-nonce-20261203",
"sid": "P1UHrcGWBnyoj-TOSMyqNKLL",
"at_hash": "gTDJxHGxoPQYfEofG0he0g",
"email_verified": true,
"name": "Alice Kim",
"preferred_username": "alice",
"email": "alice@example.test"
},
"signature_preview": "KW5ysvVoXFF9...<masked:342chars>"
},
"access_token": {
"header": { "alg": "RS256", "typ": "JWT", "kid": "b44vG5nLsjpADU1ewl_5Y6laaS-22x6ejCqfcee_4Os" },
"payload": {
"exp": 1796308157,
"iat": 1796307857,
"iss": "https://127.0.0.1:18443/realms/demo-realm",
"aud": "account",
"sub": "d7f25ecb-41ec-44a7-bcad-d6d986cb566f",
"typ": "Bearer",
"azp": "app1-client",
"sid": "P1UHrcGWBnyoj-TOSMyqNKLL",
"realm_access": {
"roles": ["demo-user", "offline_access", "default-roles-demo-realm", "uma_authorization"]
},
"resource_access": {
"account": { "roles": ["manage-account", "manage-account-links", "view-profile"] }
},
"scope": "openid email profile",
"preferred_username": "alice",
"email": "alice@example.test"
},
"signature_preview": "eR2TlvBFrOCy...<masked:342chars>"
},
"refresh_token": {
"header": { "alg": "HS512", "typ": "JWT", "kid": "77499da8-e5fc-4478-8de6-9551ced55fb2" },
"payload": {
"exp": 1796309657,
"iat": 1796307857,
"iss": "https://127.0.0.1:18443/realms/demo-realm",
"aud": "https://127.0.0.1:18443/realms/demo-realm",
"sub": "d7f25ecb-41ec-44a7-bcad-d6d986cb566f",
"typ": "Refresh",
"azp": "app1-client",
"sid": "P1UHrcGWBnyoj-TOSMyqNKLL",
"scope": "openid web-origins email roles basic profile acr"
},
"signature_preview": "Cfv8er38rSdB...<masked:86chars>"
}
}
Two details stand out from this capture:
id_tokenandaccess_tokenshare the same RSA signing key (kid). Because both useb44vG5nL..., verifying only the JWKS signature without checkingaudlets anaccess_tokenslip through as anid_token.refresh_tokenuses symmetricHS512. That internal HMAC key is never published atjwks_uri. Clients shouldn’t parse or validate Refresh Tokens—treat them as opaque credentials and send them only to the IdP’s/tokenendpoint.
By default in Keycloak 26.8.2,
access_tokencarries"aud": "account". When you protect your own backend API as a Resource Server, add an Audience Mapper to the client scope so the API’s identifier appears inaud.
Verifying ID Tokens with JWKS and rejecting misuse
OpenID Connect Core 1.0 Section 3.1.3.7 requires clients to verify the signature, iss, aud, exp, and nonce before trusting an id_token. The snippet below fetches the matching public key from jwks_uri (discovered via /.well-known/openid-configuration) using PyJWT[crypto] 2.15.1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# verify_id_token.py (PyJWT[crypto]==2.15.1, Python 3.14.7)
# For local self-signed TLS, set SSL_CERT_FILE=/path/to/local-ca-cert.pem before running
import jwt
from jwt import PyJWKClient
# jwks_uri = "https://127.0.0.1:18443/realms/demo-realm/protocol/openid-connect/certs"
jwks_client = PyJWKClient(jwks_uri)
signing_key = jwks_client.get_signing_key_from_jwt(id_token)
claims = jwt.decode(
id_token,
signing_key.key,
algorithms=["RS256"], # Pin alg to prevent none / HS256 confusion
audience="app1-client", # Enforce aud == client_id
issuer="https://127.0.0.1:18443/realms/demo-realm",
)
assert claims["nonce"] == expected_nonce
Running four test cases against the real Keycloak 26.8.2 tokens produced these exact outcomes:
| Test input | Outcome | Observed error / reason |
|---|---|---|
Valid id_token (matching kid) | Verified | RS256 signature, iss, aud="app1-client", and nonce all matched |
id_token with 1-byte payload edit | Rejected | InvalidSignatureError: Signature verification failed |
access_token passed as id_token | Rejected | InvalidAudienceError: Audience doesn't match (aud="account") |
Mismatched expected_nonce | Rejected | Caught by the nonce equality check |
Notice at_hash in the id_token payload as well. It holds the Base64URL-encoded left half of the SHA-256 hash of the accompanying access_token, letting a client confirm both tokens came from the same issuance. For an end-to-end app integration that pairs JWKS validation with iss+sub account linking on Keycloak 26.8.2, see the Gnuboard5 OIDC SSO plugin write-up.
Common token misuse patterns and when not to use each
Even when teams know the spec, shortcuts creep in under deadline pressure. Avoid these four patterns in production:
- Sending
id_tokeninAuthorization: Bearerheaders to your API: Anid_tokentargetsclient_idand carries no API scopes or roles. Accepting it on a Resource Server breaks least-privilege scoping and can fail during token refresh when an IdP omits a newid_token. - Base64-decoding
access_tokenin the browser to decide login state: Unverified decoding trusts client-modifiable strings and breaks the moment your IdP switches to opaque Access Tokens. - Verifying only the JWKS signature without pinning
alg,iss, andaud: As the Keycloak 26.8.2 output showed,id_tokenandaccess_tokenshare the same RSAkid. Dropping theaudcheck accepts an Access Token as a login token, and omittingalgorithms=["RS256"]leaves you open to algorithm-confusion attacks. - Storing
refresh_tokenin browserlocalStorage: Even with Keycloak’s short defaults,refresh_tokenlasts six times longer thanaccess_token(1800svs.300s), and production realms often extend it to hours or days. A single XSS bug exposes long-lived token minting.
These measurements reflect Keycloak
26.8.2default client scopes and mappers in a single local container. Custom Protocol Mappers or IdPs configured for opaque Access Tokens change the exact claims andaudvalues, so always inspect live tokens andjwks_urimetadata before enforcing rules in production.
Pre-production checklist and references
Before shipping an OAuth 2.0 + OIDC integration, verify four items:
- Separate token consumers: Use
id_tokenonly for client login verification andaccess_tokenonly for Resource Server API calls. - Enforce full ID Token validation: Match
kidfromjwks_uri, pinalgorithms, and checkiss,aud == client_id,exp, andnonce. - Configure explicit API audiences: Add an Audience Mapper on the IdP so every Resource Server checks its own identifier in
access_token.audalongsidescopeor role claims. - Bind local accounts to
iss+sub: Never key local user records solely on mutableemailclaims.