|
| 1 | +--- |
| 2 | +title: Library OAuth2 authentication APIs |
| 3 | +description: Describes the Embedding SDK APIs that is available for Library OAuth2 authentication. |
| 4 | +--- |
| 5 | + |
| 6 | +To simplify the [Library OAuth2](https://doc-dev.microstrategy.com/producthelp/2021/Workstation/en-us/Content/oauth2.htm) login workflow, we expose new APIs |
| 7 | + |
| 8 | +`microstrategy.auth.oauthLogin(serverUrl, options)` |
| 9 | + |
| 10 | +`microstrategy.auth.oauthRefreshToken(serverUrl, options)` |
| 11 | + |
| 12 | +The details of those new APIs are as below: |
| 13 | + |
| 14 | +### `microstrategy.auth.oauthLogin(serverUrl, options)` |
| 15 | + |
| 16 | +#### Description |
| 17 | + |
| 18 | +This API can be used to start the Library OAuth2 login workflow on the embedding scenario. |
| 19 | + |
| 20 | +#### Input Parameters |
| 21 | + |
| 22 | +- `serverUrl` (`string`): |
| 23 | + The URL of a Library server that has been successfully configured with OAuth2. |
| 24 | + |
| 25 | +- `options` (`OAuth2LoginOptions`): |
| 26 | + An object containing the following fields: |
| 27 | + |
| 28 | + - `clientId` (`string`): |
| 29 | + The client identifier issued by the authorization server during OAuth2 client registration. |
| 30 | + - `clientSecret` (`string`, optional): |
| 31 | + The client secret corresponding to the clientId, used to authenticate the client with the authorization server. |
| 32 | + |
| 33 | + If provided together with `requireRefreshToken: true`, a refresh token will be issued as part of the login response. |
| 34 | + |
| 35 | + - `requireRefreshToken` (`boolean`, optional): |
| 36 | + Specifies whether a refresh token should be requested during login. |
| 37 | + |
| 38 | + Only takes effect if `clientSecret` is provided. |
| 39 | + |
| 40 | + If true, both an access token and a refresh token will be returned. |
| 41 | + |
| 42 | + If false (or omitted), only an access token will be returned. |
| 43 | + |
| 44 | +#### Return type |
| 45 | + |
| 46 | +Returns a `Promise` that resolves to an object containing the following fields: |
| 47 | + |
| 48 | +```ts |
| 49 | +{ |
| 50 | + accessToken: string; |
| 51 | + refreshToken?: string; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +- accessToken: The OAuth2 access token for authenticating subsequent requests. |
| 56 | +- refreshToken (optional): The refresh token that can be used to obtain a new access token when the |
| 57 | + current one expires. |
| 58 | + |
| 59 | +#### Example |
| 60 | + |
| 61 | +```js |
| 62 | +microstrategy.embeddingContexts.embedLibraryPage({ |
| 63 | + serverUrl: "{YOUR_LIBRARY_SERVER_URL}", |
| 64 | + placeholder: document.getElementById("LibraryHomePageContainer"), |
| 65 | + enableCustomAuthentication: true, |
| 66 | + customAuthenticationType: microstrategy.dossier.CustomAuthenticationType.AUTH_TOKEN, |
| 67 | + getLoginToken: async () => { |
| 68 | + const data = await microstrategy.auth.oauthLogin("{YOUR_LIBRARY_SERVER_URL}", { |
| 69 | + clientId: "{YOUR_OAUTH2_CLIENT_ID}", |
| 70 | + clientSecret: "{YOUR_OAUTH2_CLIENT_SECRET}", |
| 71 | + requireRefreshToken: true, |
| 72 | + }); |
| 73 | + return data.accessToken; |
| 74 | + }, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +### `microstrategy.auth.oauthRefreshToken(serverUrl, options)` |
| 79 | + |
| 80 | +#### Description |
| 81 | + |
| 82 | +This API allows obtaining a new access token using a previously acquired refresh token. |
| 83 | + |
| 84 | +#### Input Parameters |
| 85 | + |
| 86 | +- `serverUrl` (`string`): |
| 87 | + The URL of a Library server that has been successfully configured with OAuth2. |
| 88 | + |
| 89 | +- `options` (`OAuth2RefreshTokenOptions`): |
| 90 | + An object containing the following fields: |
| 91 | + |
| 92 | + - `clientId` (`string`): |
| 93 | + The client identifier issued by the authorization server during OAuth2 client registration. |
| 94 | + |
| 95 | + - `clientSecret` (`string`): |
| 96 | + The client secret corresponding to the clientId, used to authenticate the client with the authorization server. |
| 97 | + |
| 98 | + - `refreshToken` (`string`): |
| 99 | + The refresh token that was previously obtained during login. |
| 100 | + |
| 101 | +#### Return type |
| 102 | + |
| 103 | +Returns a `Promise` that resolves to an object containing the following fields: |
| 104 | + |
| 105 | +```ts |
| 106 | +{ |
| 107 | + accessToken: string; |
| 108 | + refreshToken: string; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +- accessToken: The OAuth2 access token for authenticating subsequent requests. |
| 113 | +- refreshToken: The refresh token that can be used to obtain a new access token when the |
| 114 | + current one expires. |
| 115 | + |
| 116 | +#### Example |
| 117 | + |
| 118 | +```js |
| 119 | +const data = await microstrategy.auth.oauthRefreshToken("{YOUR_LIBRARY_SERVER_URL}", { |
| 120 | + clientId: "{YOUR_OAUTH2_CLIENT_ID}", |
| 121 | + clientSecret: "{YOUR_OAUTH2_CLIENT_SECRET}", |
| 122 | + refreshToken: "{YOUR_REFRESH_TOKEN}", |
| 123 | +}); |
| 124 | +const { accessToken } = data; |
| 125 | +const { refreshToken } = data; |
| 126 | +``` |
| 127 | + |
| 128 | +#### API errors |
| 129 | + |
| 130 | +This API would have the wrong behavior or report an error in the cases below: |
| 131 | + |
| 132 | +- When `serverUrl` is not a Library server that supports OAuth2 login. |
| 133 | +- If you receive a `HTTP Status 400 - Bad Request` response, ensure that the redirectUrl is [correctly configured](https://doc-dev.microstrategy.com/producthelp/2021/Workstation/en-us/Content/config_oauth2.htm). The expected format for the redirectUrl is: `YOUR_LIBRARY_SERVER_URL/static/embedding/auth-end.html?origin=YOUR_EMBEDDING_ORIGIN` and Make sure to replace: |
| 134 | + |
| 135 | + - `YOUR_LIBRARY_SERVER_URL` with the actual Library server URL. |
| 136 | + - `YOUR_EMBEDDING_ORIGIN` with the origin of the embedding application. For example, if your embedding page URL is `https://example.com:8443/path/demo.html`, then the origin is `https://example.com:8443`. |
| 137 | + |
| 138 | +- When the client application can't get the Strategy login window because of |
| 139 | + [COOP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy) |
| 140 | + setting is too strict. |
0 commit comments