|
1 | 1 | import { oidc } from '@forgerock/oidc-client'; |
2 | 2 |
|
3 | | -async function app() { |
4 | | - const oidcClient = await oidc({ |
5 | | - config: { |
6 | | - clientId: 'WebOAuthClient', |
7 | | - redirectUri: 'http://localhost:8443/', |
8 | | - scope: 'openid', |
9 | | - serverConfig: { |
10 | | - wellknown: |
11 | | - 'https://openam-sdks.forgeblocks.com/am/oauth2/alpha/.well-known/openid-configuration', |
12 | | - }, |
| 3 | +// const pingAmConfig = { |
| 4 | +// config: { |
| 5 | +// clientId: 'WebOAuthClient', |
| 6 | +// redirectUri: 'http://localhost:8443/', |
| 7 | +// scope: 'openid', |
| 8 | +// serverConfig: { |
| 9 | +// wellknown: |
| 10 | +// 'https://openam-sdks.forgeblocks.com/am/oauth2/alpha/.well-known/openid-configuration', |
| 11 | +// }, |
| 12 | +// }, |
| 13 | +// }; |
| 14 | +const pingOneConfig = { |
| 15 | + config: { |
| 16 | + clientId: '654b14e2-7cc5-4977-8104-c4113e43c537', |
| 17 | + redirectUri: 'http://localhost:8443/', |
| 18 | + scope: 'openid', |
| 19 | + serverConfig: { |
| 20 | + wellknown: |
| 21 | + 'https://auth.pingone.ca/02fb4743-189a-4bc7-9d6c-a919edfe6447/as/.well-known/openid-configuration', |
13 | 22 | }, |
14 | | - }); |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +async function app() { |
| 27 | + const oidcClient = await oidc(pingOneConfig); |
15 | 28 |
|
16 | 29 | // create object from URL query parameters |
17 | 30 | const urlParams = new URLSearchParams(window.location.search); |
18 | 31 | const code = urlParams.get('code'); |
19 | | - // const state = urlParams.get('state'); |
| 32 | + const state = urlParams.get('state'); |
20 | 33 | // get error and error_description if they exist |
21 | 34 | const error = urlParams.get('error'); |
22 | 35 | // const errorDescription = urlParams.get('error_description'); |
23 | 36 |
|
| 37 | + // Handle background authorization flow |
24 | 38 | if (!code && !error) { |
25 | 39 | const response = await oidcClient.authorize.background(); |
26 | 40 |
|
27 | 41 | if ('error' in response) { |
28 | 42 | console.error('Authorization Error:', response); |
29 | | - // window.location.assign(response.redirectUrl); |
| 43 | + |
| 44 | + if (response.redirectUrl) { |
| 45 | + window.location.assign(response.redirectUrl); |
| 46 | + } else { |
| 47 | + console.log('Authorization failed with no ability to redirect:', response); |
| 48 | + } |
30 | 49 | return; |
| 50 | + |
| 51 | + // Handle success response from background authorization |
31 | 52 | } else if ('code' in response) { |
32 | 53 | console.log('Authorization Code:', response.code); |
| 54 | + const tokenResponse = await oidcClient.token.exchange(response.code, response.state); |
| 55 | + if ('error' in response) { |
| 56 | + console.error('Token Exchange Error:', tokenResponse); |
| 57 | + } else { |
| 58 | + console.log('Token Exchange Response:', tokenResponse); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Handle the user redirecting after authentication |
| 63 | + } else if (code && state) { |
| 64 | + const response = await oidcClient.token.exchange(code, state); |
| 65 | + |
| 66 | + if ('error' in response) { |
| 67 | + console.error('Token Exchange Error:', response); |
| 68 | + } else { |
| 69 | + console.log('Token Exchange Response:', response); |
33 | 70 | } |
34 | 71 | } |
35 | 72 | } |
|
0 commit comments