Skip to content

joma74/oauth2-nailed

Repository files navigation

Replay after Udemy "OAuth2.0 : Nailed the core framework"

See https://www.udemy.com/course/oauth-2-nailed-the-core-framework-with-hands-dirty

The oauth-nailed-app-1-auth-code UI

A golang server app

The oauth-nailed-app-1-auth-code UI

The oauth-nailed-app-2-implicit-grant UI

A react client app

The oauth-nailed-app-2-implicit-grant UI

Treated OAuth 2.0 Flows

The Abstract OAuth 2.0 Flow

The abstract OAuth 2.0 flow illustrated describes the interaction between the four roles.

The abstract OAuth 2.0 flow illustrated describes the interaction between the four roles.

See https://tools.ietf.org/html/rfc6749#section-1.2

The Authorization Code Grant

The authorization code grant type is used to obtain both access tokens and refresh tokens and is optimized for confidential clients.

Since this is a redirection-based flow, the client must be capable of interacting with the resource owner's user-agent (typically a web browser) and capable of receiving incoming requests (via redirection) from the authorization server.

Authorization Code Flow

See https://tools.ietf.org/html/rfc6749#section-4.1

The Implicit Grant

The implicit grant type is used to obtain access tokens (it does not support the issuance of refresh tokens) and is optimized for public clients known to operate a particular redirection URI. These clients are typically implemented in a browser using a scripting language such as JavaScript.

Implicit Grant Flow

See https://tools.ietf.org/html/rfc6749#section-4.2

The Resource Owner Password Credentials Grant

The resource owner password credentials grant type is suitable in cases where the resource owner has a trust relationship with the client, such as the device operating system or a highly privileged application.

This grant type is suitable for clients capable of obtaining the resource owner's credentials (username and password, typically using an interactive form). It is also used to migrate existing clients using direct authentication schemes such as HTTP Basic or Digest authentication to OAuth by converting the stored credentials to an access token.

Resource Owner Password Credentials Grant Flow

See https://tools.ietf.org/html/rfc6749#section-4.3

Setup Keycloak on Docker

docker run -p 9112:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -e TZ=Europe/Vienna quay.io/keycloak/keycloak:11.0.0
docker stop amazing_kapitsa
docker rename amazing_kapitsa keycloak_1
docker start keycloak_1
docker logs -f  --tail 20  keycloak_1

The admin interface is then reachable via http://localhost:9112/auth/

Administer Keycloak

Configure Realm

  • Name: myrealm

  • Client: oauth-nailed-app-1-auth-code

  • Client: oauth-nailed-app-1-token-checker

    • Client Protocol: openid-connect
    • Access Type: bearer-only
  • Client: oauth-nailed-app-2-implicit-grant

So that

Keycloaks Well-known Openid Configuration

Manage User

  • Name/Pwd: myuser/myuser
  • Email Verified: Off

Setup Client Scope

Add new client scope

  • Name: billingService
  • Name: billingServiceV2

Add scope billingService and billingServiceV2 to Client Scopes of clients

  • oauth-nailed-app-1-auth-code (Optional)
  • oauth-nailed-app-2-implicit-grant (Optional)

Setup Scope

  • oauth-nailed-app-1-auth-code
    • Full Scope Allowed: OFF

Setup billingService and billingServiceV2 Audience Mapper

Setup billingService and billingServiceV2 Audience Mapper

Setup billingService Audience Mapper

Check generated token includes output of the above billingService and billingServiceV2 Audience Mapper

Check generated token for the billingService Audience Mapper

Consents in Keycloak

Grant consent screen

Consent to grant access to infos

Administer consent revocation via Keycloak

Check and revoek consent of Keycloak user

Starting the OAuth Auth Code Client

cd src/client/
go run .

The OAuth Auth Code Client page is then reachable via http://localhost:9110/

It covers the flow of

  • A (Authorization Request, 4.1.1)
  • C (Authorization Response, 4.1.2)
  • D (Access Token Request, 4.1.3)
  • E (Access Token Response, 4.1.4)
  • E (Accessing Protected Resources, 7.)
  • F (Accessing Protected Resources, 7.)

The numbers reference the related section in https://tools.ietf.org/html/rfc6749

Starting the OAuth Protected Resource

cd src/billingservice/
go run .

It covers the flow of

  • E (Accessing Protected Resources, 7.)
  • F (Accessing Protected Resources, 7.)

The numbers reference the related section in https://tools.ietf.org/html/rfc6749

Starting the OAuth Implicit Grant Client

cd src/implicit-client
yarn start

The OAuth Implicit Grant Client page is then reachable via http://localhost:9109

It covers the flow of

  • A (Authorization Request, 4.2.1)
  • C (Access Token Response, 4.2.2)

The numbers reference the related section in https://tools.ietf.org/html/rfc6749#section-4.2

Proof Key for Code Exchange(PKCE) by OAuth Public Clients

The redirect URI Request of the OAuth Authentication Code Flow is deemed interceptable by a mallicious app on the same OS as where the redirect URI points to. So, a mallicious app might get the access token thereafter.

In that case, the Requesting Party creates a Proof Key and sends it's (prefered SHA265) hash along with the Authentication request. The Authorization server remembers that hash, and when the access token is requested, compares it to Proof Key, which is then sent along the request.

Consent to grant access to infos

References

ietf rfc* Standards

OpenID Connect

Keycloak

Tooling

Diverse

Go

Setup

joma@edison:oauth2-nailed (master*=) $ GOPATH=$(go env GOPATH)
joma@edison:oauth2-nailed (master*=) $ tree -L 2 $GOPATH
/home/joma/go
├── bin
│   ├── dlv
│   ├── fillstruct
│   ├── gocode
│   ├── gocode-gomod
│   ├── godef
│   ├── godoctor
│   ├── goimports
│   ├── golint
│   ├── gomodifytags
│   ├── go-outline
│   ├── gopkgs
│   ├── goplay
│   ├── gorename
│   ├── goreturns
│   ├── go-symbols
│   ├── gotests
│   ├── guru
│   └── impl
├── pkg
│   ├── linux_amd64
│   └── mod
└── src
    ├── github.com
    └── golang.org

7 directories, 18 files

Install a module

joma@edison:oauth2-nailed (master*=) $ cd src/client/
joma@edison:client (master*=) $ go get github.com/google/uuid
go: finding github.com/google/uuid v1.1.1
go: downloading github.com/google/uuid v1.1.1