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