-
Notifications
You must be signed in to change notification settings - Fork 5
/
webfinger.go
82 lines (64 loc) · 1.75 KB
/
webfinger.go
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
/*
Implements the webfinger protocol.
https://tools.ietf.org/html/rfc7033
This protocol allows one website to ask
another "Do you have an object with this id"?
Webfinger always lives at `/.well-known/webfinger`
and it takes queries like this:
```
/.well-known/webfinger?resource=acct:joe@example.org
```
Webfinger is required for interop'ing with Mastodon
and possibly other sites.
*/
package webfinger
import (
"encoding/json"
"errors"
"net/http"
"strings"
)
func getAddressFromResource(resource string) (string, error) {
fragments := strings.Split(resource, ":")
if fragments[0] != "acct" {
return "", errors.New("resource did not start with 'acct:'")
}
return fragments[1], nil
}
// Get returns a webfinger response
func Get(w http.ResponseWriter, r *http.Request) {
// expect a request like ?resource=acct:joe@example.org
resource := r.URL.Query().Get("resource")
if resource == "" {
http.Error(w, "missing 'resource' query parameter in webfinger", http.StatusBadRequest)
return
}
// acct:foo@dogs.com => foo@dogs.com
address, err := getAddressFromResource(resource)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
actor, err := atAddress(address)
if err != nil {
// Poor formated errors are user errors (ie: 400)
if _, ok := err.(*badAddressError); ok {
http.Error(w, "incorrect address format", http.StatusBadRequest)
return
}
// other errors are just 500s
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 404s
if actor == nil {
http.Error(w, "no such account", http.StatusNotFound)
}
bytes, err := json.Marshal(actor)
// This _really_ should not happen.
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bytes)
}