summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/token-endpoint.scm
blob: a10c84303d11938f58640ea93e02b8317f2cf0db (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
;; disfluid, implementation of the Solid specification
;; Copyright (C) 2020, 2021  Vivien Kraus

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.

;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (webid-oidc token-endpoint)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc authorization-code)
  #:use-module (webid-oidc dpop-proof)
  #:use-module (webid-oidc jws)
  #:use-module (webid-oidc jwk)
  #:use-module (webid-oidc oidc-id-token)
  #:use-module (webid-oidc access-token)
  #:use-module (webid-oidc web-i18n)
  #:use-module ((webid-oidc parameters) #:prefix p:)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module ((webid-oidc refresh-token) #:prefix refresh:)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web uri)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 control)
  #:use-module (ice-9 exceptions)
  #:use-module (srfi srfi-19)
  #:use-module (rnrs bytevectors)
  #:use-module (sxml simple)
  #:use-module (sxml match)
  #:use-module (oop goops)
  #:duplicates (merge-generics)
  #:declarative? #t
  #:export
  (
   &unsupported-grant-type
   make-unsupported-grant-type
   unsupported-grant-type?
   unsupported-grant-type-grant-type

   &no-authorization-code
   make-no-authorization-code
   no-authorization-code?

   &no-refresh-token
   make-no-refresh-token
   no-refresh-token?

   make-token-endpoint
   ))

(define-exception-type
  &unsupported-grant-type
  &external-error
  make-unsupported-grant-type
  unsupported-grant-type?
  (grant-type unsupported-grant-type-grant-type))

(define-exception-type
  &no-authorization-code
  &external-error
  make-no-authorization-code
  no-authorization-code?)

(define-exception-type
  &no-refresh-token
  &external-error
  make-no-refresh-token
  no-refresh-token?)

