summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/resource-server.scm
blob: 65d64f09ba8c42118e4e038cbb74d3b7dea366b3 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
;; 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 resource-server)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc provider-confirmation)
  #:use-module (webid-oidc jwk)
  #:use-module (webid-oidc dpop-proof)
  #:use-module (webid-oidc serve)
  #:use-module ((webid-oidc server create) #:prefix ldp:)
  #:use-module ((webid-oidc server read) #:prefix ldp:)
  #:use-module ((webid-oidc server update) #:prefix ldp:)
  #:use-module ((webid-oidc server delete) #:prefix ldp:)
  #:use-module ((webid-oidc server resource wac) #:prefix wac:)
  #:use-module ((webid-oidc server resource path) #:prefix ldp:)
  #:use-module ((webid-oidc server resource content) #:prefix ldp:)
  #:use-module (webid-oidc server precondition)
  #:use-module (webid-oidc server endpoint)
  #:use-module (webid-oidc server endpoint authentication)
  #:use-module (webid-oidc http-link)
  #:use-module ((webid-oidc parameters) #:prefix p:)
  #:use-module ((webid-oidc config) #:prefix cfg:)
  #:use-module (webid-oidc jti)
  #:use-module (webid-oidc access-token)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web uri)
  #:use-module (web server)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 receive)
  #:use-module (webid-oidc web-i18n)
  #:use-module (ice-9 getopt-long)
  #:use-module (ice-9 suspendable-ports)
  #:use-module (ice-9 control)
  #:use-module (ice-9 match)
  #:use-module (ice-9 exceptions)
  #:use-module (sxml simple)
  #:use-module (srfi srfi-19)
  #:use-module (oop goops)
  #:duplicates (merge-generics)
  #:declarative? #t
  #:export
  (
   make-authenticator
   make-resource-server
   ))

(define-class <stub-endpoint> (<endpoint>))

