|
| 1 | +## Multiple Authentication strategies |
| 2 | + |
| 3 | +An authentication system in a LoopBack 4 application could potentially support |
| 4 | +multiple popular strategies, including basic auth, oauth2, saml, openid-connect, |
| 5 | +etc...And also allow programmers to use either a token based or a session based |
| 6 | +approach to track the logged-in user. |
| 7 | + |
| 8 | +The diagram below illustrates the high level abstraction of such an extensible |
| 9 | +authentication system. |
| 10 | + |
| 11 | +<img src="./imgs/multiple-auth-strategies-login.png" width="1000px" /> |
| 12 | + |
| 13 | +Assume the app has a static login page with a list of available choices for |
| 14 | +users to login: |
| 15 | + |
| 16 | +- local: basic auth with email/username + password |
| 17 | +- facebook account: oauth2 |
| 18 | +- google account: oauth2 |
| 19 | +- ibm intranet account: saml |
| 20 | +- openid account: openid-connect |
| 21 | +- ... |
| 22 | + |
| 23 | +For the local login, we retrieve the user from a local database. |
| 24 | + |
| 25 | +For the third-party service login, e.g. facebook account login, we retrieve the |
| 26 | +user info from the facebook authorization server using oauth2, then find or |
| 27 | +create the user in the local database. |
| 28 | + |
| 29 | +By clicking any one of the links, you login with a particular account and your |
| 30 | +status will be tracked in a session(with session-based auth), or your profile |
| 31 | +will be encoded into a JWT token(with token-based auth). |
| 32 | + |
| 33 | +A common flow for all the login strategies would be: the authentication action |
| 34 | +verifies the credentials and returns the raw information of that logged-in user. |
| 35 | + |
| 36 | +Here the raw information refers to the data returned from a third-party service |
| 37 | +or a persistent database. Therefore you need another step to convert it to a |
| 38 | +user profile instance which describes your application's user model. Finally the |
| 39 | +user profile is either tracked by a generated token OR a session + cookie. |
| 40 | + |
| 41 | +The next diagram illustrates the flow of verifying the client requests sent |
| 42 | +after the user has logged in. |
| 43 | + |
| 44 | +<img src="./imgs/multiple-auth-strategies-verify.png" width="1000px" /> |
| 45 | + |
| 46 | +The request goes through the authentication action which invokes the |
| 47 | +authentication strategy to decode/deserialize the user profile from the |
| 48 | +token/session, binds it to the request context so that actions after |
| 49 | +'authenticate' could inject it using DI. |
| 50 | + |
| 51 | +Next let's walk through the typical API flow of user login and user |
| 52 | +verification. |
| 53 | + |
| 54 | +## API Flows (using BasicAuth + JWT as example) |
| 55 | + |
| 56 | +Other than the LoopBack core and its authentication module, there are different |
| 57 | +parts included and integrated together to perform the authentication. |
| 58 | + |
| 59 | +The next diagram, using the BasicAuth + JWT authentication strategy as an |
| 60 | +example, draws two API flows: |
| 61 | + |
| 62 | +- Login: user login with email+password |
| 63 | +- Verify: verify the logged-in user |
| 64 | + |
| 65 | +along with the responsibilities divided among different parts: |
| 66 | + |
| 67 | +- LoopBack core: resolve a strategy based on the endpoint's corresponding |
| 68 | + authentication metadata, execute the authentication action which invokes the |
| 69 | + strategy's `authenticate` method. |
| 70 | + |
| 71 | +- Authentication strategy: |
| 72 | + |
| 73 | + - (login flow) verify user credentials and return a user profile(it's up to |
| 74 | + the programmer to create the JWT access token inside the controller |
| 75 | + function). |
| 76 | + - (verify flow) verify the token and decode user profile from it. |
| 77 | + |
| 78 | +- Authentication services: some utility services that can be injected in the |
| 79 | + strategy class. (Each service's functionalities will be covered in the next |
| 80 | + section) |
| 81 | + |
| 82 | +_Note: FixIt! the step 6 in the following diagram should be moved to LoopBack |
| 83 | +side_ |
| 84 | + |
| 85 | +<img src="./imgs/API-flow-(JWT).png" width="1000px" /> |
| 86 | + |
| 87 | +_Note: Another section for session based auth TBD_ |
| 88 | + |
| 89 | +## Authentication framework architecture |
| 90 | + |
| 91 | +The following diagram describes the architecture of the entire authentication |
| 92 | +framework and the detailed responsibility of each part. |
| 93 | + |
| 94 | +You can check the pseudo code in folder `docs` for: |
| 95 | + |
| 96 | +- [authentication-action](./authentication-action.md) |
| 97 | +- [authentication-strategy](./authentication-strategy.md) |
| 98 | +- [basic auth strategy](./strategies/basic-auth.md) |
| 99 | +- [jwt strategy](./strategies/jwt.md) |
| 100 | +- [oauth2 strategy](./strategies/oauth2.md) |
| 101 | +- [endpoints defined in controller](./controller-functions.md) |
| 102 | + |
| 103 | +And the abstractions for: |
| 104 | + |
| 105 | +- [user service](../src/services/user.service.ts) |
| 106 | +- [token service](../src/services/token.service.ts) |
| 107 | + |
| 108 | +<img src="./imgs/auth-framework-architecture.png" width="1000px" /> |
| 109 | + |
| 110 | +### Token based authentication |
| 111 | + |
| 112 | +- Login flow |
| 113 | + |
| 114 | + - authentication action: |
| 115 | + - resolve metadata to get the strategy |
| 116 | + - invoke strategy.authenticate() |
| 117 | + - set the current user as the return of strategy.authenticate() |
| 118 | + - strategy: |
| 119 | + - extract credentials from |
| 120 | + - transport layer(request) |
| 121 | + - or local configuration file |
| 122 | + - verify credentials and return the user profile (call user service) |
| 123 | + - controller function: |
| 124 | + - generate token (call token service) |
| 125 | + - return token or serialize it into the response |
| 126 | + |
| 127 | +- Verify flow |
| 128 | + - authentication action: |
| 129 | + - resolve metadata to get the strategy |
| 130 | + - invoke strategy.authenticate() |
| 131 | + - set the current user as the return of strategy.authenticate() |
| 132 | + - strategy: |
| 133 | + - extract access token from transport layer(request) |
| 134 | + - verify access token(call token service) |
| 135 | + - decode user from access token(call token service) |
| 136 | + - return user |
| 137 | + - controller: |
| 138 | + - process the injected user |
| 139 | + |
| 140 | +### Session based authentication |
| 141 | + |
| 142 | +- Login flow |
| 143 | + |
| 144 | + - authentication action: |
| 145 | + - resolve metadata to get the strategy |
| 146 | + - invoke strategy.authenticate() |
| 147 | + - strategy: |
| 148 | + - extract credentials from |
| 149 | + - transport layer (request) |
| 150 | + - or local configuration file |
| 151 | + - verify credentials (call user service) and return the user profile |
| 152 | + - controller: |
| 153 | + - serialize user info into the session |
| 154 | + |
| 155 | +- Verify flow |
| 156 | + - authentication action: |
| 157 | + - resolve metadata to get the strategy |
| 158 | + - invoke strategy.authenticate() |
| 159 | + - set the current user as the return of strategy.authenticate() |
| 160 | + - strategy: |
| 161 | + - extract session info from cookie(call session service) |
| 162 | + - deserialize user info from session(call session service) |
| 163 | + - return user |
| 164 | + - controller function: |
| 165 | + - process the injected user |
0 commit comments