webfinger.scm
| 1 | ;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu> |
| 2 | ;;;; |
| 3 | ;;;; This library is free software; you can redistribute it and/or |
| 4 | ;;;; modify it under the terms of the GNU Lesser General Public |
| 5 | ;;;; License as published by the Free Software Foundation; either |
| 6 | ;;;; version 3 of the License, or (at your option) any later version. |
| 7 | ;;;; |
| 8 | ;;;; This library is distributed in the hope that it will be useful, |
| 9 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | ;;;; Lesser General Public License for more details. |
| 12 | ;;;; |
| 13 | ;;;; You should have received a copy of the GNU Lesser General Public |
| 14 | ;;;; License along with this library; if not, write to the Free Software |
| 15 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | ;;;; |
| 17 | |
| 18 | (define-module (webfinger webfinger) |
| 19 | #:use-module (json) |
| 20 | #:use-module (rnrs bytevectors) |
| 21 | #:use-module (srfi srfi-9) |
| 22 | #:use-module (web client) |
| 23 | #:use-module (web uri) |
| 24 | #:export (jrd-record? |
| 25 | make-jrd-record |
| 26 | jrd-record-subject |
| 27 | jrd-record-aliases |
| 28 | jrd-record-properties |
| 29 | jrd-record-links |
| 30 | jrd-record->json |
| 31 | json->jrd-record |
| 32 | |
| 33 | make-link-record |
| 34 | link-record? |
| 35 | link-record-rel |
| 36 | link-record-type |
| 37 | link-record-href |
| 38 | link-record-titles |
| 39 | link-record-properties |
| 40 | link-record->json |
| 41 | json->link-record |
| 42 | |
| 43 | webfinger-query |
| 44 | find-actor-object |
| 45 | jrd->string)) |
| 46 | |
| 47 | (define-json-mapping <link-record> make-link-record link-record? |
| 48 | json->link-record <=> link-record->json |
| 49 | (rel link-record-rel) ; string |
| 50 | (type link-record-type) ; string |
| 51 | (href link-record-href) ; string |
| 52 | (titles link-record-titles) ; alist whose keys are languages or "und" and values ar strings |
| 53 | (properties link-record-properties)) ; alist whose keys and values are strings |
| 54 | |
| 55 | (define-json-mapping <jrd-record> make-jrd-record jrd-record? |
| 56 | json->jrd-record <=> jrd-record->json |
| 57 | (subject jrd-record-subject) ; string |
| 58 | (aliases jrd-record-aliases "aliases" ; string list |
| 59 | (lambda (val) |
| 60 | (if val (array->list val) '())) |
| 61 | (lambda (val) (list->array 1 val))) |
| 62 | (properties jrd-record-properties) ; alist whose keys and values are strings |
| 63 | (links jrd-record-links "links" ; list of links |
| 64 | (lambda (val) |
| 65 | (if val (map json->link-record (array->list val)) '())) |
| 66 | (lambda (val) (list->array 1 (map link-record->json val))))) |
| 67 | |
| 68 | (define (download-json uri) |
| 69 | (json-string->scm |
| 70 | (call-with-values |
| 71 | (lambda () |
| 72 | (http-get uri #:headers '((Accept . "application/jrd+json")))) |
| 73 | (lambda (hdr body) |
| 74 | (utf8->string body))))) |
| 75 | |
| 76 | (define* (webfinger-query server resource #:optional rel) |
| 77 | "Queries a webfinger @var{server} for @var{resource}. This follows the |
| 78 | webfinger specification at @url{https://tools.ietf.org/html/rfc7033}. Returns |
| 79 | a @code{jrd-record} object." |
| 80 | (let* ((uri (or (string->uri server) (build-uri 'https #:host server))) |
| 81 | (query (string-append "resource=" resource |
| 82 | (if rel |
| 83 | (apply |
| 84 | string-append |
| 85 | (map |
| 86 | (lambda (rel) |
| 87 | (string-append "rel=" rel)) |
| 88 | rel)) |
| 89 | ""))) |
| 90 | (uri (build-uri |
| 91 | 'https |
| 92 | #:host (uri-host uri) |
| 93 | #:path "/.well-known/webfinger" |
| 94 | #:query query))) |
| 95 | (json->jrd-record (download-json uri)))) |
| 96 | |
| 97 | (define (find-actor-object user server) |
| 98 | "Find the actor object of @var{user} on @var{server}. This uses the webfinger |
| 99 | protocol to find the user actor object URL, which does not have any default |
| 100 | or common location. This is used in many services that implement the |
| 101 | ActivityPub protocol." |
| 102 | (let* ((server (if (string->uri server) (uri-host (string->uri server)) server)) |
| 103 | (resource (webfinger-query server (string-append "acct:" user "@" |
| 104 | server))) |
| 105 | (links (jrd-record-links resource))) |
| 106 | (link-record-href |
| 107 | (car (filter |
| 108 | (lambda (link) |
| 109 | (equal? (link-record-type link) "application/activity+json")) |
| 110 | links))))) |
| 111 | |
| 112 | (define (jrd->string jrd) |
| 113 | (scm->json-string |
| 114 | (let ((subject (jrd-record-subject jrd)) |
| 115 | (aliases (jrd-record-aliases jrd)) |
| 116 | (properties (jrd-record-properties jrd)) |
| 117 | (links (jrd-record-links jrd))) |
| 118 | `(("subject" . ,subject) |
| 119 | ,@(if (and aliases (not (null? aliases))) |
| 120 | `(("aliases" . ,(list->array 1 aliases))) |
| 121 | '()) |
| 122 | ,@(if (and properties (not (null? properties))) |
| 123 | `(("properties" . ,properties)) |
| 124 | '()) |
| 125 | ("links" . ,(list->array 1 (map link->scm links))))))) |
| 126 | |
| 127 | (define (link->scm link) |
| 128 | (let ((rel (link-record-rel link)) |
| 129 | (type (link-record-type link)) |
| 130 | (href (link-record-href link)) |
| 131 | (titles (link-record-titles link)) |
| 132 | (properties (link-record-properties link))) |
| 133 | `(("rel" . ,rel) |
| 134 | ,@(if type `(("href" . ,href)) '()) |
| 135 | ("href" . ,href) |
| 136 | ,@(if type `(("titles" . ,titles)) '()) |
| 137 | ,@(if type `(("properties" . ,properties)) '())))) |
| 138 |