|
| 1 | +# Authorization Code Grant Example |
| 2 | + |
| 3 | +> [!CAUTION] |
| 4 | +> Do not use this example in production 1:1. It is meant for educational purposes and |
| 5 | +> needs to be adapted to your specific use case. It is a minimal example that does not |
| 6 | +> include all necessary security measures. |
| 7 | +
|
| 8 | +## Architecture |
| 9 | + |
| 10 | +The authorization code workflow is described in |
| 11 | +[RFC 6749, section 4.1](https://datatracker.ietf.org/doc/html/rfc6749.html#section-4.1): |
| 12 | + |
| 13 | +``` |
| 14 | ++----------+ |
| 15 | +| Resource | |
| 16 | +| Owner | |
| 17 | +| | |
| 18 | ++----------+ |
| 19 | + ^ |
| 20 | + | |
| 21 | + (B) |
| 22 | ++----|-----+ Client Identifier +---------------+ |
| 23 | +| -+----(A)-- & Redirection URI ---->| | |
| 24 | +| User- | | Authorization | |
| 25 | +| Agent -+----(B)-- User authenticates --->| Server | |
| 26 | +| | | | |
| 27 | +| -+----(C)-- Authorization Code ---<| | |
| 28 | ++-|----|---+ +---------------+ |
| 29 | + | | ^ v |
| 30 | + (A) (C) | | |
| 31 | + | | | | |
| 32 | + ^ v | | |
| 33 | ++---------+ | | |
| 34 | +| |>---(D)-- Authorization Code ---------' | |
| 35 | +| Client | & Redirection URI | |
| 36 | +| | | |
| 37 | +| |<---(E)----- Access Token -------------------' |
| 38 | ++---------+ (w/ Optional Refresh Token) |
| 39 | +``` |
| 40 | + |
| 41 | +### Provider dependencies |
| 42 | + |
| 43 | +- @node-oauth/express-oauth-server (uses @node-oauth/oauth2-server) |
| 44 | +- express |
| 45 | +- body-parser |
| 46 | +- cors |
| 47 | + |
| 48 | +### Client dependencies |
| 49 | + |
| 50 | +- express |
| 51 | +- ejs |
| 52 | + |
| 53 | +## Installation and usage |
| 54 | + |
| 55 | +1. Install dependencies in both provider and client directories: |
| 56 | + |
| 57 | +```shell |
| 58 | +$ cd provider && npm install |
| 59 | +$ cd ../client && npm install |
| 60 | +``` |
| 61 | + |
| 62 | +2. Create a `.env` file in the authorization-code/provider directory: |
| 63 | + |
| 64 | +``` |
| 65 | +CLIENT_ID=testclient |
| 66 | +CLIENT_SECRET=testsecret |
| 67 | +REDIRECT_URI=http://localhost:3000/callback |
| 68 | +USER_ID=user1 |
| 69 | +USERNAME=demo |
| 70 | +PASSWORD=demo |
| 71 | +``` |
| 72 | + |
| 73 | +3. Create a `.env` file in the authorization-code/client directory: |
| 74 | + |
| 75 | +``` |
| 76 | +AUTH_SERVER=http://localhost:8080 |
| 77 | +CLIENT_ID=testclient |
| 78 | +CLIENT_SECRET=testsecret |
| 79 | +REDIRECT_URI=http://localhost:3000/callback |
| 80 | +``` |
| 81 | + |
| 82 | +4. Start the provider (authorization server + resource server): |
| 83 | + |
| 84 | +```shell |
| 85 | +$ cd provider && npm start |
| 86 | +``` |
| 87 | + |
| 88 | +5. Start the client application: |
| 89 | + |
| 90 | +```shell |
| 91 | +$ cd client && npm start |
| 92 | +``` |
| 93 | + |
| 94 | +6. Visit http://localhost:3000 to start the authorization code flow. |
| 95 | + |
| 96 | +## About This Example |
| 97 | + |
| 98 | +This example demonstrates a clear separation between the OAuth2 provider (authorization server + resource server) |
| 99 | +and the client application. |
| 100 | +Unlike other examples that might combine both roles in a single application, this example shows: |
| 101 | + |
| 102 | +- **Provider** (port 8080): Acts as both authorization server and resource server |
| 103 | +- **Client** (port 3000): A separate web application that consumes OAuth2 services |
| 104 | + |
| 105 | +This separation makes it easier to understand what the `@node-oauth/oauth2-server` library supports and what it doesn't. |
| 106 | + |
| 107 | +## Flow |
| 108 | + |
| 109 | +1. User visits the client application at http://localhost:3000 |
| 110 | +2. User clicks "Login" to start the authorization flow |
| 111 | +3. User is redirected to the provider's authorization page |
| 112 | +4. User enters credentials and grants authorization |
| 113 | +5. User is redirected back to the client with an authorization code |
| 114 | +6. Client exchanges the code for an access token |
| 115 | +7. Client can now access protected resources using the access token |
0 commit comments