-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathfapi2.diff
172 lines (161 loc) · 5.9 KB
/
fapi2.diff
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
diff --git a/examples/oauth.ts b/examples/fapi2.ts
index d55e62d..bba1f24 100644
--- a/examples/oauth.ts
+++ b/examples/fapi2.ts
@@ -9,12 +9,22 @@ let algorithm!:
| 'oidc' /* For .well-known/openid-configuration discovery */
| undefined /* Defaults to 'oidc' */
let client_id!: string
-let client_secret!: string
/**
* Value used in the authorization request as redirect_uri pre-registered at the Authorization
* Server.
*/
let redirect_uri!: string
+/**
+ * In order to take full advantage of DPoP you shall generate a random private key for every
+ * session. In the browser environment you shall use IndexedDB to persist the generated
+ * CryptoKeyPair.
+ */
+let DPoPKeys!: oauth.CryptoKeyPair
+/**
+ * A key that the client has pre-registered at the Authorization Server for use with Private Key JWT
+ * client authentication method.
+ */
+let clientPrivateKey!: oauth.CryptoKey
// End of prerequisites
@@ -23,36 +33,54 @@ const as = await oauth
.then((response) => oauth.processDiscoveryResponse(issuer, response))
const client: oauth.Client = { client_id }
-const clientAuth = oauth.ClientSecretPost(client_secret)
+const clientAuth = oauth.PrivateKeyJwt(clientPrivateKey)
+const DPoP = oauth.DPoP(client, DPoPKeys)
const code_challenge_method = 'S256'
/**
* The following MUST be generated for every redirect to the authorization_endpoint. You must store
- * the 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.
+ * the code_verifier in the end-user session such that it can be recovered as the user gets
+ * redirected from the authorization server back to your application.
*/
const code_verifier = oauth.generateRandomCodeVerifier()
const code_challenge = await oauth.calculatePKCECodeChallenge(code_verifier)
-let state: string | undefined
+
+// Pushed Authorization Request & Response (PAR)
+let request_uri: string
+{
+ const params = new URLSearchParams()
+ params.set('client_id', client.client_id)
+ params.set('code_challenge', code_challenge)
+ params.set('code_challenge_method', code_challenge_method)
+ params.set('redirect_uri', redirect_uri)
+ params.set('response_type', 'code')
+ params.set('scope', 'api:read')
+
+ const pushedAuthorizationRequest = () =>
+ oauth.pushedAuthorizationRequest(as, client, clientAuth, params, {
+ DPoP,
+ })
+ let response = await pushedAuthorizationRequest()
+
+ const processPushedAuthorizationResponse = () =>
+ oauth.processPushedAuthorizationResponse(as, client, response)
+ let result = await processPushedAuthorizationResponse().catch(async (err) => {
+ if (oauth.isDPoPNonceError(err)) {
+ // the AS-signalled nonce is now cached, retrying
+ response = await pushedAuthorizationRequest()
+ return processPushedAuthorizationResponse()
+ }
+ throw err
+ })
+
+ ;({ request_uri } = result)
+}
{
// redirect user to as.authorization_endpoint
const authorizationUrl = new URL(as.authorization_endpoint!)
authorizationUrl.searchParams.set('client_id', client.client_id)
- authorizationUrl.searchParams.set('redirect_uri', redirect_uri)
- authorizationUrl.searchParams.set('response_type', 'code')
- authorizationUrl.searchParams.set('scope', 'api:read')
- authorizationUrl.searchParams.set('code_challenge', code_challenge)
- authorizationUrl.searchParams.set('code_challenge_method', code_challenge_method)
-
- /**
- * We cannot be sure the AS supports PKCE so we're going to use state too. Use of PKCE is
- * backwards compatible even if the AS doesn't support it which is why we're using it regardless.
- */
- if (as.code_challenge_methods_supported?.includes('S256') !== true) {
- state = oauth.generateRandomState()
- authorizationUrl.searchParams.set('state', state)
- }
+ authorizationUrl.searchParams.set('request_uri', request_uri)
// now redirect the user to authorizationUrl.href
}
@@ -62,18 +90,32 @@ let state: string | undefined
let access_token: string
{
const currentUrl: URL = getCurrentUrl()
- const params = oauth.validateAuthResponse(as, client, currentUrl, state)
+ const params = oauth.validateAuthResponse(as, client, currentUrl)
+
+ const authorizationCodeGrantRequest = () =>
+ oauth.authorizationCodeGrantRequest(
+ as,
+ client,
+ clientAuth,
+ params,
+ redirect_uri,
+ code_verifier,
+ { DPoP },
+ )
+
+ let response = await authorizationCodeGrantRequest()
- const response = await oauth.authorizationCodeGrantRequest(
- as,
- client,
- clientAuth,
- params,
- redirect_uri,
- code_verifier,
- )
+ const processAuthorizationCodeResponse = () =>
+ oauth.processAuthorizationCodeResponse(as, client, response)
- const result = await oauth.processAuthorizationCodeResponse(as, client, response)
+ let result = await processAuthorizationCodeResponse().catch(async (err) => {
+ if (oauth.isDPoPNonceError(err)) {
+ // the AS-signalled nonce is now cached, retrying
+ response = await authorizationCodeGrantRequest()
+ return processAuthorizationCodeResponse()
+ }
+ throw err
+ })
console.log('Access Token Response', result)
;({ access_token } = result)
@@ -81,11 +123,22 @@ let access_token: string
// Protected Resource Request
{
- const response = await oauth.protectedResourceRequest(
- access_token,
- 'GET',
- new URL('https://rs.example.com/api'),
- )
+ const protectedResourceRequest = () =>
+ oauth.protectedResourceRequest(
+ access_token,
+ 'GET',
+ new URL('https://rs.example.com/api'),
+ undefined,
+ undefined,
+ { DPoP },
+ )
+ let response = await protectedResourceRequest().catch((err) => {
+ if (oauth.isDPoPNonceError(err)) {
+ // the RS-signalled nonce is now cached, retrying
+ return protectedResourceRequest()
+ }
+ throw err
+ })
console.log('Protected Resource Response', await response.json())
}