;; 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 .
(define-module (tests resource-server)
#:use-module (webid-oidc provider-confirmation)
#:use-module (webid-oidc server endpoint)
#:use-module (webid-oidc server endpoint authentication)
#:use-module (webid-oidc jti)
#:use-module (webid-oidc jwk)
#:use-module (webid-oidc jws)
#:use-module (webid-oidc oidc-configuration)
#:use-module (webid-oidc access-token)
#:use-module (webid-oidc dpop-proof)
#:use-module (webid-oidc testing)
#:use-module ((webid-oidc parameters) #:prefix p:)
#:use-module (web uri)
#:use-module (web request)
#:use-module (srfi srfi-19)
#:use-module (web response)
#:use-module (ice-9 optargs)
#:use-module (ice-9 control)
#:use-module (ice-9 receive)
#:use-module (oop goops)
#:duplicates (merge-generics))
(define-class ())
(define return (make-parameter values))
(define-method (handle (backend ) request request-body)
((return) (assq-ref (request-meta request) 'user)))
(with-test-environment
"resource-server"
(lambda ()
(define client-key (generate-key #:n-size 2048))
(define idp-key (generate-key #:n-size 2048))
(define jwks (make #:keys (list idp-key)))
(define jwks-uri (string->uri "https://identity.provider/keys"))
(define oidc-config
(make
#:jwks-uri jwks-uri
#:authorization-endpoint "https://identity.provider/authorize"
#:token-endpoint "https://identity.provider/token"
#:solid-oidc-supported "https://solidproject.org/TR/solid-oidc"))
(define oidc-config-uri
(string->uri
"https://identity.provider/.well-known/openid-configuration"))
(define subject (string->uri "https://identity.provider/subject#me"))
(define* (http-get uri #:key (headers '()))
(define exp (time-utc->date (make-time time-utc 0 3600)))
(cond ((equal? uri oidc-config-uri)
(serve oidc-config exp))
((equal? uri jwks-uri)
(serve jwks exp))
(else (exit 1))))
(define access-token
(parameterize ((p:current-date 10))
(issue
idp-key
#:webid subject
#:iss (string->uri "https://identity.provider")
#:client-key client-key
#:client-id (string->uri "https://client"))))
(define uri (string->uri "https://resource.server/resource"))
(define server-uri (string->uri "https://resource.server/"))
(define method 'GET)
(define dpop-proof
(parameterize ((p:current-date 15))
(issue
client-key
#:jwk (public-key client-key)
#:htm method
#:htu uri
#:access-token access-token)))
(define rq
(call-with-input-string
(format #f "GET /resource HTTP/1.1\r\n\
Host: resource.server\r\n\
User-Agent: Test Suite\r\n\
Upgrade-Insecure-Requests: 1\r\n\
Cache-Control: max-age=0\r\n\
Authorization: DPoP ~a\r\n\
DPoP: ~a\r\n\r\n"
access-token
dpop-proof)
read-request))
(define rq-body "")
(define authenticator
(make
#:backend (make )
#:server-uri server-uri))
(define parsed
(let/ec ret
(parameterize ((p:current-date 20)
(p:anonymous-http-request http-get)
(return ret))
(handle authenticator rq rq-body))))
(unless (uri? parsed)
(exit 2))
(unless (equal? parsed subject)
(exit 3))))