summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/oidc-id-token.scm
blob: 9fe276cdf69d942c05c21b81d08f72e418cc4b40 (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
;; webid-oidc, 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 oidc-id-token)
  #:use-module (webid-oidc oidc-configuration)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc jws)
  #:use-module (webid-oidc jti)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (ice-9 optargs)
  #:use-module (srfi srfi-19))

(define-public (the-id-token-header x)
  (with-exception-handler
      (lambda (error)
        (raise-not-an-id-token-header x error))
    (lambda ()
      (the-jws-header x))))

(define-public (id-token-header? x)
  (false-if-exception
   (and (the-id-token-header x) #t)))

(define-public (the-id-token-payload x)
  (with-exception-handler
      (lambda (error)
        (raise-not-an-id-token-payload x error))
    (lambda ()
      (let ((x (the-jws-payload x)))
        (let ((webid (assq-ref x 'webid))
              (iss (assq-ref x 'iss))
              (sub (assq-ref x 'sub))
              (aud (assq-ref x 'aud))
              (nonce (assq-ref x 'nonce))
              (iat (assq-ref x 'iat))
              (exp (assq-ref x 'exp)))
          (unless (and webid (string? webid) (string->uri webid))
            (raise-incorrect-webid-field webid))
          (unless (and iss (string? iss) (string->uri iss))
            (raise-incorrect-iss-field iss))
          (unless (string? sub)
            (raise-incorrect-sub-field sub))
          (unless (and aud (string? aud) (string->uri aud))
            (raise-incorrect-aud-field aud))
          (unless (string? nonce)
            (raise-incorrect-nonce-field nonce))
          (unless (integer? iat)
            (raise-incorrect-iat-field iat))
          (unless (and (integer? exp) (>= exp iat))
            (raise-incorrect-exp-field exp))
          x)))))

(define-public (id-token-payload? x)
  (false-if-exception
   (and (the-id-token-header x) #t)))

(define-public (the-id-token x)
  (with-exception-handler
      (lambda (cause)
        (raise-not-an-id-token x cause))
    (lambda ()
      (cons (the-id-token-header (car x))
            (the-id-token-payload (cdr x))))))

(define-public (id-token? x)
  (false-if-exception
   (and (the-id-token x) #t)))

(define-public (make-id-token header payload)
  (the-id-token
   (cons header payload)))

(define-public (make-id-token-payload webid iss sub aud nonce exp iat)
  (when (date? exp)
    (set! exp (date->time-utc exp)))
  (when (time? exp)
    (set! exp (time-second exp)))
  (when (date? iat)
    (set! iat (date->time-utc iat)))
  (when (time? iat)
    (set! iat (time-second iat)))
  (when (uri? webid)
    (set! webid (uri->string webid)))
  (when (uri? iss)
    (set! iss (uri->string iss)))
  (when (uri? aud)
    (set! aud (uri->string aud)))
  (the-id-token-payload
   `((webid . ,webid)
     (iss . ,iss)
     (sub . ,sub)
     (aud . ,aud)
     (nonce . ,nonce)
     (exp . ,exp)
     (iat . ,iat))))

(define-public (id-token-header code)
  (car (the-id-token code)))

(define-public (id-token-payload code)
  (cdr (the-id-token code)))

(define-public (id-token-alg code)
  (when (id-token? code)
    (set! code (id-token-header code)))
  (jws-alg (the-id-token-header code)))

(define-public (id-token-webid code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (string->uri
   (assq-ref (the-id-token-payload code) 'webid)))

(define-public (id-token-iss code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (string->uri
   (assq-ref (the-id-token-payload code) 'iss)))

(define-public (id-token-sub code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (assq-ref (the-id-token-payload code) 'sub))

(define-public (id-token-aud code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (string->uri
   (assq-ref (the-id-token-payload code) 'aud)))

(define-public (id-token-nonce code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (assq-ref (the-id-token-payload code) 'nonce))

(define-public (id-token-exp code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (time-utc->date
   (make-time time-utc 0 (assq-ref
                          (the-id-token-payload code)
                          'exp))))

(define-public (id-token-iat code)
  (when (id-token? code)
    (set! code (id-token-payload code)))
  (time-utc->date
   (make-time time-utc 0 (assq-ref
                          (the-id-token-payload code)
                          'iat))))

(define*-public (id-token-decode str #:key (http-get http-get))
  (with-exception-handler
      (lambda (error)
        (raise-cannot-decode-id-token str error))
    (lambda ()
      (jws-decode
       str
       (lambda (token)
         (let ((iss (id-token-iss token)))
           (let ((cfg
                  (with-exception-handler
                      (lambda (error)
                        (raise-cannot-fetch-issuer-configuration iss error))
                    (lambda ()
                      (get-oidc-configuration
                       (uri-host iss)
                       #:userinfo (uri-userinfo iss)
                       #:port (uri-port iss)
                       #:http-get http-get)))))
             (with-exception-handler
                 (lambda (error)
                   (raise-cannot-fetch-jwks iss
                                            (oidc-configuration-jwks-uri cfg)
                                            error))
               (lambda ()
                 (oidc-configuration-jwks cfg #:http-get http-get))))))))))

(define-public (id-token-encode id-token key)
  (with-exception-handler
      (lambda (error)
        (raise-cannot-encode-id-token id-token key error))
    (lambda ()
      (jws-encode id-token key))))

(define*-public (issue-id-token
                 issuer-key
                 #:key
                 (alg #f)
                 (webid #f)
                 (iss #f)
                 (sub #f)
                 (aud #f)
                 (exp #f)
                 (iat #f))
  (unless sub
    (set! sub webid))
  (id-token-encode
   (make-id-token
    `((alg . ,(symbol->string alg)))
    (make-id-token-payload webid iss sub aud (stubs:random 12) exp iat))
   issuer-key))