summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/server/create.scm
blob: dc12ff9429ad254997ee9cd8ab775d27e4fef5bc (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(define-module (webid-oidc server create)
  #:use-module (webid-oidc errors)
  #:use-module (webid-oidc server resource path)
  #:use-module (webid-oidc server resource content)
  #:use-module (webid-oidc server read)
  #:use-module (webid-oidc cache)
  #:use-module (webid-oidc fetch)
  #:use-module (webid-oidc rdf-index)
  #:use-module (webid-oidc server resource wac)
  #:use-module ((webid-oidc stubs) #:prefix stubs:)
  #:use-module (webid-oidc rdf-index)
  #:use-module ((webid-oidc refresh-token) #:prefix refresh:)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (rdf rdf)
  #:use-module (turtle tordf)
  #:use-module (turtle fromrdf)
  #:use-module (rnrs bytevectors)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 iconv)
  #:use-module (ice-9 textual-ports)
  #:use-module (ice-9 binary-ports)
  #:use-module (ice-9 threads)
  #:use-module (rnrs bytevectors)
  #:use-module (oop goops)
  #:export
  (

   create
   create-root

   ))

(define (without-containment-triples doc-uri content-type content)
  (case content-type
    ((text/turtle)
     #t)
    (else
     (raise-exception (make-unsupported-media-type content-type))))
  (let ((graph (fetch
                doc-uri
                #:http-get
                (lambda (uri . args)
                  (values
                   (build-response #:headers `((content-type ,content-type)))
                   content)))))
    (with-index
     graph
     (lambda (rdf-match)
       (unless (null? (rdf-match (uri->string doc-uri)
                                 "http://www.w3.org/ns/auth/acl#contains"
                                 #f))
         (raise-exception (make-incorrect-containment-triples
                           (uri-path doc-uri))))))))

(define (types-indicate-container? types)
  (and (not (null? types))
       (let ((next (car types)))
         (when (uri? next)
           (set! next (uri->string next)))
         (or (equal? next "http://www.w3.org/ns/ldp#BasicContainer")
             (types-indicate-container? (cdr types))))))

(define* (create server-name owner user container types slug content-type content
                 #:key
                 (http-get http-get))
  (check-acl-can-append server-name container owner user #:http-get http-get)
  (unless (and slug (not (equal? slug "")))
    (set! slug (stubs:random 12)))
  (when (string-contains slug "/")
    (let ((i (string-contains slug "/")))
      (set! slug (substring slug 0 i))))
  (let ((container? (types-indicate-container? types)))
    (let ((doc-uri
           (build-uri
            (uri-scheme server-name)
            #:userinfo (uri-userinfo server-name)
            #:host (uri-host server-name)
            #:port (uri-port server-name)
            #:path
            (string-append
             "/"
             (encode-and-join-uri-path
              (append (split-and-decode-uri-path container)
                      (list slug)))
             ;; There’s no risk to have // here, because slug is
             ;; non-empty.
             (if container? "/" "")))))
      (when (auxiliary-path? (uri-path doc-uri))
        (raise-exception (make-path-is-auxiliary (uri-path doc-uri))))
      (when container?
        (without-containment-triples doc-uri content-type content))
      (with-session
       (lambda (load-content-type load-contained load-static-content
                                  do-create do-delete)
         (catch 'slug-already-exists
           (lambda ()
             (update-path
              (uri-path doc-uri)
              (lambda (etag auxiliary)
                (when etag
                  (throw 'slug-already-exists))
                (values
                 (do-create content-type (and container? '()) content)
                 '()))
              load-content-type load-contained load-static-content
              do-create do-delete)
             doc-uri)
           (lambda error
             (create server-name owner user container types
                     (string-append slug "-" (stubs:random 12))
                     content-type content
                     #:http-get http-get))))))))

(define (create-root server-name owner)
  (define (fix-angle-aux accu chars)
    (if (null? chars)
        (list->string (reverse accu))
        (let ((next (car chars))
              (rest (cdr chars)))
          (let ((next-accu
                 (if (eqv? next #\>)
                     (reverse (string->list "%3E"))
                     (list next))))
            (fix-angle-aux (append next-accu accu) rest)))))
  (define (fix-angle str)
    (fix-angle-aux '() (string->list str)))
  (with-session
   (lambda (load-content-type load-contained load-static-content
                              do-create do-delete)
     (catch 'already-exists
       (lambda ()
         (update-path
          "/"
          (lambda (etag auxiliary)
            (when etag
              (throw 'already-exists))
            (let ((root-uri
                   (build-uri
                    (uri-scheme server-name)
                    #:userinfo (uri-userinfo server-name)
                    #:host (uri-host server-name)
                    #:port (uri-port server-name)
                    #:path "/")))
              (values
               (do-create 'text/turtle '() "")
               (list
                (cons (string->uri "http://www.w3.org/ns/auth/acl#accessControl")
                      (do-create 'text/turtle #f
                                 (format #f "@prefix acl: <http://www.w3.org/ns/auth/acl#> .

<#default>
  a acl:Authorization;
  acl:accessTo <~a>;
  acl:agent <~a>;
  acl:mode acl:Read, acl:Write, acl:Control;
  acl:default <~a/>.
"
                                         (fix-angle (uri->string root-uri))
                                         (fix-angle (uri->string owner))
                                         (fix-angle (uri->string root-uri)))))))))
          load-content-type load-contained load-static-content
          do-create do-delete)
         #t)
       (lambda error
         #f))
     (when (and (equal? (uri-scheme server-name)
                        (uri-scheme owner))
                (equal? (uri-userinfo server-name)
                        (uri-userinfo owner))
                (equal? (uri-host server-name)
                        (uri-host owner))
                (equal? (uri-port server-name)
                        (uri-port owner)))
       ;; We need to make sure that the profile exists
       (catch 'already-exists
         (lambda ()
           (update-path
            (uri-path owner)
            (lambda (etag auxiliary)
              (when etag
                (throw 'already-exists))
              (values
               (do-create 'text/turtle #f
                          (format #f "@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix ldp: <http://www.w3.org/ns/ldp#> .

<~a~a> a foaf:Person;
  ldp:inbox </inbox/> .
"
                                  (if (uri-query owner)
                                      (string-append
                                       "?"
                                       (fix-angle
                                        (uri-encode (uri-query owner))))
                                      "")
                                  (if (uri-fragment owner)
                                      (string-append
                                       "#"
                                       (fix-angle
                                        (uri-encode (uri-fragment owner))))
                                      "")))
               (list
                (cons (string->uri "http://www.w3.org/ns/auth/acl#accessControl")
                      (let ((doc-uri
                             (build-uri
                              (uri-scheme owner)
                              #:userinfo (uri-userinfo owner)
                              #:host (uri-host owner)
                              #:port (uri-port owner)
                              #:path (uri-path owner))))
                        (do-create 'text/turtle #f
                                   (format #f "@prefix acl: <http://www.w3.org/ns/auth/acl#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<#public>
  a acl:Authorization;
  acl:accessTo <~a>;
  acl:agentClass foaf:Agent;
  acl:mode acl:Read.

<#default>
  a acl:Authorization;
  acl:accessTo <~a>;
  acl:agent <~a>;
  acl:mode acl:Read, acl:Write, acl:Control.
"
                                           (fix-angle (uri->string doc-uri))
                                           (fix-angle (uri->string doc-uri))
                                           (fix-angle (uri->string owner)))))))))
            load-content-type load-contained load-static-content
            do-create do-delete
            #:create-intermediate-containers? #t))
         (lambda error #f))
       ;; Also, we need an inbox: look up the profile to know where to
       ;; put it
       (receive (main-profile-etag rest)
           (read-path (uri-path owner))
         (when (and main-profile-etag
                    (eq? (load-content-type main-profile-etag) 'text/turtle))
           (false-if-exception
            (with-index
             (fetch 
              (build-uri
               (uri-scheme owner)
               #:userinfo (uri-userinfo owner)
               #:host (uri-host owner)
               #:port (uri-port owner)
               #:path (uri-path owner))
              #:http-get
              (lambda args
                (values
                 (build-response #:headers '((content-type text/turtle)))
                 (load-static-content main-profile-etag))))
             (lambda (rdf-match)
               (let ((inboxes
                      (map rdf-triple-object
                           (rdf-match
                            (string->uri owner)
                            "http://www.w3.org/ns/ldp#inbox"
                            #f))))
                 (for-each
                  (lambda (inbox-path)
                    (catch 'already-exists
                      (lambda ()
                        (update-path
                         inbox-path
                         (lambda (etag auxiliary)
                           (when etag
                             (throw 'already-exists))
                           (values
                            (do-create 'text/turtle '() "")
                            (list
                             (cons (string->uri "http://www.w3.org/ns/auth/acl#accessControl")
                                   (do-create 'text/turtle #f
                                              (format #f "@prefix acl: <http://www.w3.org/ns/auth/acl#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<#public>
  a acl:Authorization;
  acl:accessTo <~a>;
  acl:agentClass foaf:Agent;
  acl:mode acl:Read.

<#default>
  a acl:Authorization;
  acl:accessTo <~a>;
  acl:agent <~a>;
  acl:mode acl:Read, acl:Write, acl:Control.
"
                                                      (fix-angle inbox-path)
                                                      (fix-angle inbox-path)
                                                      (fix-angle (uri->string owner))))))))
                         load-content-type load-contained load-static-content
                         do-create do-delete
                         #:create-intermediate-containers? #t))
                      (lambda error #f)))
                  inboxes)))))))))))