mirror of
https://github.com/hanzoai/openid-client.git
synced 2026-09-24 17:01:10 +00:00
82 lines
2.6 KiB
Diff
82 lines
2.6 KiB
Diff
diff --git a/examples/oauth.ts b/examples/oidc.ts
|
|
index dde3bbc..58ee3ef 100644
|
|
--- a/examples/oauth.ts
|
|
+++ b/examples/oidc.ts
|
|
@@ -18,33 +18,33 @@ let config = await client.discovery(server, clientId, clientSecret)
|
|
|
|
let code_challenge_method = 'S256'
|
|
/**
|
|
- * The following (code_verifier and potentially state) MUST be generated for
|
|
+ * The following (code_verifier and potentially nonce) MUST be generated for
|
|
* every redirect to the authorization_endpoint. You must store the
|
|
- * code_verifier and state in the end-user session such that it can be recovered
|
|
+ * code_verifier and nonce in the end-user session such that it can be recovered
|
|
* as the user gets redirected from the authorization server back to your
|
|
* application.
|
|
*/
|
|
let code_verifier = client.randomPKCECodeVerifier()
|
|
let code_challenge = await client.calculatePKCECodeChallenge(code_verifier)
|
|
-let state!: string
|
|
+let nonce!: string
|
|
|
|
{
|
|
// redirect user to as.authorization_endpoint
|
|
let parameters: Record<string, string> = {
|
|
redirect_uri,
|
|
- scope: 'api:read',
|
|
+ scope: 'openid email',
|
|
code_challenge,
|
|
code_challenge_method,
|
|
}
|
|
|
|
/**
|
|
- * We cannot be sure the AS supports PKCE so we're going to use state too. Use
|
|
+ * We cannot be sure the AS supports PKCE so we're going to use nonce too. Use
|
|
* of PKCE is backwards compatible even if the AS doesn't support it which is
|
|
* why we're using it regardless.
|
|
*/
|
|
if (!config.serverMetadata().supportsPKCE()) {
|
|
- state = client.randomState()
|
|
- parameters.state = state
|
|
+ nonce = client.randomNonce()
|
|
+ parameters.nonce = nonce
|
|
}
|
|
|
|
let redirectTo = client.buildAuthorizationUrl(config, parameters)
|
|
@@ -55,26 +55,26 @@ let state!: string
|
|
|
|
// one eternity later, the user lands back on the redirect_uri
|
|
// Authorization Code Grant
|
|
+let sub: string
|
|
let access_token: string
|
|
{
|
|
let currentUrl: URL = getCurrentUrl()
|
|
let tokens = await client.authorizationCodeGrant(config, currentUrl, {
|
|
pkceCodeVerifier: code_verifier,
|
|
- expectedState: state,
|
|
+ expectedNonce: nonce,
|
|
+ idTokenExpected: true,
|
|
})
|
|
|
|
console.log('Token Endpoint Response', tokens)
|
|
;({ access_token } = tokens)
|
|
+ let claims = tokens.claims()!
|
|
+ console.log('ID Token Claims', claims)
|
|
+ ;({ sub } = claims)
|
|
}
|
|
|
|
-// Protected Resource Request
|
|
+// UserInfo Request
|
|
{
|
|
- let protectedResource = await client.fetchProtectedResource(
|
|
- config,
|
|
- access_token,
|
|
- new URL('https://rs.example.com/api'),
|
|
- 'GET',
|
|
- )
|
|
+ let userInfo = await client.fetchUserInfo(config, access_token, sub)
|
|
|
|
- console.log('Protected Resource Response', await protectedResource.json())
|
|
+ console.log('UserInfo Response', userInfo)
|
|
}
|