A GENTLE INTRODUCTION�TO OAUTH2
Fabio, Moratti | Software Architect
AGENDA
WHY OAUTH2
CALLING A BACK-END API
THE PROBLEM
BEFORE OAUTH
2007: the first draft of OAuth1 published
2012: OAuth 2.0 Spec finalized
… work continues
WHAT OAUTH SOLVES?
OAuth2 solves the SingleSignOn use case: the user must not share the password with the application
HOW?
Introduces a separate (centralized) service that manages the authentication and redirects the user back to the API: this magically solves all the problems: the application never handles the password and authentication mechanisms are centralized
HOTEL ANALOGY
Front desk checks passport,
gives key card
Reader checks card,
opens room
HOTEL ANALOGY
Front desk is the OAuth2 server
The door reader is the back-end API
The key card is the token used for authorization
OAUTH2 ELEMENTS
ROLES
The user: you
API
The application: running in a back-end server, in the browser or installed on the mobile phone
The device: browser or mobile phone
Authorization Server
Resource Owner
OAuth Client
User Agent
Resource Server
The API or the back-end service
The centralized AOuth2 service that provides authorization
APPLICATIONS CATEGORIES
APPLICATION TYPES
Confidential: can be deployed with a secret: in practice server-side apps only
Public: cannot be deployed (safely) with a secret: obvious case is SPAs, but mobile apps have the same issue
USER CONSENT
The user consent is provided by the Authorization Server
FRONT AND BACK CHANNELS
Back Channel is a client / server communication, i.e. HTTPS call that provides:
Back Channel
Front Channel
Front Channel: the communication “bounces” on the user browser address bar via a redirect
SCOPES
APPLICATION REGISTRATION
OAUTH IN ACTION
SERVER SIDE APPLICATION
API
Authorization Server
Resource Owner
OAuth Client
User Agent
Resource Server
Start using the app
Redirect: params + hash
Login + hash
Authorization code
Authorization code
Authorization code + secret
API request + Access Token
Access Token
Generate secret and compute hash (PKCE)
Login & consent
Username
Password
Login
Verify secret’s hash
Verify Access Token
API Response
Application Response
SERVER SIDE APPLICATION
https://auth-server.com/auth?� response_type=code&
client_id=<CLIENT_ID>&
redirect_uri=<APP URL>&
scope=<auth scope>&
state=<state>&
code_challenge=<CODE_CHALLENGE>&
code_challenge_method=S256
The link prepared by the app to redirect the user to the AS
Proof Key Code Exchange
SERVER SIDE APPLICATION
https://app.com/redirect?� code=AUTH_CODE&� state=<state>
The link where the user is redirected by the AS
https://auth-server.com/token?� grant_type=authorization_code&
code=<AUTH_CODE>&
redirect_uri=REDIRECT_URI&
code_verifier=VERIFIER_STRING&
client_id=<CLIENT_ID>&
client_secret=<CLIENT_SECRET>
The call from the application to the AS
SERVER SIDE APPLICATION
{
“token_type”: “Bearer”,
“access_token”: “<ACCESS_TOKEN>”,
“expires_in”: 3600
“scope”: “<scope>”,
“refresh_token”: “<REFRESH_TOKEN>”
}
NATIVE APPLICATIONS
The flow is similar to the server side app
SINGLE PAGE APPLICATIONS
The flow is similar to the server side app
IOT DEVICES
DEVICE FLOW
API
Authorization Server
Resource Owner
OAuth Client
User Agent
Resource Server
Start using the app
Authorization code
Poll for authorization
Login & consent
Username
Password
Login
When user completes authentication
Access Token
User wants to login
Show URL & Code, check every 5 sec.
Go to this URL and enter the code
CLIENT CREDENTIALS
https://auth-server.com/token?� grant_type=client_credentials&
client_id=<CLIENT_ID>&
client_secret=<CLIENT_SECRET>
{
“token_type”: “Bearer”,
“access_token”: “<TOKEN>”,
“expires_in”: 3600
“scope”: “<scope>”
}
TOKENS
JWT: common standard to implement self encoded tokens.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
{
"alg": "HS256",
"typ": "JWT"
}
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
Header
Payload
OPEN ID CONNECT
{
"iss": "http://server.example.com",
"sub": "248289761001",
"aud": "s6BhdRkqt3",
"nonce": "n-0S6_WzA2Mj",
"exp": 1311281970,
"iat": 1311280970,
"name": "Jane Doe",
"birthdate": "0000-10-31",
"email": "janedoe@example.com",
}
RESOURCES
RESOURCES
THANK YOU