summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/authorization-code.scm
blob: 1481b2c9c31af19ad3b7e59c093dc7227a1b5d4c (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
;; 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 authorization-code)
  #:use-module (webid-oidc errors)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module (webid-oidc jws)
  #:use-module (webid-oidc jwk)
  #:use-module (webid-oidc jti)
  #:use-module ((webid-oidc parameters) #:prefix p:)
  #:use-module (web uri)
  #:use-module (srfi srfi-19)
  #:use-module (webid-oidc web-i18n)
  #:use-module (ice-9 match)
  #:use-module (ice-9 exceptions)
  #:declarative? #t
  #:export
  (

   &invalid-authorization-code
   make-invalid-authorization-code
   invalid-authorization-code?

   the-authorization-code
   authorization-code?

   authorization-code-alg

   authorization-code-webid
   authorization-code-client-id
   authorization-code-jti
   authorization-code-exp

   authorization-code-decode
   issue-authorization-code
   ))

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

(define (the-authorization-code x)
  (with-exception-handler
      (lambda (error)
        (let ((final-message
               (cond
                ((invalid-jws? error)
                 (if (exception-with-message? error)
                     (format #f (G_ "this is not an authorization code, because it is not even a JWS: ~a")
                             (exception-message error))
                     (format #f (G_ "this is not an authorization code, because it is not even a JWS"))))
                (else
                 (if (exception-with-message? error)
                     (format #f (G_ "this is not an authorization code: ~a")
                             (exception-message error))
                     (format #f (G_ "this is not an authorization code")))))))
          (raise-exception
           (make-exception
            (make-invalid-authorization-code)
            (make-exception-with-message final-message)
            error))))
    (lambda ()
      (match (the-jws x)
        ((header . payload)
         (let examine-payload ((payload payload)
                               (webid #f)
                               (client-id #f)
                               (jti #f)
                               (exp #f)
                               (other-fields '()))
           (match payload
             (()
              (unless (and webid client-id jti exp)
                (fail (format #f (G_ "the payload is missing ~s")
                              `(,@(if webid '() '("webid"))
                                ,@(if client-id '() '("client_id"))
                                ,@(if jti '() '("jti"))
                                ,@(if exp '() '("exp"))))))
              `(,header
                . ((webid . ,(uri->string webid))
                   (client_id . ,(uri->string client-id))
                   (jti . ,jti)
                   (exp . ,(time-second (date->time-utc exp)))
                   ,@(reverse other-fields))))
             ((('webid . (? string? (= string->uri (? uri? webid-given)))) payload ...)
              (examine-payload payload
                               (or webid webid-given)
                               client-id jti exp other-fields))
             ((('webid . infringing) payload ...)
              (fail (format #f (G_ "the \"webid\" field should be an URI, ~s is given")
                            infringing)))
             ((('client_id . (? string? (= string->uri (? uri? client-id-given)))) payload ...)
              (examine-payload payload webid
                               (or client-id client-id-given)
                               jti exp other-fields))
             ((('client_id . infringing) payload ...)
              (fail (format #f (G_ "the \"client_id\" field should be an URI, ~s is given")
                            infringing)))
             ((('jti . (? string? jti-given)) payload ...)
              (examine-payload payload webid client-id
                               (or jti jti-given)
                               exp other-fields))
             ((('jti . invalid) payload ...)
              (fail (format #f (G_ "the \"jti\" field should be a string, ~s is given")
                            invalid)))
             ((('exp . (? (lambda (x) (and (integer? x) (>= x 0))) exp-given)) payload ...)
              (examine-payload payload webid client-id jti
                               (or exp (time-utc->date (make-time time-utc 0 exp-given)))
                               other-fields))
             ((('exp . infringing) payload ...)
              (fail (format #f (G_ "the \"exp\" field should be a timestamp, ~s is given")
                            infringing)))
             ((field payload ...)
              (examine-payload payload webid client-id jti exp `(,field ,@other-fields))))))
        (else
         (scm-error 'wrong-type-arg "the-authorization-code"
                    "expected a pair of lists"
                    '()
                    (list x)))))))

(define (authorization-code? x)
  (false-if-exception (the-authorization-code x)))

(define (authorization-code-alg x)
  (match (the-authorization-code x)
    ((header . _)
     (string->symbol (assq-ref header 'alg)))))

(define (authorization-code-webid x)
  (match (the-authorization-code x)
    ((_ . payload)
     (string->uri (assq-ref payload 'webid)))))

(define (authorization-code-client-id x)
  (match (the-authorization-code x)
    ((_ . payload)
     (string->uri (assq-ref payload 'client_id)))))

(define (authorization-code-jti x)
  (match (the-authorization-code x)
    ((_ . payload)
     (assq-ref payload 'jti))))

(define (authorization-code-exp x)
  (match (the-authorization-code x)
    ((_ . payload)
     (time-utc->date (make-time time-utc 0 (assq-ref payload 'exp))))))

(define (authorization-code-decode str jwk)
  (parameterize ((p:current-date
                  (time-second (date->time-utc ((p:current-date))))))
    (with-exception-handler
        (lambda (error)
          (let ((final-message
                 (if (exception-with-message? error)
                     (format #f (G_ "the authorization code is invalid: ~a")
                             (exception-message error))
                     (format #f (G_ "the authorization code is invalid")))))
            (raise-exception
             (make-exception
              (make-invalid-authorization-code)
              (make-exception-with-message final-message)
              error))))
      (lambda ()
        (let ((code (the-authorization-code (jws-decode str (lambda (x) jwk)))))
          (let ((exp (authorization-code-exp code))
                (current-date ((p:current-date))))
            (let ((exp-s (time-second (date->time-utc exp)))
                  (current-s (time-second (date->time-utc current-date))))
              (when (>= current-s exp-s)
                (let ((final-message
                       (format #f (G_ "the authorization expired ~a, which is in the past (from ~a)")
                               (date->string exp)
                               (date->string current-date))))
                  (raise-exception
                   (make-exception
                    (make-expired exp current-date)
                    (make-exception-with-message final-message)))))
              (jti-check (authorization-code-jti code)
                         (- exp-s current-s))
              code)))))))

(define (authorization-code-encode authorization-code key)
  (with-exception-handler
      (lambda (error)
        (let ((final-message
               (if (exception-with-message? error)
                   (format #f (G_ "cannot encode the authorization code: ~a")
                           (exception-message error))
                   (format #f (G_ "cannot encode the authorization code")))))
          (raise-exception
           (make-exception-with-message final-message))))
    (lambda ()
      (jws-encode authorization-code key))))

(define* (issue-authorization-code issuer-key
                                   #:key
                                   (validity 120)
                                   webid
                                   client-id)
  (let* ((iat (time-second (date->time-utc ((p:current-date)))))
         (exp (+ iat validity)))
    (authorization-code-encode
     `(((alg . ,(symbol->string (alg issuer-key))))
       . ((webid . ,(uri->string webid))
          (client_id . ,(uri->string client-id))
          (exp . ,exp)
          (jti . ,(stubs:random 12))))
     issuer-key)))