Equinor #AppSec community
De-mystify, build confidence and prepare for further exploration of A&A
|
|
The more I learn about the topic at hand, the more I realize how complex it is. I am not an expert. Very rarely is the answer black and white. Context is important. I assume that quite a few of you have experience, insight and knowledge to share. Please share! Sharing is caring!
Code examples are not production quality - they are happy-path
Authentication =is the mechanism to verify the identity of a user
Authorization = is the mechanism to verify access to a resource
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.oAuth2 is a delegation protocol, a means of giving someone who controls a resource the capability to delegate access to the resource on their behalf (without impersonating).
+--------+ +---------------+ | |--(A)- Authorization Request ->| Resource | | | | Owner | | |<-(B)-- Authorization Grant ---| | | | +---------------+ | | | | +---------------+ | |--(C)-- Authorization Grant -->| Authorization | | Client | | Server | | |<-(D)----- Access Token -------| | | | +---------------+ | | | | +---------------+ | |--(E)----- Access Token ------>| Resource | | | | Server | | |<-(F)--- Protected Resource ---| | +--------+ +---------------+
+-- Resource --+ +-- Client -- + +- Authorization -+ +-- Resource --+ + Owner + + + + Server + + Server + +--------------+ +-------------+ +-----------------+ +--------------+ - - - - | (A)Client request| | | | Authorization| | | |<------------------| | | |- - - - - - - - - - - - - - - - - ->| | | | | | |(B) Resource Owner | | | |Grants authorization | | |----------------------------------->| | | |<- - - - - - - -| | | | | | | |(C) Client send | | | | Authorization | | | | grant | | | |--------------->| | | | | | | | (D)Autorization| | | | server sends| | | | access token| | | |<---------------| | | | | | | |(E)Client sends | | | |Access Token | | | |------------------------------------->| | | | | | | |(F)Protected resource| | | | sends resource| | |<-------------------------------------| | | | | - - - -
To request an access token, the client obtains authorization from the resource owner. The authorization is expressed in the form of an authorization grant, which the client uses to request the access token. OAuth defines four grant types: authorization code, implicit, resource owner password credentials, and client credentials. It also provides an extension mechanism for defining additional grant types.
More on tokens later....
+----------+ | Resource | | Owner | | | +----------+ ^ | (B) +----|-----+ Client Identifier +---------------+ | -+----(A)-- & Redirection URI ---->| | | User- | | Authorization | | Agent -+----(B)-- User authenticates --->| Server | | | | | | -+----(C)-- Authorization Code ---<| | +-|----|---+ +---------------+ | | ^ v (A) (C) | | | | | | ^ v | | +---------+ | | | |>---(D)-- Authorization Code ---------' | | Client | & Redirection URI | | | | | |<---(E)----- Access Token -------------------' +---------+ (w/ Optional Refresh Token)
-1- Client via user agent - initiates |
-2- GET /authorize? client_id=client &scope=(optional) &reponse_type=code &state=1234 &redirect_uri= https://app/callback |
-3- Authentication is handed over to authentication server |
-4- 303 redirect https://app/callback? code=o-t-c &state=1234 |
-5- POST /token grant_type=authorization_code &client_id= &client_secret= &code= &redirect_uri= |
-6- { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"Bearer", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA" } |
oAuth2 term | OICD term |
Resouce owner | User |
Client | Relying party |
Authorisation server, Protected resource | Identity provider |
-2- GET /authorize? client_id=client &scope=read+openid &reponse_type=code &state=1234 &redirect_uri= https://app/callback |
-3- Authentication is handed over to authentication server |
-4- 303 redirect https://app/callback? code=o-t-c &state=1234 |
-5- POST /token grant_type=authorization_code &client_id= &client_secret= &code= &redirect_uri= |
-6- { "access_token":"ey2YotnFZFEjr1zCsicMWpAA", "token_type":"Bearer", "id_token": "eynndj3nn77hs7Hhjhjh" , "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA" } |
https://github.com/larskaare/WebAuthAuthorAndOtherCreatures
git clone git@github.com:larskaare/WebAuthAuthorAndOtherCreatures.git
$ npm install
$ export PORT=3000
$ export CLIENT_SECRET=[your secret]
$ export CLIENT_ID=[your client id]
$ export NODE_ENV=[production | developement | debug]
$ npm start
{
"typ": "JWT",
"alg": "HS256"
}.
{
"iss": "joe",
"exp": 1300819380,
}.
[Signature]
Header, Payload with claims and Signature - based64 encoded
{
"typ": "JWT",
"alg": "RS256"
}.{
"aud": "cab2507d-e7d1-46fd-9580-ea7de0cd02ea",
"iss": "https://login.microsoftonline.com/../v2.0",
"exp": 1571657734,
"name": "Jon Doe",
"oid": "..",
"roles": [
"Admin"
],
"sub": "..",
}.[Signature]
$ npm install
$ npm start
$ npm install
$ npm start
$ npm install
{...}.
{...
"name": "Jon Doe",
"preferred_username": "",
"roles": [
"Writer"
],
"groups": ["",""],
}.[Signature]
Explore
ID Tokens and
Acess Tokens and in MS Identity platform as well as the optional claims (and the new token configuration in the AD app object).
(Claims does not always show up in the token you expect. OIDC drives some claims into the ID token)
$ npm start
"appRoles": [
{
"allowedMemberTypes": ["User"],
"displayName": "Writer",
"id": "b421319c-3e45-4685-aaf2-b1282ad05a6f",
"isEnabled": true,
"description": "Writers Have the ability to create tasks.",
"value": "Writer"
}
],
"availableToOtherTenants": false,
$ npm install
$ npm start
+----------+ | Resource | | Owner | | | +----------+ ^ | (B) +----|-----+ Client Identifier +---------------+ | -+----(A)-- & Redirection URI --->| | | User- | | Authorization | | Agent -|----(B)-- User authenticates -->| Server | | | | | | |<---(C)--- Redirection URI ----<| | | | with Access Token +---------------+ | | in Fragment | | +---------------+ | |----(D)--- Redirection URI ---->| Web-Hosted | | | without Fragment | Client | | | | Resource | | (F) |<---(E)------- Script ---------<| | | | +---------------+ +-|--------+ | | (A) (G) Access Token | | ^ v +---------+ | | | Client | | | +---------+ Client lives inside browser (user agent)
-2- GET /authorize? client_id=client &scope=user.read &reponse_type=token &state=1234 &redirect_uri= https://app/callback |
-3- Authentication is handed over to authentication server |
-4- 303 redirect https://app/callback? Fragment: # access_token=AT expires_in=3600 token_type=Bearer &state=1234 |
(for comparison)(spec)
-2- GET /authorize? client_id=client &scope=openid &reponse_type=id_token &state=1234 &nonce=a_nonce &redirect_uri= https://app/callback |
-3- Authentication is handed over to authentication server |
-4- 303 redirect https://app/callback? Fragment: # id_token=IDT expires_in=3600 token_type=Bearer &state=1234 &nonce=a_nonce |
$ npm install
$ npm start
For each request to get a token:
-1- Client via user agent - initiates |
-2- GET /authorize? client_id=client &scope=(optional) &reponse_type=code &state=1234 &redirect_uri= https://app/callback &code_challenge=(SHA of PKCE secret) &code_challenge_method=S256 |
-3- Authentication is handed over to authentication server |
-4- 303 redirect https://app/callback? code=o-t-c &state=1234 |
-5- POST /token grant_type=authorization_code &client_id= &code_verfier=(the PKCE secret) &code= &redirect_uri= |
-6- { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", "token_type":"Bearer" } |
$ npm install
$ npm start
$ npm install
$ npm install
$ npm start
$ npm start
|
|
|
|
|