summaryrefslogtreecommitdiff
path: root/tests/token-endpoint-issue.scm
blob: 6d1cda65727954af7ff3ee804235732ba492a4f3 (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
;; 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 (tests token-endpoint-refresh)
  #:use-module (webid-oidc server endpoint)
  #:use-module (webid-oidc server endpoint identity-provider)
  #:use-module (webid-oidc authorization-code)
  #:use-module (webid-oidc refresh-token)
  #:use-module (webid-oidc dpop-proof)
  #:use-module (webid-oidc jwk)
  #:use-module (webid-oidc access-token)
  #:use-module (webid-oidc jws)
  #:use-module (webid-oidc jti)
  #:use-module (webid-oidc testing)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module ((webid-oidc parameters) #:prefix p:)
  #:use-module (web uri)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (srfi srfi-19)
  #:use-module (web response)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 receive)
  #:use-module (oop goops)
  #:duplicates (merge-generics)
  #:declarative? #t)

(with-test-environment
 "token-endpoint-issue"
 (lambda ()
   (define key (generate-key #:n-size 2048))
   (call-with-output-file (string-append (p:data-home) "/key-file.jwk")
     (lambda (port)
       (stubs:scm->json
        (key->jwk key)
        port
        #:pretty #t)))
   (define client-key (generate-key #:n-size 2048))
   (define subject (string->uri "https://token-endpoint-issue.scm/profile/card#me"))
   (define client (string->uri "https://token-endpoint-issue.scm/client/card#app"))
   (define issuer (string->uri "https://issuer.token-endpoint-issue.scm"))
   (define endpoint
     (make <token-endpoint>
       #:issuer "https://issuer.token-endpoint-issue.scm"
       #:key-file (string-append (p:data-home) "/key-file.jwk")))
   (define authz
     (parameterize ((p:current-date 0))
       (issue <authorization-code>
              key
              #:webid subject
              #:client-id client)))
   (with-exception-handler
       (lambda (exn)
         (unless (and (web-exception? exn)
                      (eqv? (web-exception-code exn) 400))
           (raise-exception
            (make-exception
             (make-exception-with-message
              (format #f "the error code should be 400"))
             exn))))
     (lambda ()
       ;; The refresh token is fake!
       (let ((dpop
              (parameterize ((p:current-date 0))
                (issue <dpop-proof>
                       client-key
                       #:jwk (public-key client-key)
                       #:htm 'POST
                       #:htu (string->uri
                              "https://token-endpoint-issue.scm/token")))))
         (parameterize ((p:current-date 0))
           (handle endpoint
                   (build-request (string->uri
                                   "http://localhost:8080/token")
                                  #:headers `((content-type application/x-www-form-urlencoded)
                                              (dpop . ,dpop))
                                  #:method 'POST
                                  #:port #t)
                   "grant_type=authorization_code&code=fake")))
       (exit 3))
     #:unwind? #t
     #:unwind-for-type &web-exception)
   (receive (response response-body . _)
       (let ((dpop
              (parameterize ((p:current-date 10))
                (issue <dpop-proof>
                       client-key
                       #:jwk (public-key client-key)
                       #:htm 'POST
                       #:htu (string->uri
                              "https://token-endpoint-issue.scm/token")))))
         (parameterize ((p:current-date 10))
           (handle endpoint
                   (build-request (string->uri
                                   "http://localhost:8080/token")
                                  #:headers `((content-type application/x-www-form-urlencoded)
                                              (dpop . ,dpop))
                                  #:method 'POST
                                  #:port #t)
                   (string-append "grant_type=authorization_code&code=" authz))))
     (unless (eq? (response-code response) 200)
       (exit 4))
     (unless (eq? (car (response-content-type response)) 'application/json)
       (exit 5))
     (let ((response (stubs:json-string->scm response-body)))
       (let ((access-token-enc (assq-ref response 'access_token))
             (refresh-token-enc (assq-ref response 'refresh_token)))
         (unless access-token-enc
           (exit 6))
         (unless refresh-token-enc
           (exit 7))
         (let ((access-token
                (parameterize ((p:current-date 20)
                               (p:anonymous-http-request
                                (lambda* (uri . args)
                                  (cond
                                   ((equal? uri (string->uri "https://issuer.token-endpoint-issue.scm/.well-known/openid-configuration"))
                                    (values (build-response #:headers '((content-type application/json)))
                                            "{
  \"jwks_uri\": \"https://token-endpoint-issue.scm/keys\",
  \"token_endpoint\": \"https://token-endpoint-issue.scm/token\",
  \"authorization_endpoint\": \"https://token-endpoint-issue.scm/authorize\",
  \"solid_oidc_supported\": \"https://solidproject.org/TR/solid-oidc\"
}"))
                                   ((equal? uri (string->uri "https://token-endpoint-issue.scm/keys"))
                                    (values (build-response #:headers '((content-type application/json)))
                                            (stubs:scm->json-string `((keys . ,(list->vector (list (key->jwk key))))))))
                                   (else
                                    (format (current-error-port) "Unknown URI: ~s\n" (uri->string uri))
                                    (exit 11))))))
                  (decode <access-token> access-token-enc))))
           (unless access-token
             (exit 8))
           (let ((access-token-cnf/jkt (cnf/jkt access-token)))
             (unless access-token-cnf/jkt
               (exit 9))
             (unless (string=? access-token-cnf/jkt (jkt client-key))
               (exit 10)))))))))