summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/hello-world.scm
blob: 68d7644685c51abae411c3d9995f951b11e9b41f (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
;; disfluis, 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 hello-world)
  #:use-module (webid-oidc server endpoint)
  #:use-module (webid-oidc server endpoint hello)
  #:use-module (webid-oidc server log)
  #:use-module (webid-oidc web-i18n)
  #:use-module ((webid-oidc config) #:prefix cfg:)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web uri)
  #:use-module (web server)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 i18n)
  #:use-module (ice-9 getopt-long)
  #:use-module (ice-9 suspendable-ports)
  #:use-module (ice-9 match)
  #:use-module (sxml simple)
  #:use-module (sxml match)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (oop goops)
  #:duplicates (merge-generics)
  #:declarative? #t)

(define-public (main)
  (setvbuf (current-output-port) 'none)
  (setvbuf (current-error-port) 'none)
  (setlocale LC_ALL "")
  (bindtextdomain cfg:package cfg:localedir)
  (textdomain cfg:package)
  (let ((version-sym
         (string->symbol (G_ "command-line|version")))
        (complete-corresponding-source-sym
         (string->symbol (G_ "command-line|complete-corresponding-source")))
        (help-sym
         (string->symbol (G_ "command-line|help")))
        (port-sym
         (string->symbol (G_ "command-line|port")))
        (log-file-sym
         (string->symbol (G_ "command-line|log-file")))
        (error-file-sym
         (string->symbol (G_ "command-line|error-file"))))
    (let ((options
           (let ((option-spec
                  `((,complete-corresponding-source-sym (single-char #\S) (value #t))
                    (,version-sym (single-char #\v) (value #f))
                    (,help-sym (single-char #\h) (value #f))
                    (,log-file-sym (single-char #\l) (value #t))
                    (,error-file-sym (single-char #\e) (value #t))
                    (,port-sym (single-char #\p) (value #t)))))
             (getopt-long (command-line) option-spec))))
      (cond
       ((option-ref options help-sym #f)
        (format #t (G_ "~a [OPTIONS]...

Display your identity contained in the XXX-Agent header.

This program is covered by the GNU Affero GPL, version 3 or
later. This license requires you to provide a way for any user over
the network to download the complete corresponding source code (with
your modifications) at no cost. The server adds a \"Source:\" header
to all responses.

Options:
  -S MEANS, --~a=MEANS:
      specify a way to download the complete corresponding source
      code. For instance, this would be an URI pointing to a tarball.
  -h, --~a:
      display this help message and exit.
  -v, --~a:
      display the version information (~a) and exit.
  -p PORT, --~a=PORT:
      set the port to bind.
  -l FILE.log, --~a=FILE.log:
    redirect the program standard output to FILE.log.
  -e FILE.err, --~a=FILE.err:
    redirect the program errors to FILE.err.
")
                (car (command-line))
                complete-corresponding-source-sym
                help-sym version-sym
                cfg:version
                port-sym
                log-file-sym
                error-file-sym))
       ((option-ref options version-sym #f)
        (format #t (G_ "~a version ~a\n")
                cfg:package cfg:version))
       (else
        (let ((port-string
               (option-ref options port-sym "8080"))
              (means-string
               (let ((str (option-ref options complete-corresponding-source-sym #f)))
                 (unless str
                   (format (current-error-port)
                           (G_ "You are legally required to link to the complete corresponding source code.\n"))
                   (exit 1))
                 str))
              (log-file (option-ref options log-file-sym #f))
              (error-file (option-ref options error-file-sym #f)))
          (unless (and (string->number port-string)
                       (integer? (string->number port-string))
                       (>= (string->number port-string) 0)
                       (<= (string->number port-string) 65535))
            (format (current-error-port)
                    (G_ "The port should be a number between 0 and 65535.\n"))
            (exit 1))
          (define greeter (make <greeter>))
          (let ((handler
                 (lambda (request request-body)
                   (when log-file
                     (prepare-log-file log-file))
                   (when error-file
                     (prepare-error-file error-file))
                   (parameterize ((web-locale request))
                     (with-exception-handler
                         (lambda (exn)
                           (unless (web-exception? exn)
                             (raise-exception exn))
                           (values
                            (build-response
                             #:code (web-exception-code exn)
                             #:reason-phrase (web-exception-reason-phrase exn)
                             #:headers `((content-type application/xhtml+xml)))
                            (call-with-output-string
                              (cute sxml->xml
                                    `(*TOP*
                                      (*PI* xml "version=\"1.0\" encoding=\"utf-8\"")
                                      (html (@ (xmlns "http://www.w3.org/1999/xhtml")
                                               (xml:lang ,(W_ "xml-lang|en")))
                                            (body
                                             ,(call-with-input-string
                                                  (format #f (W_ "<h1>Please authenticate</h1>"))
                                                xml->sxml)
                                             ,(if (user-message? exn)
                                                  (user-message-sxml exn)
                                                  (call-with-input-string
                                                      (format #f (W_ "<p>No more information.</p>"))
                                                    xml->sxml)))))
                                    <>))))
                       (lambda ()
                         (set! request
                               (let ((user
                                      (match (assq-ref (request-headers request) 'xxx-agent)
                                        ((? string? (= string->uri (? uri? uri)))
                                         uri)
                                        (else #f))))
                                 (build-request (request-uri request)
                                                #:meta (if user `((user . ,user)) '())
                                                #:headers (request-headers request)
                                                #:version (request-version request)
                                                #:method (request-method request))))
                         (receive (response response-body response-meta)
                             (handle greeter request request-body)
                           (when (port? response-body)
                             (set! response-body
                                   (read-response-body response)))
                           (values response response-body)))
                       #:unwind? #t)))))
            (install-suspendable-ports!)
            (run-server handler 'http (list #:port (string->number port-string))))))))))