summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/client/reverse-stubs.scm
blob: 5a68525f4a510b2d63a2d2090c504158f88899d8 (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
;; disfluid, implementation of the Solid specification
;; Copyright (C) 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 client reverse-stubs)
  #:use-module (webid-oidc client)
  #:use-module (webid-oidc client accounts)
  #:use-module (webid-oidc client application)
  #:use-module (webid-oidc jwk)
  #:use-module (webid-oidc oidc-id-token)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module (web uri)
  #:use-module (oop goops)
  #:use-module (ice-9 receive)
  #:duplicates (merge-generics)
  #:declarative? #t
  #:export
  (
   make-client
   get-client-id
   get-client-jwk
   get-client-redirect-uri

   make-account-full
   get-account-subject
   get-account-issuer
   get-account-key-pair
   get-account-id-token-header
   get-account-id-token
   get-account-access-token
   get-account-refresh-token
   ))

(define (make-client client-id jwk redirect-uri)
  (make <client>
    #:client-id client-id
    #:key-pair
    (if jwk
        (jwk->key (stubs:json-string->scm jwk))
        ;; Generate a new one:
        #t)
    #:redirect-uri redirect-uri))

(define (get-client-id client)
  (uri->string (client-id client)))

(define (get-key-pair client)
  (stubs:scm->json-string (key->jwk (key-pair client))))

(define (get-redirect-uri client)
  (uri->string (redirect-uri client)))

(define (make-account-full subject issuer key-pair id-token-header id-token access-token refresh-token)
  (make <account>
    #:subject (string->uri subject)
    #:issuer (string->uri issuer)
    #:key-pair (jwk->key (stubs:json-string->scm key-pair))
    #:id-token
    (and id-token-header id-token
         (make <id-token>
           #:jwt-header (stubs:json-string->scm id-token-header)
           #:jwt-payload (stubs:json-string->scm id-token)))
    #:access-token access-token
    #:refresh-token refresh-token))

(define (get-account-subject account)
  (uri->string (subject account)))

(define (get-account-issuer account)
  (uri->string (issuer account)))

(define (get-account-key-pair account)
  (stubs:scm->json-string (key->jwk (key-pair account))))

(define (get-account-id-token-header account)
  (receive (id-token-header id-token)
      (let ((id (id-token account)))
        (if id
            (token->jwt id)
            (values #f #f)))
    (and id-token-header
         (stubs:scm->json-string id-token-header))))

(define (get-account-id-token account)
  (receive (id-token-header id-token)
      (let ((id (id-token account)))
        (if id
            (token->jwt id)
            (values #f #f)))
    (and id-token
         (stubs:scm->json-string id-token))))

(define (get-account-access-token account)
  (access-token account))

(define (get-account-refresh-token account)
  (refresh-token account))