summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/authorization-code.scm
blob: ebe97c4445eb249c5c3e55a365883edac496dd93 (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
(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 jti)
  #:use-module (web uri)
  #:use-module (srfi srfi-19))

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

(define-public (authorization-code-header? x)
  (false-if-exception
   (and (the-authorization-code-header x) #t)))

(define-public (the-authorization-code-payload x)
  (with-exception-handler
      (lambda (error)
        (raise-not-an-authorization-code-payload x error))
    (lambda ()
      (let ((x (the-jws-payload x)))
        (let ((exp (assq-ref x 'exp))
              (jti (assq-ref x 'jti))
              (webid (assq-ref x 'webid))
              (client-id (assq-ref x 'client_id)))
          (unless (integer? exp)
            (raise-incorrect-exp-field exp))
          (unless (string? jti)
            (raise-incorrect-jti-field jti))
          (unless (and (string? webid) (string->uri webid))
            (raise-incorrect-webid-field webid))
          (unless (and (string? client-id) (string->uri client-id))
            (raise-incorrect-client-id-field client-id))
          x)))))

(define-public (authorization-code-payload? x)
  (false-if-exception
   (and (the-authorization-code-payload x) #t)))

(define-public (the-authorization-code x)
  (with-exception-handler
      (lambda (error)
        (raise-not-an-authorization-code x error))
    (lambda ()
      (cons (the-authorization-code-header (car x))
            (the-authorization-code-payload (cdr x))))))

(define-public (authorization-code? x)
  (false-if-exception
   (and (the-authorization-code x) #t)))

(define-public (make-authorization-code header payload)
  (the-authorization-code (cons header payload)))

(define-public (make-authorization-code-header alg)
  (when (symbol? alg)
    (set! alg (symbol->string alg)))
  (the-authorization-code-header
   `((alg . ,alg))))

(define-public (make-authorization-code-payload exp jti sub aud)
  (when (date? exp)
    (set! exp (date->time-utc exp)))
  (when (time? exp)
    (set! exp (time-second exp)))
  (when (uri? sub)
    (set! sub (uri->string sub)))
  (when (uri? aud)
    (set! aud (uri->string aud)))
  (the-authorization-code-payload
   `((exp . ,exp)
     (jti . ,jti)
     (webid . ,sub)
     (client_id . ,aud))))

(define-public (authorization-code-header code)
  (car (the-authorization-code code)))

(define-public (authorization-code-payload code)
  (cdr (the-authorization-code code)))

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

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

(define-public (authorization-code-jti code)
  (when (authorization-code? code)
    (set! code (authorization-code-payload code)))
  (assq-ref (the-authorization-code-payload code) 'jti))

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

(define-public (authorization-code-client-id code)
  (when (authorization-code? code)
    (set! code (authorization-code-payload code)))
  (string->uri
   (assq-ref (the-authorization-code-payload code) 'client_id)))

(define-public (authorization-code-decode current-time jti-list str jwk)
  (when (date? current-time)
    (set! current-time (date->time-utc current-time)))
  (when (time? current-time)
    (set! current-time (time-second current-time)))
  (with-exception-handler
      (lambda (error)
        (raise-cannot-decode-authorization-code str error))
    (lambda ()
      (let ((code (the-authorization-code (jws-decode str (lambda (x) jwk)))))
        (let ((exp (time-second (date->time-utc (authorization-code-exp code)))))
          (unless (<= current-time exp)
            (raise-authorization-code-expired exp current-time))
          (unless (jti-check current-time (authorization-code-jti code)
                             jti-list
                             (- exp current-time))
            (with-exception-handler
                (lambda (error)
                  (raise-jti-found (authorization-code-jti code) error))
              (lambda ()
                (error "the jti-check function returned #f"))))
          code)))))

(define-public (authorization-code-encode authorization-code key)
  (with-exception-handler
      (lambda (error)
        (raise-cannot-encode-authorization-code authorization-code key error))
    (lambda ()
      (jws-encode authorization-code key))))

(define-public (issue-authorization-code alg jwk exp sub aud)
  (authorization-code-encode
   (make-authorization-code
    (make-authorization-code-header alg)
    (make-authorization-code-payload exp (stubs:random 12) sub aud))
   jwk))