Building secure applications
with keycloak (OIDC/JWT)
Abhishek Koserwal
Red Hat
● Identification: a set of attributes related to an entity
■ (eg: user -> attribute [ name, email, mobile ] )
● Authentication: is the process of verifying an identity
■ (who they say they are)
● Authorization: is the process of verifying what someone is allowed to do
■ (permissions)
● Accounting: logs, user actions, traceability of actions
IAAA Security Factor
Oauth 2 & OpenID Connect
Oauth 2 != Authentication, only Authorization
OpenID Connect = Identity + Authentication + Authorization
50+ Security Specifications...
What is Keycloak?
Open Source
Identity Solution for Applications, Services and APIs
Why to use keycloak?
● Reliable Solution
● ! Reinventing the wheel ? (shared libraries, keys/certs, configuration, standards)
● Open Source (3C’s)
■ Cost
■ Customizable / Contributions
■ Community
● Hybrid Cloud Model
Core Concepts
Keycloak
Users
Identity
Provider
User
Federation
LDAP
Kerberos
SAML
OpenID Connect
Github, Twitter,
Google,
Facebook..etc
Roles
Groups
Events
Roles
UI (Themes)
Clients
Realm: master
Security
Defenses
App: Integration
Keycloak
Frontend App
Client ID: hello-world-app
Realm: external-
apps
Backend App
Keycloak
Adapter
OpenID Connect / SAML
Resource Endpoint
<HTTPS>Client Side: JS
Server Side:
Java, Python, Node.js, Ruby, C#..
etc
Mobile App
SDK: Android, IOS
How we used..
Keycloak
adapter
keycloak
Backend
Services/
BFF
Keycloak
adapter
Backend
Services/
BFF
Load Balancer
FrontendClient
Sticky
Sessions
<SAML>
Session ID: S-1
T1 T2
How we used..
Keycloak
adapter
keycloak
Backend
Services/
BFF
Keycloak
adapter
Backend
Services/
BFF
Load Balancer
FrontendClient
Sticky
Sessions
<SAML>
Session ID: S-1 Session ID: S-1
Session Replication
T1 T2
Problems
● Scalability with server side sessions
● Sticky Sessions are Evil
● Shifting monolith to Openshift/Containers (stateful -> stateless)
Service-to-Service : Authentication & Authorization
Service
B
Service
A
{
userId: “jack”
}
How to verify
and validate?
{
userId: ??
}
The Confused Deputy Problem
Service
B
Service
A
{
userId: “jack”
permission:”user”
}
Service
C
{
userId: “jack”
permission:”user”
}
{
userId: “jack”
permission:”admin”
}
Stateless Architecture
Keycloak
Load Balancer / Routes
Backend
Services/
BFF
Token
Validation
Node
Pod-1 Pod-2
Realm: /JWKS (Json Web Key Set)
{
Key: “AAkV6d-anw0vwPMJfCb8223”
}
Frontend
Keycloak
adapter
Backend
Services/
BFF
Token
Validation
Stateless Architecture
Keycloak
Load Balancer / Routes
Backend
Services/
BFF
Token
Validation
Node
Frontend
Keycloak
adapter
Backend
Services/
BFF
Token
Validation
JWT: <Header>.<Payload>.<Signature>
<Header>: Check
alg: "HS256"/ RSA256"
<Payload>: Claims
{"aud": "hello-world-app"}
<Signature>:
Verify Signature
Response
Setup: keycloak
docker pull jboss/keycloak
docker run -d -e KEYCLOAK_USER=<USERNAME> -e KEYCLOAK_PASSWORD=<PASSWORD> -p 8081:8080 jboss/keycloak
Require docker daemon running
Standalone server distribution
(https://www.keycloak.org/downloads.htm)
Standard way to run: Jboss / Wildfly
Application Demo
JWT: Json Web Tokens
● JWT over HTTPS and never HTTP
● Access tokens: are tokens that give those who have them access to protected resources (Short
lived)
● Refresh tokens: allow clients to request new access tokens.
● Cookie vs local storage
○ local storage prone to cross-site scripting (XSS)
○ Cookie only with HttpOnly flag (size < 4 kb), prone to Cross-Site Request Forgery (CSRF)
Keycloak
vs
Others
● Designed as a single product
● Easy to setup & configure
● Supports Docker registry
Auth
● OpenJDK support
● spring-boot support :
http://start.spring.io/
Securing keycloak
● Make sure to secure keycloak end-points
● IP Restriction/Port restriction for the endpoint/auth/admin console
● Configure security defenses like: Password guess: brute force attacks
● If an access token or refresh token is compromised, revocation policy to all
applications
● Client config: hostname is based on the request headers.
Q & A
Thank You!

Building secure applications with keycloak

  • 1.
    Building secure applications withkeycloak (OIDC/JWT) Abhishek Koserwal Red Hat
  • 2.
    ● Identification: aset of attributes related to an entity ■ (eg: user -> attribute [ name, email, mobile ] ) ● Authentication: is the process of verifying an identity ■ (who they say they are) ● Authorization: is the process of verifying what someone is allowed to do ■ (permissions) ● Accounting: logs, user actions, traceability of actions IAAA Security Factor
  • 3.
    Oauth 2 &OpenID Connect Oauth 2 != Authentication, only Authorization OpenID Connect = Identity + Authentication + Authorization 50+ Security Specifications...
  • 4.
    What is Keycloak? OpenSource Identity Solution for Applications, Services and APIs
  • 5.
    Why to usekeycloak? ● Reliable Solution ● ! Reinventing the wheel ? (shared libraries, keys/certs, configuration, standards) ● Open Source (3C’s) ■ Cost ■ Customizable / Contributions ■ Community ● Hybrid Cloud Model
  • 6.
    Core Concepts Keycloak Users Identity Provider User Federation LDAP Kerberos SAML OpenID Connect Github,Twitter, Google, Facebook..etc Roles Groups Events Roles UI (Themes) Clients Realm: master Security Defenses
  • 7.
    App: Integration Keycloak Frontend App ClientID: hello-world-app Realm: external- apps Backend App Keycloak Adapter OpenID Connect / SAML Resource Endpoint <HTTPS>Client Side: JS Server Side: Java, Python, Node.js, Ruby, C#.. etc Mobile App SDK: Android, IOS
  • 8.
  • 9.
    How we used.. Keycloak adapter keycloak Backend Services/ BFF Keycloak adapter Backend Services/ BFF LoadBalancer FrontendClient Sticky Sessions <SAML> Session ID: S-1 Session ID: S-1 Session Replication T1 T2
  • 10.
    Problems ● Scalability withserver side sessions ● Sticky Sessions are Evil ● Shifting monolith to Openshift/Containers (stateful -> stateless)
  • 11.
    Service-to-Service : Authentication& Authorization Service B Service A { userId: “jack” } How to verify and validate? { userId: ?? }
  • 12.
    The Confused DeputyProblem Service B Service A { userId: “jack” permission:”user” } Service C { userId: “jack” permission:”user” } { userId: “jack” permission:”admin” }
  • 13.
    Stateless Architecture Keycloak Load Balancer/ Routes Backend Services/ BFF Token Validation Node Pod-1 Pod-2 Realm: /JWKS (Json Web Key Set) { Key: “AAkV6d-anw0vwPMJfCb8223” } Frontend Keycloak adapter Backend Services/ BFF Token Validation
  • 14.
    Stateless Architecture Keycloak Load Balancer/ Routes Backend Services/ BFF Token Validation Node Frontend Keycloak adapter Backend Services/ BFF Token Validation JWT: <Header>.<Payload>.<Signature> <Header>: Check alg: "HS256"/ RSA256" <Payload>: Claims {"aud": "hello-world-app"} <Signature>: Verify Signature Response
  • 15.
    Setup: keycloak docker pulljboss/keycloak docker run -d -e KEYCLOAK_USER=<USERNAME> -e KEYCLOAK_PASSWORD=<PASSWORD> -p 8081:8080 jboss/keycloak Require docker daemon running Standalone server distribution (https://www.keycloak.org/downloads.htm) Standard way to run: Jboss / Wildfly
  • 16.
  • 17.
    JWT: Json WebTokens ● JWT over HTTPS and never HTTP ● Access tokens: are tokens that give those who have them access to protected resources (Short lived) ● Refresh tokens: allow clients to request new access tokens. ● Cookie vs local storage ○ local storage prone to cross-site scripting (XSS) ○ Cookie only with HttpOnly flag (size < 4 kb), prone to Cross-Site Request Forgery (CSRF)
  • 18.
    Keycloak vs Others ● Designed asa single product ● Easy to setup & configure ● Supports Docker registry Auth ● OpenJDK support ● spring-boot support : http://start.spring.io/
  • 19.
    Securing keycloak ● Makesure to secure keycloak end-points ● IP Restriction/Port restriction for the endpoint/auth/admin console ● Configure security defenses like: Password guess: brute force attacks ● If an access token or refresh token is compromised, revocation policy to all applications ● Client config: hostname is based on the request headers.
  • 20.

Editor's Notes

  • #4 https://www.x2u.club/work-delegation-memes.html
  • #5 https://www.keycloak.org/
  • #7 https://www.keycloak.org/docs/latest/server_admin/index.html#core-concepts-and-terms
  • #8 https://www.keycloak.org/docs/3.1/securing_apps/topics/overview/what-are-client-adapters.html
  • #9 https://www.keycloak.org/docs/3.1/server_admin/topics/sso-protocols/saml.html
  • #11 http://www.chaosincomputing.com/2012/05/sticky-sessions-are-evil/
  • #12 https://dzone.com/articles/api-security-ways-to-authenticate-and-authorize
  • #13 https://blogs.mulesoft.com/dev/connectivity-dev/google-oauth-security-confused-deputy/ https://samnewman.io/books/building_microservices/
  • #14 https://www.keycloak.org/docs/3.3/server_admin/topics/identity-broker/oidc.html
  • #15 https://www.keycloak.org/docs/3.3/server_admin/topics/identity-broker/oidc.html https://jwt.io/ https://auth0.com/blog/a-look-at-the-latest-draft-for-jwt-bcp/ https://github.com/akoserwal/keycloak-jwt https://www.npmjs.com/package/keycloak-angular https://github.com/akoserwal/keycloak-jwt https://github.com/akoserwal/statelessappdemo
  • #16 https://www.keycloak.org/downloads.html
  • #18 https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage https://www.owasp.org/index.php/HttpOnly https://www.keycloak.org/docs/2.5/server_admin/topics/threat/csrf.html http://self-issued.info/docs/draft-sheffer-oauth-jwt-bcp-00.html http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/
  • #19 https://gist.github.com/bmaupin/6878fae9abcb63ef43f8ac9b9de8fafd https://www.keycloak.org/docs/3.3/server_admin/topics/sso-protocols/docker.html
  • #20 https://www.keycloak.org/docs/latest/server_admin/index.html#threat-model-mitigation