CS › Web-Network

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.

OAuth 2.0 vs. OIDC for Access, ID, and Refresh Token roles

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-dev over self-signed HTTPS) tested with Python 3.14.7 and PyJWT 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 openid in scope, the identity provider (IdP) returns a signed id_token JWT 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.

Propertyid_tokenaccess_tokenrefresh_token
Primary roleUser authentication proof (OIDC)API authorization grant (OAuth 2.0)Obtain fresh Access Tokens
Intended verifierClient app (Relying Party)Resource Server (API)IdP /token endpoint only
Expected audClient’s own client_idTarget API ID (Keycloak default: account)IdP issuer URL
Keycloak default algRS256 (asymmetric, JWKS)RS256 (asymmetric, JWKS)HS512 (symmetric, IdP-internal)
Key claimsiss, sub, aud, nonce, at_hashaud, scope, realm_access, resource_accessiss, sub, aud, sid, scope
Observed TTL (exp - iat)300s (5 min)300s (5 min)1800s (30 min)
Do not use forAuthorization: Bearer API callsEstablishing a client login sessionStoring 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 fieldid_tokenaccess_tokenrefresh_token
Header algRS256RS256HS512
Header kidb44vG5nL... (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_hashBoth presentAbsentAbsent
Payload scope / rolesAbsentscope, realm_access, resource_accessscope only (no role claims)
TTL (exp - iat)300s300s1800s

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:

  1. id_token and access_token share the same RSA signing key (kid). Because both use b44vG5nL..., verifying only the JWKS signature without checking aud lets an access_token slip through as an id_token.
  2. refresh_token uses symmetric HS512. That internal HMAC key is never published at jwks_uri. Clients shouldn’t parse or validate Refresh Tokens—treat them as opaque credentials and send them only to the IdP’s /token endpoint.

By default in Keycloak 26.8.2, access_token carries "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 in aud.

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 inputOutcomeObserved error / reason
Valid id_token (matching kid)VerifiedRS256 signature, iss, aud="app1-client", and nonce all matched
id_token with 1-byte payload editRejectedInvalidSignatureError: Signature verification failed
access_token passed as id_tokenRejectedInvalidAudienceError: Audience doesn't match (aud="account")
Mismatched expected_nonceRejectedCaught 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:

  1. Sending id_token in Authorization: Bearer headers to your API: An id_token targets client_id and 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 new id_token.
  2. Base64-decoding access_token in the browser to decide login state: Unverified decoding trusts client-modifiable strings and breaks the moment your IdP switches to opaque Access Tokens.
  3. Verifying only the JWKS signature without pinning alg, iss, and aud: As the Keycloak 26.8.2 output showed, id_token and access_token share the same RSA kid. Dropping the aud check accepts an Access Token as a login token, and omitting algorithms=["RS256"] leaves you open to algorithm-confusion attacks.
  4. Storing refresh_token in browser localStorage: Even with Keycloak’s short defaults, refresh_token lasts six times longer than access_token (1800s vs. 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.2 default client scopes and mappers in a single local container. Custom Protocol Mappers or IdPs configured for opaque Access Tokens change the exact claims and aud values, so always inspect live tokens and jwks_uri metadata 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_token only for client login verification and access_token only for Resource Server API calls.
  • Enforce full ID Token validation: Match kid from jwks_uri, pin algorithms, and check iss, aud == client_id, exp, and nonce.
  • Configure explicit API audiences: Add an Audience Mapper on the IdP so every Resource Server checks its own identifier in access_token.aud alongside scope or role claims.
  • Bind local accounts to iss + sub: Never key local user records solely on mutable email claims.
The OAuth 2.0 authorization framework enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service, or by allowing the third-party application to obtain access on its own behalf. This specification replaces and obsoletes the OAuth 1.0 protocol described in RFC 5849. [STANDARDS-TRACK]
OpenID Connect Core 1.0 incorporating errata set 2
The page you’re looking for does not exist.
This post is licensed under CC BY 4.0 by the author.