|
| 1 | +import * as client from 'openid-client' |
| 2 | + |
| 3 | +// Prerequisites |
| 4 | + |
| 5 | +let getCurrentUrl!: (...args: any) => URL |
| 6 | +let server!: URL // Authorization server's Issuer Identifier URL |
| 7 | +let clientId!: string |
| 8 | +let clientSecret!: string |
| 9 | +/** |
| 10 | + * Value used in the authorization request as redirect_uri pre-registered at the |
| 11 | + * Authorization Server. |
| 12 | + */ |
| 13 | +let redirect_uri!: string |
| 14 | + |
| 15 | +/** |
| 16 | + * In order to take full advantage of DPoP you shall generate a random key pair |
| 17 | + * for every session. In the browser environment you shall use IndexedDB to |
| 18 | + * persist the generated CryptoKeyPair. |
| 19 | + */ |
| 20 | +let DPoPKeys!: client.CryptoKeyPair |
| 21 | + |
| 22 | +// End of prerequisites |
| 23 | + |
| 24 | +let config = await client.discovery(server, clientId, clientSecret) |
| 25 | + |
| 26 | +let DPoP = client.getDPoPHandle(config, DPoPKeys) |
| 27 | + |
| 28 | +let code_challenge_method = 'S256' |
| 29 | +/** |
| 30 | + * The following (code_verifier and potentially state) MUST be generated for |
| 31 | + * every redirect to the authorization_endpoint. You must store the |
| 32 | + * code_verifier and state in the end-user session such that it can be recovered |
| 33 | + * as the user gets redirected from the authorization server back to your |
| 34 | + * application. |
| 35 | + */ |
| 36 | +let code_verifier = client.randomPKCECodeVerifier() |
| 37 | +let code_challenge = await client.calculatePKCECodeChallenge(code_verifier) |
| 38 | +let state!: string |
| 39 | + |
| 40 | +{ |
| 41 | + // redirect user to as.authorization_endpoint |
| 42 | + let parameters: Record<string, string> = { |
| 43 | + redirect_uri, |
| 44 | + scope: 'api:read', |
| 45 | + code_challenge, |
| 46 | + code_challenge_method, |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * We cannot be sure the AS supports PKCE so we're going to use state too. Use |
| 51 | + * of PKCE is backwards compatible even if the AS doesn't support it which is |
| 52 | + * why we're using it regardless. |
| 53 | + */ |
| 54 | + if (!config.serverMetadata().supportsPKCE()) { |
| 55 | + state = client.randomState() |
| 56 | + parameters.state = state |
| 57 | + } |
| 58 | + |
| 59 | + let redirectTo = client.buildAuthorizationUrl(config, parameters) |
| 60 | + |
| 61 | + console.log('redirecting to', redirectTo.href) |
| 62 | + // now redirect the user to redirectTo.href |
| 63 | +} |
| 64 | + |
| 65 | +// one eternity later, the user lands back on the redirect_uri |
| 66 | +// Authorization Code Grant |
| 67 | +let access_token: string |
| 68 | +{ |
| 69 | + let currentUrl: URL = getCurrentUrl() |
| 70 | + let tokens = await client.authorizationCodeGrant( |
| 71 | + config, |
| 72 | + currentUrl, |
| 73 | + { |
| 74 | + pkceCodeVerifier: code_verifier, |
| 75 | + expectedState: state, |
| 76 | + }, |
| 77 | + undefined, |
| 78 | + { DPoP }, |
| 79 | + ) |
| 80 | + |
| 81 | + console.log('Token Endpoint Response', tokens) |
| 82 | + ;({ access_token } = tokens) |
| 83 | +} |
| 84 | + |
| 85 | +// Protected Resource Request |
| 86 | +{ |
| 87 | + let protectedResource = await client.fetchProtectedResource( |
| 88 | + config, |
| 89 | + access_token, |
| 90 | + new URL('https://rs.example.com/api'), |
| 91 | + 'GET', |
| 92 | + undefined, |
| 93 | + undefined, |
| 94 | + { DPoP }, |
| 95 | + ) |
| 96 | + |
| 97 | + console.log('Protected Resource Response', await protectedResource.json()) |
| 98 | +} |
0 commit comments