Authn & Authz for Microservice: 03 — Request Flows

End-to-end walkthroughs of the five key interactions in this PoC, showing how Keycloak, Kong, OPA, and banking-api-service collaborate on each request.


Flow 1: Demo User Setup

You run the demo script once before any login. It calls identity-bootstrap-service, which provisions repeatable demo users in Keycloak.

Steps:

  1. The demo script calls POST /demo/users on identity-bootstrap-service.
  2. identity-bootstrap-service authenticates to Keycloak using an admin credential.
  3. identity-bootstrap-service creates or reconciles the demo user.
  4. It assigns the user’s role (customer or ops-admin) and custom claims (e.g., account_ids).
  5. Keycloak confirms the user is ready; identity-bootstrap-service returns 201.
Keycloak identity-bootstrap-service Demo Script Keycloak identity-bootstrap-service Demo Script POST /demo/users Admin token request Admin access token Create or reconcile demo user Assign role and attributes User ready 201 Created

For details on how identity-bootstrap-service works, see 10 — Identity Bootstrap Service.


Flow 2: User Login

You exchange credentials for a JWT. The token carries identity and claims used in every subsequent request.

Steps:

  1. The client posts username + password to Keycloak’s token endpoint.
  2. Keycloak validates the credentials.
  3. Keycloak returns a signed JWT access token.
Keycloak Client Keycloak Client POST /token (username + password) Validate credentials Signed JWT access token

For Keycloak token endpoint details and JWT claim structure, see 06 — Keycloak IdP.


Flow 3: Allowed Account Access

alice reads one of her own accounts. Kong (PEP) verifies the token is live, OPA (PDP) confirms alice owns that account, and banking-api-service re-validates before serving the data.

Example: alice requests GET /api/accounts/A-1001 where A-1001 belongs to alice.

Steps:

  1. The client sends the JWT as a bearer token to Kong.
  2. Kong introspects the token with Keycloak — confirms it is active.
  3. Kong sends the decoded claims and requested account_id to OPA.
  4. OPA evaluates the policy and returns allow (the account is in alice’s account_ids claim).
  5. Kong forwards the request to banking-api-service.
  6. banking-api-service re-validates the JWT signature, issuer, and audience.
  7. banking-api-service verifies alice owns A-1001.
  8. banking-api-service returns 200 with the account data.
banking-api-service OPA Keycloak Kong Client banking-api-service OPA Keycloak Kong Client GET /api/accounts/A-1001 + JWT Introspect token active account_id=A-1001 + alice claims + role allow Forward request Validate JWT (signature / issuer / audience) Confirm alice owns A-1001 200 account data 200 account data

For introspection details, see 11 — JWT Signature Validation. For OPA policy logic, see 08 — OPA. For wire-level payloads, see 14 — Request-Response Reference.


Flow 4: Forbidden Account Access

alice attempts to read an account she does not own. OPA denies it at the gateway — banking-api-service is never reached.

Example: alice requests GET /api/accounts/A-2001 where A-2001 belongs to a different customer.

For contrast: ops-admin making the same request would receive allow from OPA, because the ops-admin role grants access to any account.

Steps:

  1. The client sends the JWT to Kong.
  2. Kong introspects the token — confirms it is active.
  3. Kong sends the decoded claims and account_id=A-2001 to OPA.
  4. OPA returns deny (A-2001 is not in alice’s account_ids claim and she lacks the ops-admin role).
  5. Kong returns 403 Forbidden. The request never reaches banking-api-service.
OPA Keycloak Kong Client OPA Keycloak Kong Client GET /api/accounts/A-2001 + JWT (alice) Introspect token active account_id=A-2001 + alice claims + role deny 403 Forbidden

For OPA policy logic, see 08 — OPA. For wire-level payloads, see 14 — Request-Response Reference.


Flow 5: Missing Or Tampered Token

Requests without a valid token are rejected by Kong before any policy check.

Missing token:

  1. The client calls Kong without a bearer token.
  2. Kong rejects immediately with 401 Unauthorized.

Tampered token:

  1. The client sends a modified JWT.
  2. Kong introspects the token with Keycloak.
  3. Keycloak reports the token as inactive (signature mismatch or unknown).
  4. Kong returns 401 Unauthorized.
Keycloak Kong Client Keycloak Kong Client alt [missing token] [tampered token] API request (no token or tampered token) 401 Unauthorized Introspect token inactive 401 Unauthorized

For introspection details, see 11 — JWT Signature Validation.


Why These Flows Matter

Each flow illustrates the clean separation between components (defined in 01 — Concepts):

  • Keycloak authenticates and signs tokens.
  • Kong enforces edge access and drives the policy check.
  • OPA decides allow or deny from policy rules alone.
  • banking-api-service re-validates and provides business data.

No component over-reaches into another’s responsibility.


📚 返回专栏目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值