summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/client-manifest.scm
blob: 847fc5489878de97210a0d4444a6f2cac61873bf (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
;; 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 client-manifest)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc fetch)
  #:use-module (webid-oidc web-i18n)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 optargs)
  #:use-module (rdf rdf)
  #:use-module (turtle tordf)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 match)
  #:use-module (sxml match)
  #:use-module (sxml simple)
  #:declarative? #t
  #:export
  (

   public-oidc-client

   &invalid-client-manifest
   make-invalid-client-manifest
   invalid-client-manifest?

   &unauthorized-redirect-uri
   make-unauthorized-redirect-uri
   unauthorized-redirect-uri?

   &inconsistent-client-manifest
   make-inconsistent-client-manifest
   inconsistent-client-manifest?

   &cannot-serve-public-manifest
   make-cannot-serve-public-manifest
   cannot-serve-public-manifest?

   &cannot-fetch-client-manifest
   make-cannot-fetch-client-manifest
   cannot-fetch-client-manifest?

   the-client-manifest
   client-manifest?
   make-client-manifest
   client-manifest-client-id
   client-manifest-check-redirect-uri

   serve-client-manifest
   get-client-manifest

   ))

(define public-oidc-client
  'public-oidc-client)

(define-exception-type
  &invalid-client-manifest
  &external-error
  make-invalid-client-manifest
  invalid-client-manifest?)

(define-exception-type
  &unauthorized-redirect-uri
  &external-error
  make-unauthorized-redirect-uri
  unauthorized-redirect-uri?)

(define-exception-type
  &inconsistent-client-manifest
  &external-error
  make-inconsistent-client-manifest
  inconsistent-client-manifest?)

(define-exception-type
  &cannot-serve-public-manifest
  &external-error
  make-cannot-serve-public-manifest
  cannot-serve-public-manifest?)

(define-exception-type
  &cannot-fetch-client-manifest
  &external-error
  make-cannot-fetch-client-manifest
  cannot-fetch-client-manifest?)

(define (the-client-manifest x)
  (with-exception-handler
      (lambda (error)
        (let ((sysadmin-message
               (if (exception-with-message? error)
                   (format #f (G_ "this is not a client manifest: ~a")
                           (exception-message error))
                   (format #f (G_ "this is not a client manifest"))))
              (user-message
               (let ((new-paragraph
                      (sxml-match
                       (xml->sxml (W_ "<p>The client manifest could
not be queried. It can be because the client application is down, or
it is incomplete, or unusable for other reasons.</p>"))
                       ((*TOP* ,element)
                        element))))
                 (if (message-for-the-user? error)
                     (sxml-match
                      (user-message error)
                      ((div ,element ...)
                       `(div ,new-paragraph ,element ...))
                      (,element
                          `(div ,new-paragraph ,element)))
                     new-paragraph))))
          (raise-exception
           (make-exception
            (make-invalid-client-manifest)
            (make-exception-with-message sysadmin-message)
            (make-message-for-the-user user-message)
            error))))
    (lambda ()
      (let examine-fields ((fields x)
                           (client-id #f)
                           (redirect-uris #f)
                           (other-fields '()))
        (match fields
          (()
           (unless (and client-id redirect-uris)
             (fail (format #f (G_ "the client manifest is missing ~s")
                           (apply append
                                  `(,@(if client-id '() '("client_id"))
                                    ,@(if redirect-uris '() '("redirect_uris")))))))
           `((client_id . ,(uri->string client-id))
             (redirect_uris . ,(list->vector (map uri->string redirect-uris)))
             ,@(reverse other-fields)))
          ((('client_id . (? string? (= string->uri (? uri? client-id-given)))) fields ...)
           (examine-fields fields (or client-id client-id-given)
                           redirect-uris other-fields))
          ((('client_id . invalid) _ ...)
           (fail (format #f (G_ "~s is an invalid \"client_id\" value, because it is not an URI")
                         invalid)))
          ((('redirect_uris . #((? string? (= string->uri (? uri? uri))) ...)) fields ...)
           (examine-fields fields client-id (or redirect-uris uri) other-fields))
          ((('redirect_uris . #(_ ...)) _ ...)
           (fail (format #f (G_ "at least one of the redirect URIs is not a proper URI"))))
          ((('redirect_uris . _) _ ...)
           (fail (format #f (G_ "the \"redirect_uris\" field should be a vector of URIs"))))
          ((other-field fields ...)
           (examine-fields fields client-id redirect-uris
                           `(,other-field ,@other-fields)))
          (else
           (fail (format #f (G_ "the client manifest should be a JSON object")))))))))

(define (client-manifest? x)
  (false-if-exception
   (the-client-manifest x)))

(define (make-client-manifest client-id redirect-uris)
  (the-client-manifest
   `((client_id . ,(uri->string client-id))
     (redirect_uris . ,(list->vector
                        (map uri->string
                             redirect-uris))))))

(define (client-manifest-client-id mf)
  (if (eq? mf public-oidc-client)
      (string->uri "http://www.w3.org/ns/solid/terms#PublicOidcClient")
      (string->uri (assq-ref (the-client-manifest mf) 'client_id))))

(define (check-redirect mf uris redir)
  (match uris
    (()
     (let ((final-message
            (format #f (G_ "the client manifest does not allow ~s as a redirection uri")
                    (uri->string redir)))
           (final-user-message
            (sxml-match
             (xml->sxml (W_ "<p>The application wants to get your
authorization through <strong>~s</strong>, which is not
approved.</p>"))
             ((*TOP* ,element) element))))
       (raise-exception
        (make-exception
         (make-unauthorized-redirect-uri)
         (make-exception-with-message final-message)
         (make-message-for-the-user final-user-message)))))
    (((? (cute equal? <> redir) redir) _ ...)
     #t)
    ((_ uris ...)
     (check-redirect mf uris redir))))

(define (client-manifest-check-redirect-uri mf redir)
  (unless (uri? redir)
    (set! redir (string->uri redir)))
  (if (eq? mf public-oidc-client)
      #t
      (let ((redirect-uris
             (assq-ref (the-client-manifest mf) 'redirect_uris)))
        (check-redirect (the-client-manifest mf)
                        (map string->uri (vector->list redirect-uris))
                        redir))))

(define (serve-client-manifest expiration-date mf)
  (when (eq? mf public-oidc-client)
    (let ((final-message
           (format #f (G_ "cannot serve the public manifest"))))
      (raise-exception
       (make-exception
        (make-cannot-serve-public-manifest)
        (make-exception-with-message final-message)))))
  (let ((json-object (stubs:scm->json-string
                      `((@context . "https://www.w3.org/ns/solid/oidc-context.jsonld")
                        ,@(the-client-manifest mf)))))
    (values (build-response #:headers `((content-type application/ld+json)
                                        (expires . ,expiration-date)))
            json-object)))

(define* (get-client-manifest id
                              #:key
                              (http-get http-get))
  (unless (uri? id)
    (set! id (string->uri id)))
  (with-exception-handler
      (lambda (error)
        (let ((final-message
               (if (exception-with-message? error)
                   (format #f (G_ "cannot fetch the client manifest ~s: ~a")
                           (uri->string id)
                           (exception-message error))
                   (format #f (G_ "cannot fetch the client manifest ~s")
                           (uri->string id)))))
          (raise-exception
           (make-exception
            (make-cannot-fetch-client-manifest)
            (make-exception-with-message final-message)
            error))))
    (lambda ()
      (if (equal? id
                  (string->uri
                   "http://www.w3.org/ns/solid/terms#PublicOidcClient"))
          public-oidc-client
          (receive (response response-body)
              (http-get id)
            (when (bytevector? response-body)
              (set! response-body (utf8->string response-body)))
            (let ((mf (the-client-manifest (stubs:json-string->scm response-body))))
              (unless (equal? (client-manifest-client-id mf) id)
                (let ((final-message
                       (format #f (G_ "the client manifest is dereferenced from ~s, but it pretends to be ~s")
                               (uri->string id)
                               (uri->string (client-manifest-client-id mf)))))
                  (raise-exception
                   (make-exception
                    (make-inconsistent-client-manifest)
                    (make-exception-with-message final-message)))))
              mf))))))