Skip to content

Commit cb52133

Browse files
authored
Merge pull request #1250 from authts/improve-doc
improve documentation
2 parents b5d3233 + 7755013 commit cb52133

15 files changed

+322
-61
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ available [here](docs/migration.md).
2222
Implements the following OAuth 2.0 protocols and supports
2323
[OpenID Connect Core 1.0](https://openid.net/specs/openid-connect-core-1_0.html):
2424

25-
- [Authorization Code Grant](https://oauth.net/2/grant-types/authorization-code/)
26-
with [PKCE](https://oauth.net/2/pkce/)
27-
- [Resource Owner Password Credentials Grant](https://www.rfc-editor.org/rfc/rfc6749#section-1.3.3); however, read the [security concerns](docs/ropc.md) before using this flow
28-
- [Refresh Token Grant](https://oauth.net/2/grant-types/refresh-token/)
25+
- [Authorization Code Grant with Proof Key for Code Exchange (PKCE)](docs/protocols/authorization-code-grant-with-pkce.md)
26+
- [Authorization Code Grant](docs/protocols/authorization-code-grant.md)
27+
- [Resource Owner Password Credentials (ROPC) Grant](docs/protocols/resource-owner-password-credentials-grant.md)
28+
- [Refresh Token Grant](docs/protocols/refresh-token-grant.md)
29+
- [Silent Refresh Token in iframe Flow](docs/protocols/silent-refresh-token-in-iframe-flow.md)
2930

3031
## Table of Contents
3132

docs/index.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,25 @@ with you use to use the library:
1515
The remainder of this document will primarily focus on the
1616
[UserManager](classes/UserManager.html).
1717

18-
## UserManager
1918

20-
### Configuration
19+
# Principle of function
20+
To understand how to use this library see here:
21+
- [Authorization Code Grant with Proof Key for Code Exchange (PKCE)](https://github.com/authts/oidc-client-ts/blob/main/docs/protocols/authorization-code-grant-with-pkce.md)
22+
- [Authorization Code Grant](https://github.com/authts/oidc-client-ts/blob/main/docs/protocols/authorization-code-grant.md)
23+
- [Resource Owner Password Credentials (ROPC) Grant](https://github.com/authts/oidc-client-ts/blob/main/docs/protocols/resource-owner-password-credentials-grant.md)
24+
- [Refresh Token Grant](https://github.com/authts/oidc-client-ts/blob/main/docs/protocols/refresh-token-grant.md)
25+
- [Silent Refresh Token in iframe Flow](https://github.com/authts/oidc-client-ts/blob/main/docs/protocols/silent-refresh-token-in-iframe-flow.md)
2126

27+
# UserManager
28+
29+
## Configuration
2230
The [UserManager](classes/UserManager.html) constructor requires a settings
2331
object as a parameter:
2432

2533
- [UserManagerSettings](interfaces/UserManagerSettings.html) which extends
2634
- [OidcClientSettings](interfaces/OidcClientSettings.html)
2735

28-
#### Required settings
29-
36+
### Required settings
3037
- [authority](interfaces/OidcClientSettings.html#authority): The URL of the
3138
OIDC/OAuth2 provider.
3239
- [client_id](interfaces/OidcClientSettings.html#client_id): Your client
@@ -35,8 +42,7 @@ object as a parameter:
3542
URI of your client application to receive a response from the OIDC/OAuth2
3643
provider.
3744

38-
#### Provider settings if CORS not supported on OIDC/OAuth2 provider metadata endpoint
39-
45+
### Provider settings if CORS not supported on OIDC/OAuth2 provider metadata endpoint
4046
The [authority](interfaces/OidcClientSettings.html#authority) URL setting is
4147
used to make HTTP requests to discover more information about the OIDC/OAuth2
4248
provider and populate a `metadata` property on the settings. If the server does
@@ -52,8 +58,7 @@ provider:
5258
- [metadataSeed](interfaces/UserManagerSettings.html#metadataSeed) can be used
5359
to seed or add additional values to the results of the discovery request.
5460

55-
### Events
56-
61+
## Events
5762
The [UserManager](classes/UserManager.html) will raise various events about the
5863
user's session:
5964

@@ -71,11 +76,11 @@ mgr.events.addAccessTokenExpiring(function() {
7176
});
7277
```
7378

74-
## User
79+
# User
7580
The [User](classes/User.html) type is returned from the [UserManager](classes/UserManager.html)'s [getUser](classes/UserManager.html#getUser) API.
7681

7782

78-
## Logging
83+
# Logging
7984
The oidc-client-ts library supports logging. You can set a logger by assigning `Oidc.Log.logger` to anything that supports a `info`, `warn`, and `error` methods that accept a params array. By default, no logger is configured.
8085

8186
The `console` object in the browser supports these, so a common way to easily
@@ -89,7 +94,7 @@ Also, logging has levels so you can control the verbosity by calling
8994
`Oidc.Log.setLevel()` with one of `Oidc.Log.NONE`, `Oidc.Log.ERROR`,
9095
`Oidc.Log.WARN`, or `Oidc.Log.INFO`. The default is `Oidc.Log.INFO`.
9196

92-
## Provider specific settings
97+
# Provider specific settings
9398
Additional provider specific settings may be needed for a flawless operation:
9499

95100
**Amazon Cognito**
@@ -104,7 +109,7 @@ const mgr = new UserManager({
104109
```
105110

106111

107-
## Custom state in user object
112+
# Custom state in user object
108113
In case you would like to add additional data into the [User](classes/User.html) object, you can do so during the initial sign-in request.
109114

110115
```javascript
@@ -117,7 +122,7 @@ After successful sign-in the custom state is part of the [User](classes/User.htm
117122

118123
This custom state should not be confused with the URL state parameter. The latter is internally used to match against the authentication state object to finish the authentication process.
119124

120-
## Custom state in request url
125+
# Custom state in request url
121126
If you would like to encode a custom state string in the sign in request url, you can do so with the `url_state` parameter. You may want to do this in order to pass user state to the authentication server and/or a proxy and return that state as part of the response.
122127

123128
```javascript
@@ -128,13 +133,13 @@ mgr.signinRedirect({ url_state: 'custom url state' })
128133
The `url_state` will be appended to the opaque, unique value created by the library when sending the request. It should survive the round trip to your authentication server and will be part of the [User](classes/User.html#url_state) object as `url_state`.
129134

130135

131-
## Projects using oidc-client
136+
# Projects using oidc-client-ts
132137

133138
- [React context provider](https://github.com/authts/react-oidc-context)
134139
- [Angular sample](https://github.com/authts/sample-angular-oidc-client-ts)
135140
- [Chrome service worker](https://github.com/Alino/OIDC-client-ts-chromium-sample)
136141

137142

138-
## Training
143+
# Training
139144

140-
- [Securing Angular Apps with OpenID and OAuth2](https://noyes.me/ng-openid-oauth2)
145+
- [Securing Angular Apps with OpenID Connect and OAuth2](https://www.pluralsight.com/courses/openid-and-oauth2-securing-angular-apps)

docs/migration.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
## oidc-client-ts v2.4.0 → oidc-client-ts v3.0.0
22

3-
The API is largely backwards-compatible. The merge claims behavior has been improved.
3+
The API is largely backwards-compatible.
4+
5+
The "crypto-js" software library has been removed; the native crypto/crypto.subtle module built into the browser is instead used. All modern browser are expected to support it. If need to support older browsers stay with v2.4!
6+
7+
The behavior of merging claims has been improved.
48

59
### [OidcClientSettings](https://authts.github.io/oidc-client-ts/interfaces/OidcClientSettings.html)
610

711
- the following deprecated properties were **removed**:
8-
- `clockSkewInSeconds` unused since 2.0.0
9-
- `userInfoJwtIssuer` unused since 2.0.0
10-
- `refreshTokenCredentials` use `fetchRequestCredentials` since 2.1.0
12+
- `clockSkewInSeconds`
13+
- `userInfoJwtIssuer`
14+
- `refreshTokenCredentials` use `fetchRequestCredentials`
1115
- the `mergeClaims` has been replaced by `mergeClaimsStrategy`
12-
- if the previous behavior is needed `mergeClaimsStrategy: { array: "merge" }` can be used
16+
- if the previous behavior is required `mergeClaimsStrategy: { array: "merge" }` comes close to it
1317
- default of `response_mode` changed from `query` → `undefined`
1418

1519

docs/oidc-client-ts.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,6 @@ export class UserManager {
969969
readonly settings: UserManagerSettingsStore;
970970
// (undocumented)
971971
protected _signin(args: CreateSigninRequestArgs, handle: IWindow, verifySub?: string): Promise<User>;
972-
// (undocumented)
973972
signinCallback(url?: string): Promise<User | void>;
974973
// (undocumented)
975974
protected _signinEnd(url: string, verifySub?: string): Promise<User>;
@@ -984,7 +983,6 @@ export class UserManager {
984983
protected _signinStart(args: CreateSigninRequestArgs, handle: IWindow): Promise<NavigateResponse>;
985984
// (undocumented)
986985
protected _signout(args: CreateSignoutRequestArgs, handle: IWindow): Promise<SignoutResponse>;
987-
// (undocumented)
988986
signoutCallback(url?: string, keepOpen?: boolean): Promise<void>;
989987
// (undocumented)
990988
protected _signoutEnd(url: string): Promise<SignoutResponse>;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Authorization Code Grant with Proof Key for Code Exchange (PKCE)
2+
3+
The authorization code protocol is part of OAuth 2.0 (defined in [OAuth 2.0 RFC 7636](https://tools.ietf.org/html/rfc7636)). It involves the exchange of an authorization code for a token. This is the recommend authorization code flow in the [OAuth 2.1 draft](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-10).
4+
5+
6+
## Principle of function
7+
```mermaid
8+
---
9+
title: Authorization Code Grant with Proof Key for Code Exchange (PKCE)
10+
---
11+
sequenceDiagram
12+
actor User
13+
User->>App: Click sign-in link (1)
14+
15+
activate App
16+
Note left of App: Generate code_verifier and<br/>code_challenge
17+
App->>Identity Provider: Authorization code request & code_challenge (2)
18+
deactivate App
19+
20+
Identity Provider-->>User: Redirect to login/authorization prompt (3)
21+
User-->>Identity Provider: Authenticate (3)
22+
Identity Provider->>App: Authorization code (3)
23+
24+
activate App
25+
App->>Identity Provider: Authorization code & code verifier (4)
26+
Note right of Identity Provider: Validate authorization code &<br/>code_verifier
27+
Identity Provider->>App: Access token and ID token (4)
28+
deactivate App
29+
30+
App->>Your API: Request protected data with access token (5)
31+
```
32+
33+
1. The user clicks sign-in within the application.
34+
2. `signinRedirect()` or `signinPopup()` must be used to start the flow.
35+
3. The identity provider authenticates the user and stores the code_challenge and redirects the user back to the application with an authorization code.
36+
4. `signinCallback()` handles this callback by sending this authorization code and code_verifier to the identity provider and receiving in return the access token and ID token.
37+
5. The access token is now accessible via `getUser()?.access_token` and inserted into the requests to your protected API.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Authorization Code Grant
2+
3+
The authorization code protocol is part of OAuth 2.0 defined in ([OAuth 2.0 RFC 6749, section 4.1](https://tools.ietf.org/html/rfc6749#section-4.1)). It involves the exchange of an authorization code for a token.
4+
5+
**NOTE**<br/>
6+
It implies some security risks, so you should only use it after a security assessment.
7+
8+
9+
## Security concerns
10+
This flow can only be used for applications, which can protected the client secret:
11+
- **Native app**: Can not securely store the client secret, as its possible to decompile the application.
12+
- **Single-page app**: Can not securely store the client secret, as the full code is exposed in the users browser
13+
14+
In that scenarios [Authorization Code Grant with Proof Key for Code Exchange (PKCE)](authorization-code-flow-with-pkce.md) must be used.
15+
16+
17+
## Principle of function
18+
```mermaid
19+
---
20+
title: Authorization Code Grant
21+
---
22+
sequenceDiagram
23+
actor User
24+
User->>App: Click sign-in link (1)
25+
activate App
26+
App->>Identity Provider: Authorization code request (2)
27+
deactivate App
28+
29+
Identity Provider-->>User: Redirect to login/authorization prompt (3)
30+
User-->>Identity Provider: Authenticate (3)
31+
Identity Provider->>App: Authorization code (3)
32+
33+
activate App
34+
App->>Identity Provider: Authorization code & client secret (4)
35+
Note right of Identity Provider: Validate authorization code &<br/>client secret
36+
Identity Provider->>App: Access token and ID token (4)
37+
deactivate App
38+
39+
App->>Your API: Request protected data with access token (5)
40+
```
41+
42+
1. The user clicks sign-in within the application.
43+
2. `signinRedirect()` or `signinPopup()` must be used to start the flow.
44+
3. The identity provider authenticates the user and stores the code_challenge and redirects the user back to the application with an authorization code.
45+
4. `signinCallback()` handles this callback by sending this authorization code and client secret to the identity provider and receiving in return the access token and ID token.
46+
5. The access token is now accessible via `getUser()?.access_token` and inserted into the requests to your protected API.

docs/protocols/refresh-token-grant.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Refresh Token Grant
2+
3+
This protocol is part of OAuth 2.0 (defined in [OAuth 2.0 RFC 6749, section 1.5](https://tools.ietf.org/html/rfc6749#section-1.5)).
4+
The refresh token grant is used by clients to exchange a refresh token for an access token when the access token has expired.
5+
6+
7+
## Principle of function
8+
```mermaid
9+
---
10+
title: Refresh Token Grant
11+
---
12+
sequenceDiagram
13+
App->>Identity Provider: Request new access token with refresh token (1)
14+
activate App
15+
Note right of Identity Provider: Validate refresh token
16+
Identity Provider->>App: Access token and optional refresh token (1)
17+
deactivate App
18+
19+
App->>Your API: Request protected data with refreshed access token (2)
20+
```
21+
22+
1. `signinSilent()` must be used to start the flow.
23+
2. The refreshed access token is now accessible via `getUser()?.access_token` and inserted into the requests to your protected API.
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1-
## Security concerns on Resource Owner Password Credentials flow
1+
# Resource Owner Password Credentials (ROPC) Grant
22

3-
This OAuth 2.0 flow implies some security risks, so you should only use it after a security assesment.
3+
This protocol is part of OAuth 2.0 (defined in [OAuth 2.0 RFC 6749, section 4.3](https://www.rfc-editor.org/rfc/rfc6749#section-4.3)).
44

5-
To start with, this flow is not part of the [Open ID Connect standard](https://openid.net/specs/openid-connect-core-1_0.html). Furthermore, although it is part of [OAuth 2.0](https://www.rfc-editor.org/rfc/rfc6749#section-4.3), it has been removed in the [OAuth 2.1 draft](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-10), and there are good reasons for this:
5+
**NOTE**<br/>
6+
It implies some security risks, so you should only use it after a security assessment.
7+
8+
9+
## Security concerns
10+
11+
To start with, this flow is not part of the [Open ID Connect standard](https://openid.net/specs/openid-connect-core-1_0.html). Furthermore, although it is part of OAuth 2.0, it has been removed in the [OAuth 2.1 draft](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-10), and there are good reasons for this:
612

713
* When using this flow, the credentials of the user are exposed to the client application. The RFC mandates that ["the client application MUST discard the credentials once the access token has been obtained"](https://www.rfc-editor.org/rfc/rfc6749#section-4.3.1), but there is no technical way for the Identity Provider / Authorization Server to enforce this point. This flow MUST NOT be allowed unless the Client can be enforced to fulfill this requirement through other means (probably because both are under the same security domain, probably the same organization or similar constraints). This point is covered [several](https://www.rfc-editor.org/rfc/rfc6749#section-1.3.3) [times](https://www.rfc-editor.org/rfc/rfc6749#section-4.3) during the RFC and by some IdP implementations, such as [Auth0](https://auth0.com/docs/get-started/authentication-and-authorization-flow/resource-owner-password-flow) or [Keycloak](https://www.keycloak.org/docs/latest/securing_apps/#_resource_owner_password_credentials_flow), but it is sometimes quickly ignored by some developers.
814

9-
* Even if the previous point is covered, this flow is increasing the attack surface of the system. For example, if the Client Application is compromised (maybe through an XSS attack, for example), the credentials of the user are exposed, so the attacker have access to other applications accessible by the user. In comparison, for example, in the Authorization Code Flow if the Client Application is equaly compromised only the access token is exposed, usually meaning that only the given application has been compromised. A different way to see this is: usually the IdP/Authorization Server are strongly protected, and the Client Applications are allowed lower levels of security auditing... but when using this flow, if any of them is exposed, the user credentials are exposed; so both of them should be equaly treated regarding security.
15+
* Even if the previous point is covered, this flow is increasing the attack surface of the system. For example, if the Client Application is compromised (maybe through an XSS attack, for example), the credentials of the user are exposed, so the attacker have access to other applications accessible by the user. In comparison, for example, in the Authorization Code Flow if the Client Application is equally compromised only the access token is exposed, usually meaning that only the given application has been compromised. A different way to see this is: usually the IdP/Authorization Server are strongly protected, and the Client Applications are allowed lower levels of security auditing... but when using this flow, if any of them is exposed, the user credentials are exposed; so both of them should be equally treated regarding security.
1016

1117
Therefor, this flow MUST NOT be used as a replacement for the Authorization Code flow. This flow can only be seen as a replacement of classic form-based user/password authentication directly in the application.
1218

13-
Then, why are we adding this support in `oidc-client-ts`? Well... form-based user/password authentication is actually widely used in the industry, and using a standard IdP as authenticator for this architecture has some benefits (other things, such as password expiration, user management backoffice, etc are provided for free by the IdP). So this flow can be an easy help in this scenario. But you MUST NOT use this flow believing that you are having all the security benefits of OpenId Connect or OAuth; you are not.
19+
Then, why are we adding this support in `oidc-client-ts`? Well... form-based user/password authentication is actually widely used in the industry, and using a standard IdP as authenticator for this architecture has some benefits (other things, such as password expiration, user management back-office, etc are provided for free by the IdP). So this flow can be an easy help in this scenario. But you MUST NOT use this flow believing that you are having all the security benefits of OpenId Connect or OAuth; you are not.
20+
21+
22+
## Principle of function
23+
```mermaid
24+
---
25+
title: Resource Owner Password Credentials (ROPC) Grant
26+
---
27+
sequenceDiagram
28+
actor User
29+
User->>App: Click sign-in link (1)
30+
31+
activate App
32+
App->>Identity Provider: Authenticate with username and password (2)
33+
Note right of Identity Provider: Validate username &<br/>password
34+
Identity Provider->>App: Access token and ID token (2)
35+
deactivate App
36+
37+
App->>Your API: Request protected data with access token (3)
38+
```
39+
40+
1. The user clicks sign-in within the application.
41+
2. `signinResourceOwnerCredentials()` must be used to start the flow.
42+
3. The access token is now accessible via `getUser()?.access_token` and inserted into the requests to your protected API.

0 commit comments

Comments
 (0)