|
| 1 | +import * as oauth from 'oauth4webapi' |
| 2 | + |
| 3 | +// Prerequisites |
| 4 | + |
| 5 | +let issuer!: URL // Authorization server's Issuer Identifier URL |
| 6 | +let algorithm!: |
| 7 | + | 'oauth2' /* For .well-known/oauth-authorization-server discovery */ |
| 8 | + | 'oidc' /* For .well-known/openid-configuration discovery */ |
| 9 | + | undefined /* Defaults to 'oidc' */ |
| 10 | +let client_id!: string |
| 11 | + |
| 12 | +// End of prerequisites |
| 13 | + |
| 14 | +const as = await oauth |
| 15 | + .discoveryRequest(issuer, { algorithm }) |
| 16 | + .then((response) => oauth.processDiscoveryResponse(issuer, response)) |
| 17 | + |
| 18 | +const client: oauth.Client = { client_id } |
| 19 | +const clientAuth = oauth.None() |
| 20 | + |
| 21 | +let auth_req_id: string |
| 22 | +let interval: number |
| 23 | + |
| 24 | +// Backchannel Authentication Request & Response |
| 25 | +{ |
| 26 | + const parameters = new URLSearchParams() |
| 27 | + parameters.set('scope', 'openid') |
| 28 | + |
| 29 | + const response = await oauth.backchannelAuthenticationRequest(as, client, clientAuth, parameters) |
| 30 | + |
| 31 | + const result = await oauth.processBackchannelAuthenticationResponse(as, client, response) |
| 32 | + |
| 33 | + console.log('Backchannel Authentication Response', result) |
| 34 | + ;({ auth_req_id, interval = 5 } = result) |
| 35 | +} |
| 36 | + |
| 37 | +// Backchannel Authentication Grant Request & Response |
| 38 | +let access_token: string |
| 39 | +let sub: string |
| 40 | +{ |
| 41 | + let success: oauth.TokenEndpointResponse | undefined = undefined |
| 42 | + function wait() { |
| 43 | + return new Promise((resolve) => { |
| 44 | + setTimeout(resolve, interval * 1000) |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + while (success === undefined) { |
| 49 | + await wait() |
| 50 | + const response = await oauth.backchannelAuthenticationGrantRequest( |
| 51 | + as, |
| 52 | + client, |
| 53 | + clientAuth, |
| 54 | + auth_req_id, |
| 55 | + ) |
| 56 | + |
| 57 | + success = await oauth |
| 58 | + .processBackchannelAuthenticationGrantResponse(as, client, response) |
| 59 | + .catch((err) => { |
| 60 | + if (err instanceof oauth.ResponseBodyError) { |
| 61 | + switch (err.error) { |
| 62 | + case 'slow_down': |
| 63 | + interval += 5 |
| 64 | + case 'authorization_pending': |
| 65 | + return undefined |
| 66 | + } |
| 67 | + } |
| 68 | + throw err |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + console.log('Access Token Response', success) |
| 73 | + ;({ access_token } = success) |
| 74 | + const claims = oauth.getValidatedIdTokenClaims(success)! |
| 75 | + console.log('ID Token Claims', claims) |
| 76 | + ;({ sub } = claims) |
| 77 | +} |
| 78 | + |
| 79 | +// UserInfo Request |
| 80 | +{ |
| 81 | + const response = await oauth.userInfoRequest(as, client, access_token) |
| 82 | + |
| 83 | + const result = await oauth.processUserInfoResponse(as, client, sub, response) |
| 84 | + console.log('UserInfo Response', result) |
| 85 | +} |
0 commit comments