summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/jwk.scm
blob: 5b17f292dd14700c9dc36884037f8069f8750ebb (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
;; 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 jwk)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc web-i18n)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 exceptions)
  #:use-module (srfi srfi-19)
  #:use-module (web response)
  #:use-module (web client)
  #:use-module (rnrs bytevectors)
  #:declarative? #t
  #:export
  (
   the-jwk
   jwk?
   kty
   the-public-jwk
   jwk-public?
   strip
   jkt
   make-rsa-public-key
   make-rsa-private-key
   make-ec-point
   make-ec-scalar
   generate-key
   the-jwks
   jwks?
   make-jwks
   jwks-keys
   serve-jwks
   get-jwks

   &not-a-jwk
   make-not-a-jwk
   not-a-jwk?

   &not-a-jwks
   make-not-a-jwks
   not-a-jwks?
   ))

(define-exception-type
  &not-a-jwk
  &external-error
  make-not-a-jwk
  not-a-jwk?)

(define-exception-type
  &not-a-jwks
  &external-error
  make-not-a-jwks
  not-a-jwks?)

(define (the-jwk x)
  (with-exception-handler
      (lambda (error)
        (let ((final-message
               (if (exception-with-message? error)
                   (format #f (G_ "the JWK is invalid: ~a")
                           (exception-message error))
                   (format #f (G_ "the JWK is invalid")))))
          (raise-exception
           (make-exception
            (make-not-a-jwk)
            (make-exception-with-message final-message)
            error))))
    (lambda ()
      (let ((kty (stubs:kty x)))
        (unless (or (eq? kty 'EC) (eq? kty 'RSA))
          (fail (format #f (G_ "unknown key type ~s")
                        kty)))
        x))))

(define (jwk? x)
  (false-if-exception
   (and (the-jwk x) #t)))

(define (kty x)
  (stubs:kty (the-jwk x)))

(define (the-public-jwk x)
  (with-exception-handler
      (lambda (error)
        (let ((final-message
               (if (exception-with-message? error)
                   (format #f (G_ "the public JWK is invalid: ~a")
                           (exception-message error))
                   (format #f (G_ "the public JWK is invalid")))))
          (raise-exception
           (make-exception
            (make-not-a-jwk)
            (make-exception-with-message final-message)
            error))))
    (lambda ()
      (let ((key (the-jwk x)))
        (let ((crv (assq-ref key 'crv))
              (x (assq-ref key 'x))
              (y (assq-ref key 'y))
              (n (assq-ref key 'n))
              (e (assq-ref key 'e)))
          (let ((ec-part `((crv . ,crv)
                           (x . ,x)
                           (y . ,y)))
                (rsa-part `((n . ,n)
                            (e . ,e))))
            (case (stubs:kty key)
              ((EC) ec-part)
              ((RSA) rsa-part))))))))

(define (jwk-public? key)
  (false-if-exception
   (and (the-public-jwk key) #t)))

(define (strip key)
  (with-exception-handler
      (lambda (error)
        (let ((final-message
               (if (exception-with-message? error)
                   (format #f (G_ "cannot extract the public part of the key: ~a")
                           (exception-message error))
                   (format #f (G_ "cannot extract the public part of the key")))))
          (raise-exception
           (make-exception
            (make-not-a-jwk)
            (make-exception-with-message final-message)
            error))))
    (lambda ()
      (stubs:strip-key key))))

(define (jkt x)
  (stubs:jkt (the-public-jwk x)))

(define (make-rsa-public-key n e)
  (the-public-jwk
   `((n . ,n)
     (e . ,e))))

(define (make-rsa-private-key d p q dp dq qi)
  (the-jwk
   `((d . ,d)
     (p . ,p)
     (q . ,q)
     (dp . ,dp)
     (dq . ,dq)
     (qi . ,qi))))

(define (make-ec-point crv x y)
  (if (symbol? crv)
      (make-ec-point (symbol->string crv) x y)
      (the-public-jwk
       `((crv . ,crv)
         (x . ,x)
         (y . ,y)))))

(define (make-ec-scalar crv d)
  (if (symbol? crv)
      (make-ec-scalar (symbol->string crv) d)
      (the-jwk
       `((crv . ,crv)
         (d . ,d)))))

(define generate-key stubs:generate-key)

(define (the-public-keys keys)
  (map the-public-jwk keys))

(define (the-jwks jwks)
  (let ((keys (vector->list (assoc-ref jwks 'keys))))
    (unless keys
      (let ((final-message
             (format #f (G_ "the JWKS is invalid, because it does not have keys"))))
        (raise-exception
         (make-exception
          (make-not-a-jwks)
          (make-exception-with-message final-message)))))
    (with-exception-handler
        (lambda (error)
          (let ((final-message
                 (if (exception-with-message? error)
                     (format #f (G_ "the JWKS is invalid: ~a")
                             (exception-message error))
                     (format #f (G_ "the JWKS is invalid")))))
            (raise-exception
             (make-exception
              (make-not-a-jwks)
              (make-exception-with-message final-message)
              error))))
      (lambda ()
        `((keys . ,(list->vector (the-public-keys keys))))))))

(define (jwks? jwks)
  (false-if-exception
   (and (the-jwks jwks) #t)))

(define (make-jwks keys)
  (if (vector? keys)
      (make-jwks (vector->list keys))
      (let ((pubs (list->vector (map strip keys))))
        (the-jwks `((keys . ,pubs))))))

(define (jwks-keys jwks)
  (vector->list (assq-ref (the-jwks jwks) 'keys)))

(define (serve-jwks expiration-date jwks)
  (values (build-response
           #:headers `((content-type . (application/json))
                       (expires . ,expiration-date)))
          (stubs:scm->json-string (the-jwks jwks))))

(define* (get-jwks uri #:key (http-get http-get))
  (receive (response response-body) (http-get uri)
    (with-exception-handler
        (lambda (cause)
          (raise-unexpected-response response cause))
      (lambda ()
        (unless (eqv? (response-code response) 200)
          (raise-request-failed-unexpectedly
           (response-code response)
           (response-reason-phrase response)))
        (let ((content-type (response-content-type response)))
          (unless content-type
            (raise-unexpected-header-value 'content-type content-type))
          (unless (and (eq? (car content-type) 'application/json)
                       (or (equal? (assoc-ref (cdr content-type) 'charset)
                                   "utf-8")
                           (not (assoc-ref (cdr content-type) 'charset))))
            (raise-unexpected-header-value 'content-type content-type))
          (unless (string? response-body)
            (set! response-body (utf8->string response-body)))
          (the-jwks (stubs:json-string->scm response-body)))))))