summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/resource-server.scm
blob: c69bc5131c7a36a275b5941d34c9d402d6f64675 (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
(define-module (webid-oidc resource-server)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc oidc-configuration)
  #:use-module (webid-oidc provider-confirmation)
  #:use-module (webid-oidc jwk)
  #:use-module (webid-oidc dpop-proof)
  #:use-module (webid-oidc server create)
  #:use-module (webid-oidc server read)
  #:use-module (webid-oidc server update)
  #:use-module (webid-oidc server delete)
  #:use-module (webid-oidc server precondition)
  #:use-module (webid-oidc http-link)
  #: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 (web client)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 i18n)
  #:use-module (ice-9 getopt-long)
  #:use-module (ice-9 suspendable-ports)
  #:use-module (ice-9 control)
  #:use-module (sxml simple)
  #:use-module (srfi srfi-19))

(define (G_ text)
  (let ((out (gettext text)))
    (if (string=? out text)
        ;; No translation, disambiguate
        (car (reverse (string-split text #\|)))
        out)))

(define*-public (make-authenticator jti-list
                                    #:key
                                    (server-uri #f)
                                    (current-time current-time)
                                    (http-get http-get))
  (unless (and server-uri (uri? server-uri))
    (error "You need to pass #:server-uri URI where URI is the public URI of the server, as a (web uri)."))
  (lambda (request request-body)
    (let ((headers (request-headers request))
          (uri (request-uri request))
          (method (request-method request))
          (current-time
           (let ((t current-time))
             (when (thunk? t)
               (set! t (t)))
             (when (integer? t)
               (set! t (make-time time-utc 0 t)))
             (when (time? t)
               (set! t (time-utc->date t)))
             t)))
      (let ((authz (assoc-ref headers 'authorization))
            (dpop (assoc-ref headers 'dpop))
            (full-uri (build-uri (uri-scheme server-uri)
                                 #:userinfo (uri-userinfo server-uri)
                                 #:host (uri-host server-uri)
                                 #:port (uri-port server-uri)
                                 #:path (string-append
                                         "/"
                                         (encode-and-join-uri-path
                                          (append
                                           (split-and-decode-uri-path (uri-path server-uri))
                                           (split-and-decode-uri-path
                                            (uri-path uri))))))))
        (and authz dpop
             (eq? (car authz) 'dpop)
             (with-exception-handler
                 (lambda (error)
                   (format (current-error-port)
                           (G_ "~a: authentication failure: ~a\n")
                           (date->string current-time)
                           (error->str error))
                   #f)
               (lambda ()
                 (let* ((access-token
                         (access-token-decode
                          (symbol->string (cadr authz))
                          #:http-get http-get))
                        (cnf/jkt (access-token-cnf/jkt access-token))
                        (dpop-proof
                         (dpop-proof-decode
                          current-time jti-list method full-uri
                          dpop cnf/jkt)))
                   (let ((subject (access-token-webid access-token))
                         (issuer (access-token-iss access-token)))
                     (confirm-provider subject issuer #:http-get http-get)
                     subject)))
               #: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))))))

(define*-public (make-resource-server
                 #:key
                 (server-uri #f)
                 (owner #f)
                 (authenticator #f)
                 (current-time current-time)
                 (http-get http-get))
  (unless owner
    (error "The owner is not defined."))
  (declare-link-header!)
  (unless authenticator
    (set! authenticator
          (make-authenticator (make-jti-list)
                              #:server-uri server-uri
                              #:current-time current-time
                              #:http-get http-get)))
  (lambda (request request-body)
    (let ((user (authenticator request request-body)))
      (handle-errors
       (lambda (return)
         (let ((method (request-method request)))
           (case method
             ((GET HEAD OPTIONS)
              (receive (headers content)
                  (read server-uri owner user
                        (uri-path (request-uri request))
                        #:http-get http-get)
                (with-exception-handler
                    (lambda (error)
                      (return
                       (build-response
                        #:headers headers)
                       (if (eq? method 'GET)
                           content
                           "")))
                  (lambda ()
                    (unless (or (request-if-match request)
                                (request-if-none-match request))
                      ;; Act as if the precondition failed
                      (raise-exception
                       (make-precondition-failed
                        (uri-path (request-uri request))
                        (request-if-match request)
                        (request-if-none-match request)
                        (car (assq-ref headers 'etag)))))
                    (check-precondition
                     (uri-path (request-uri request))
                     (request-if-match request)
                     (request-if-none-match request)
                     (car (assq-ref headers 'etag)))
                    (return
                     (build-response
                      #:code 304
                      #:reason-phrase "Not Modified"
                      #:headers headers)
                     "")))))
             ((PUT)
              (return
               (build-response
                #:headers
                `((etag . (,(update server-uri owner user
                                    (uri-path (request-uri request))
                                    (request-if-match request)
                                    (request-if-none-match request)
                                    (request-content-type request)
                                    request-body
                                    #:http-get http-get)
                           . #f))))
               ""))
             ((POST)
              (let ((types
                     (map car
                          (filter
                           (lambda (link)
                             (equal? (assq-ref link 'rel) "type"))
                           (request-links request)))))
                (return
                 (build-response
                  #:headers
                  `((location . ,(create server-uri owner user
                                         (uri-path (request-uri request))
                                         types
                                         (assq-ref (request-headers request) 'slug)
                                         (request-content-type request)
                                         request-body
                                         #:http-get http-get))))
                 "")))
             ((DELETE)
              (delete server-uri owner user
                      (uri-path (request-uri request))
                      (request-if-match request)
                      (request-if-none-match request)
                      #:http-get http-get)
              (return
               (build-response)
               "")))))
       (lambda (return error)
         (if (cannot-fetch-group? error)
             (format (current-error-port) (G_ "Warning: ~a\n")
                     (error->str error))
             (begin
               (format (current-error-port) (G_ "Error: ~a\n")
                       (error->str error))
               (cond
                ((uri-slash-semantics-error? error)
                 (return
                  (build-response
                   #:code 301
                   #: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 (uri-slash-semantics-error-expected-path error)))))
                  ""))
                ((or (path-not-found? error)
                     (auxiliary-resource-absent? error)
                     (forbidden? error))
                 (if user
                     ;; That’s a forbidden
                     (return
                      (build-response #:code 403 #:reason-phrase "Forbidden")
                      "")
                     (return
                      (build-response #:code 401 #:reason-phrase "Unauthorized"
                                      #:headers `((www-authenticate . ((DPoP)))))
                      "")))
                ((or (cannot-delete-root? error))
                 (return
                  (build-response
                   #:code 405
                   #:reason-phrase "Method Not Allowed")
                  ""))
                ((or (container-not-empty? error)
                     (incorrect-containment-triples? error)
                     (path-is-auxiliary? error))
                 (return
                  (build-response
                   #:code 409
                   #:reason-phrase "Conflict")
                  ""))
                ((unsupported-media-type? error)
                 (return
                  (build-response
                   #:code 415
                   #:reason-phrase "Unsupported Media Type")
                  ""))
                ((precondition-failed? error)
                 (return
                  (build-response
                   #:code 412
                   #:reason-phrase "Precondition Failed")
                  ""))))))))))