(define return
  (make-parameter #f))

(define-method (handle (endpoint <stub-endpoint>) request request-body)
  ((return) (assq-ref (request-meta request) 'user)))

(define* (make-authenticator #:key (server-uri #f))
  (unless (and server-uri (uri? server-uri))
    (fail (G_ "You need to pass #:server-uri URI where URI is the public URI of the server, as a (web uri).")))
  (let* ((backend (make <stub-endpoint>))
         (endpoint (make <authenticator>
                     #:backend backend
                     #:server-uri server-uri)))
    (lambda (request request-body)
      (parameterize ((web-locale request))
        (with-exception-handler
            (lambda (error)
              #f)
          (lambda ()
            (let/ec ret
              (parameterize ((return ret))
                (handle endpoint request request-body))))
          #:unwind? #t)))))

(define (handle-errors f g)
  (call/ec
   (lambda (do-return)
     (define (return . args)
       (apply do-return args))
     (with-exception-handler
         (lambda (error)
           (g return error))
       (lambda ()
         (f return))
       #:unwind? #t))))

(define (nonrdf-or-turtle server-uri request request-body)
  ;; If the request is an exotic RDF serialization
  ;; format, we want to convert it to Turtle,
  ;; otherwise we will consider it non-rdf.
  (convert '(text/turtle */*)
           server-uri
           (uri-path (request-uri request))
           (match (request-content-type request)
             ((or (? symbol? content-type)
                  ((? symbol? content-type) _ ...))
              content-type))
           request-body))

(define (serve-get return path if-match if-none-match content-type content etag headers user)
  (define (respond-normal)
    (return
     (build-response
      #:headers headers)
     content
     user))
  (if if-match
      ;; If the precondition failed, then we should respond with 412
      (with-exception-handler
          (lambda (error)
            (unless (precondition-failed? error)
              (raise-exception error))
            (return
             (build-response
              #:code 412
              #:reason-phrase (W_ "reason-phrase|Precondition Failed"))
             #f
             user))
        (lambda ()
          (check-precondition path if-match if-none-match etag)
          (respond-normal)))
      ;; If the precondition succeeds (if-none-match is effectively
      ;; invalid), we return 200
      (with-exception-handler
          (lambda (error)
            (unless (precondition-failed? error)
              (raise-exception error))
            (return
             (build-response
              #:code 304
              #:reason-phrase (W_ "reason-phrase|Not Modified")
              #:headers
              (filter
               (lambda (h)
                 (case (car h)
                   ((cache-control content-location date etag expires vary)
                    #t)
                   (else #f)))
               headers))
             #f
             user))
        (lambda ()
          (when if-none-match
            (check-precondition path if-match if-none-match etag))
          (respond-normal)))))

(define* (make-resource-server
          #:key
          (server-uri #f)
          (owner #f)
          (authenticator #f))
  (unless owner
    (fail (G_ "The owner is not defined.")))
  (declare-link-header!)
  (unless authenticator
    (set! authenticator
          (make-authenticator
           #:server-uri server-uri)))
  (lambda (request request-body)
    (parameterize ((p:current-date ((p:current-date))) ;; Fix the date
                   (web-locale request))
      (let ((user (authenticator request request-body)))
        (handle-errors
         (lambda (return)
           (let ((method (request-method request)))
             (case method
               ((GET HEAD OPTIONS)
                (receive (headers content)
                    (ldp:read server-uri owner user
                              (uri-path (request-uri request)))
                  (let ((true-content-type
                         (car (assq-ref headers 'content-type)))
                        (other-headers
                         (filter
                          (lambda (h)
                            (not (eq? (car h) 'content-type)))
                          headers)))
                    (receive (negociated-content-type
                              negociated-content)
                        (convert (request-accept request #f)
                                 server-uri
                                 (uri-path (request-uri request))
                                 true-content-type
                                 content)
                      (serve-get
                       return
                       (uri-path (request-uri request))
                       (request-if-match request)
                       (request-if-none-match request)
                       negociated-content-type
                       negociated-content
                       (car (assq-ref headers 'etag))
                       (cons `(content-type ,negociated-content-type)
                             other-headers)
                       user)))))
               ((PUT)
                (receive (content-type content)
                    (nonrdf-or-turtle server-uri request request-body)
                  (unless content
                    (return
                     (build-response
                      #:code 400
                      #:reason-phrase (W_ "Bad Request"))
                     ""
                     user))
                  (let ((updated
                         (ldp:update server-uri owner user
                                     (uri-path (request-uri request))
                                     (request-if-match request)
                                     (request-if-none-match request)
                                     content-type
                                     content)))
                    (return
                     (build-response
                      #:headers
                      `((etag . (,(ldp:etag updated) . #f))))
                     ""
                     user))))
               ((POST)
                (receive (content-type content)
                    (nonrdf-or-turtle server-uri request request-body)
                  (unless content
                    (return
                     (build-response
                      #:code 400
                      #:reason-phrase (W_ "Bad Request"))
                     ""
                     user))
                  (let ((types
                         (map target-iri
                              (filter
                               (lambda (link)
                                 (equal? (relation-type link) "type"))
                               (request-links request)))))
                    (return
                     (build-response
                      #:code 201 #:reason-phrase (W_ "reason-phrase|Created")
                      #:headers
                      `((location . ,(ldp:create server-uri owner user
                                                 (uri-path (request-uri request))
                                                 types
                                                 (assq-ref (request-headers request) 'slug)
                                                 content-type
                                                 content))))
                     ""
                     user))))
               ((DELETE)
                (ldp:delete server-uri owner user
                            (uri-path (request-uri request))
                            (request-if-match request)
                            (request-if-none-match request))
                (return
                 (build-response)
                 ""
                 user)))))
         (lambda (return error)
           (if (wac:cannot-fetch-group? error)
               (if (exception-with-message? error)
                   (format (current-error-port)
                           (G_ "~a: ignoring a group that cannot be fetched: ~a\n")
                           (date->string ((p:current-date)))
                           (exception-message error))
                   (format (current-error-port)
                           (G_ "~a: ignoring a group that cannot be fetched\n")
                           (date->string ((p:current-date)))))
               (cond
                ((ldp:uri-slash-semantics-error? error)
                 (return
                  (build-response
                   #:code 301
                   #:reason-phrase (W_ "reason-phrase|Found")
                   #:headers
                   `((location
                      . ,(build-uri
                          (uri-scheme server-uri)
                          #:userinfo (uri-userinfo server-uri)
                          #:host (uri-host server-uri)
                          #:port (uri-port server-uri)
                          #:path (ldp:uri-slash-semantics-error-existing error)))))
                  #f
                  user))
                ((or (ldp:path-not-found? error)
                     (ldp:auxiliary-resource-absent? error)
                     (wac:forbidden? error))
                 (if user
                     ;; That’s a forbidden
                     (return
                      (build-response #:code 403 #:reason-phrase (W_ "reason-phrase|Forbidden"))
                      #f
                      user)
                     (return
                      (build-response #:code 401 #:reason-phrase (W_ "reason-phrase|Unauthorized")
                                      #:headers `((www-authenticate . ((DPoP)))))
                      #f
                      user)))
                ((ldp:cannot-delete-root? error)
                 (return
                  (build-response
                   #:code 405
                   #:reason-phrase (W_ "reason-phrase|Method Not Allowed"))
                  #f
                  user))
                ((or (ldp:container-not-empty? error)
                     (ldp:incorrect-containment-triples? error)
                     (ldp:path-is-auxiliary? error))
                 (return
                  (build-response
                   #:code 409
                   #:reason-phrase (W_ "reason-phrase|Conflict"))
                  #f
                  user))
                ((ldp:unsupported-media-type? error)
                 (return
                  (build-response
                   #:code 415
                   #:reason-phrase (W_ "reason-phrase|Unsupported Media Type"))
                  #f
                  user))
                ((precondition-failed? error)
                 (return
                  (build-response
                   #:code 412
                   #:reason-phrase (W_ "reason-phrase|Precondition Failed"))
                  #f
                  user))
                ((not-acceptable? error)
                 (return
                  (build-response
                   #:code 406
                   #:reason-phrase (W_ "reason-phrase|Not Acceptable"))
                  #f
                  user))
                (else
                 (raise-exception error))))))))))