(define (try-handle-web-failure thunk)
  (call/ec
   (lambda (return)
     (with-exception-handler
         (lambda (error)
           (unless (or (unsupported-grant-type? error)
                       (no-authorization-code? error)
                       (no-refresh-token? error)
                       (refresh:invalid-refresh-token? error)
                       (invalid-authorization-code? error))
             (let ((final-message
                    (if (exception-with-message? error)
                        (format #f (G_ "while handling web failure for the token endpoint: ~a")
                                (exception-message error))
                        (format #f (G_ "an error happened during the token endpoint failure handling")))))
               (raise-exception
                (make-exception
                 (make-exception-with-message final-message)
                 error))))
           (cond
            ((refresh:invalid-refresh-token? error)
             (return
              (build-response
               #:code 403
               #:reason-phrase (G_ "reason-phrase|Forbidden")
               #:headers '((content-type application/xhtml-xml)))
              (call-with-output-string
                (lambda (port)
                  (sxml->xml
                   `(*TOP*
                     (*PI* xml "version=\"1.0\" encoding=\"utf-8\"")
                     (html (@ (xmlns "http://www.w3.org/1999/xhtml")
                              (xml:lang ,(W_ "xml-lang|en")))
                           (body
                            ,(sxml-match
                              (xml->sxml
                               (W_ (format #f "<h1>Invalid refresh token</h1>")))
                              ((*TOP* ,title) title))
                            ,(sxml-match
                              (xml->sxml
                               (W_ (format #f "<p>The refresh token you sent is invalid, or it is already bound to another key.</p>")))
                              ((*TOP* ,p) p))
                            ,@(if (message-for-the-user? error)
                                  (user-message error)
                                  '()))))
                   port)))))
            ((invalid-authorization-code? error)
             (return
              (build-response
               #:code 400
               #:reason-phrase (G_ "reason-phrase|Bad Request")
               #:headers '((content-type application/xhtml-xml)))
              (call-with-output-string
                (lambda (port)
                  (sxml->xml
                   `(*TOP*
                     (*PI* xml "version=\"1.0\" encoding=\"utf-8\"")
                     (html (@ (xmlns "http://www.w3.org/1999/xhtml")
                              (xml:lang ,(W_ "xml-lang|en")))
                           (body
                            ,(sxml-match
                              (xml->sxml
                               (W_ (format #f "<h1>Invalid authorization code</h1>")))
                              ((*TOP* ,title) title))
                            ,(sxml-match
                              (xml->sxml
                               (W_ (format #f "<p>The authorization code is forged, or expired.</p>")))
                              ((*TOP* ,p) p))
                            ,@(if (message-for-the-user? error)
                                  (user-message error)
                                  '()))))
                   port)))))
            ;; Other bad request
            (else
             (return
              (build-response
               #:code 400
               #:reason-phrase (G_ "reason-phrase|Bad Request")
               #:headers '((content-type application/xhtml+xml)))
              (call-with-output-string
                (lambda (port)
                  (sxml->xml
                   `(*TOP*
                     (*PI* xml "version=\"1.0\" encoding=\"utf-8\"")
                     (html (@ (xmlns "http://www.w3.org/1999/xhtml")
                              (xml:lang ,(W_ "xml-lang|en")))
                           (body
                            ,(sxml-match
                              (xml->sxml
                               (W_ (format #f "<h1>Bad token request</h1>")))
                              ((*TOP* ,title) title))
                            ,(sxml-match
                              (xml->sxml
                               (W_ (format #f "<p>The token request failed.</p>")))
                              ((*TOP* ,p) p))
                            ,@(if (message-for-the-user? error)
                                  (user-message error)
                                  '()))))
                   port)))))))
       thunk))))

(define (make-token-endpoint token-endpoint-uri iss issuer-key)
  (lambda (request request-body)
    (when (bytevector? request-body)
      (set! request-body (utf8->string request-body)))
    (try-handle-web-failure
     (lambda ()
       (parameterize ((p:current-date ((p:current-date)))
                      (web-locale request))
         (let ((current-time ((p:current-date))) ;; thunk parameter
               (form-args
                (if (and (request-content-type request)
                         (eq? (car (request-content-type request))
                              'application/x-www-form-urlencoded))
                    (filter
                     (lambda (x) x)
                     (map (lambda (kv)
                            (let ((parsed
                                   (list->vector
                                    (map (lambda (x)
                                           (uri-decode x #:decode-plus-to-space? #t))
                                         (string-split kv #\=)))))
                              (if (eq? (vector-length parsed) 2)
                                  `(,(vector-ref parsed 0) . ,(vector-ref parsed 1))
                                  #f)))
                          (string-split request-body #\&)))
                    '()))
               (method (request-method request))
               ;; Maybe we’re behind a reverse proxy, so the authority of
               ;; (request-uri request) is meaningless.
               (uri (build-uri (uri-scheme token-endpoint-uri)
                               #:userinfo (uri-userinfo token-endpoint-uri)
                               #:host (uri-host token-endpoint-uri)
                               #:port (uri-port token-endpoint-uri)
                               #:path (uri-path (request-uri request))
                               #:query (uri-query (request-uri request)))))
           (let ((grant-type (assoc-ref form-args "grant_type"))
                 (dpop (decode <dpop-proof> (assq-ref (request-headers request) 'dpop)
                               #:method method
                               #:uri uri
                               #:cnf/check
                               (lambda (jkt) #t))))
             (unless (and grant-type (string? grant-type))
               (let ((final-message 
                      (format #f (G_ "missing grant type")))
                     (final-user-message
                      (sxml-match
                       (xml->sxml
                        (format #f (W_ "<p>You did not specify a grant_type for this request.</p>")))
                       ((*TOP* ,p) p))))
                 (raise-exception
                  (make-exception
                   (make-unsupported-grant-type #f)
                   (make-exception-with-message final-message)
                   (make-message-for-the-user final-user-message)))))
             (receive (webid client-id)
                 (case (string->symbol grant-type)
                   ((authorization_code)
                    (let ((code
                           (let ((str (assoc-ref form-args "code")))
                             (unless str
                               (let ((final-message
                                      (format #f (G_ "missing authorization code")))
                                     (final-user-message
                                      (sxml-match
                                       (xml->sxml
                                        (format #f (W_ "<p>You want to grant an authorization code, but you did not set one.</p>")))
                                       ((*TOP* ,p) p))))
                                 (raise-exception
                                  (make-exception
                                   (make-no-authorization-code)
                                   (make-exception-with-message final-message)
                                   (make-message-for-the-user final-user-message)))))
                             (with-exception-handler
                                 (lambda (error)
                                   (raise-exception
                                    (make-exception
                                     (make-invalid-authorization-code)
                                     error)))
                               (lambda ()
                                 (decode <authorization-code> str
                                         #:issuer-key issuer-key))))))
                      (values (webid code) (client-id code))))
                   ((refresh_token)
                    (let ((refresh-token (assoc-ref form-args "refresh_token")))
                      (unless refresh-token
                        (let ((final-message
                               (format #f (G_ "missing refresh token")))
                              (final-user-message
                               (sxml-match
                                (xml->sxml
                                 (format #f (W_ "<p>You want to grant a refresh token, but you did not set one.</p>")))
                                ((*TOP* ,p) p))))
                          (raise-exception
                           (make-exception
                            (make-no-refresh-token)
                            (make-exception-with-message final-message)
                            (make-message-for-the-user final-user-message)))))
                      (refresh:with-refresh-token
                       refresh-token
                       (jwk dpop)
                       values)))
                   (else
                    (let ((final-message
                           (format #f (G_ "unsupported grant type: ~s")
                                   grant-type))
                          (final-user-message
                           (sxml-match
                            (xml->sxml
                             (format #f (W_ "<p>You want to use <pre>~s</pre> as a grant type, but this is not supported.</p>")
                                     grant-type))
                            ((*TOP* ,p) p))))                       
                      (raise-exception
                       (make-exception
                        (make-unsupported-grant-type grant-type)
                        (make-exception-with-message final-message)
                        (make-message-for-the-user final-user-message))))))
               (let ((id-token
                      (issue <id-token>
                             issuer-key
                             #:webid webid
                             #:iss iss
                             #:aud client-id))
                     (access-token
                      (issue <access-token>
                             issuer-key
                             #:webid webid
                             #:iss iss
                             #:client-key (jwk dpop)
                             #:client-id client-id))
                     (refresh-token
                      (if (equal? grant-type "refresh_token")
                          (assoc-ref form-args "refresh_token")
                          (refresh:issue-refresh-token webid client-id
                                                       (jkt (jwk dpop))))))
                 (values
                  (build-response #:headers '((content-type application/json)
                                              (cache-control (no-cache no-store)))
                                  #:port #f)
                  (stubs:scm->json-string
                   `((id_token . ,id-token)
                     (access_token . ,access-token)
                     (token_type . "DPoP")
                     (expires_in . ,(p:oidc-token-default-validity))
                     (refresh_token . ,refresh-token)))
                  client-id
                  #f))))))))))