-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
226 lines (202 loc) · 5.45 KB
/
handlers.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
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
package webfinger
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"git.sr.ht/~mariusor/lw"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
"github.com/go-ap/filters"
"github.com/go-ap/processing"
)
type handler struct {
s []Storage
l lw.Logger
}
type Storage struct {
processing.ReadStore
Root vocab.Actor
}
func New(l lw.Logger, db ...Storage) handler {
return handler{s: db, l: l}
}
var actors = vocab.CollectionPath("actors")
func FilterName(name string) filters.Check {
return filters.NameIs(name)
}
func FilterURL(u string) filters.Check {
return filters.SameURL(vocab.IRI(u))
}
func FilterID(id string) filters.Check {
return filters.SameID(vocab.ID(id))
}
func LoadIRI(dbs []Storage, what vocab.IRI, checkFns ...filters.Check) (vocab.Item, error) {
var found vocab.Item
for _, db := range dbs {
serviceIRI := db.Root.GetLink()
result, err := db.Load(what, append(checkFns, filters.Authorized(serviceIRI))...)
if err != nil {
continue
}
err = vocab.OnObject(result, func(o *vocab.Object) error {
found = o
return nil
})
}
if !vocab.IsNil(found) {
return found, nil
}
return LoadActor(dbs, checkFns...)
}
func LoadActor(dbs []Storage, checkFns ...filters.Check) (vocab.Item, error) {
var found vocab.Item
var err error
for _, db := range dbs {
serviceIRI := db.Root.GetLink()
inCollection := actors.IRI(serviceIRI)
actors, err := db.Load(inCollection, append(checkFns, filters.Authorized(serviceIRI))...)
if err != nil {
return nil, errors.NewNotFound(err, "no actors found in collection: %s", inCollection)
}
if actors.IsCollection() {
err = vocab.OnCollectionIntf(actors, func(col vocab.CollectionInterface) error {
for _, act := range col.Collection() {
_ = vocab.OnActor(act, func(a *vocab.Actor) error {
found = a
return nil
})
}
return nil
})
} else {
err = vocab.OnActor(actors, func(a *vocab.Actor) error {
found = a
return nil
})
}
}
return found, err
}
func handleErr(l lw.Logger) func(r *http.Request, e error) errors.ErrorHandlerFn {
return func(r *http.Request, e error) errors.ErrorHandlerFn {
defer func(r *http.Request, e error) {
st := errors.HttpStatus(e)
l.Warnf("%s %s%s %d %s", r.Method, r.Host, r.RequestURI, st, http.StatusText(st))
}(r, e)
return errors.HandleError(e)
}
}
// HandleWebFinger serves /.well-known/webfinger/
func (h handler) HandleWebFinger(w http.ResponseWriter, r *http.Request) {
res := r.URL.Query().Get("resource")
if res == "" {
handleErr(h.l)(r, errors.NotFoundf("resource not found %s", res)).ServeHTTP(w, r)
return
}
hosts := make([]string, 0)
hosts = append(hosts, r.Host)
typ, handle := splitResourceString(res)
if typ == "" || handle == "" {
handleErr(h.l)(r, errors.BadRequestf("invalid resource %s", res)).ServeHTTP(w, r)
return
}
if typ == "acct" {
if strings.Contains(handle, "@") {
nh, hh := func(s string) (string, string) {
if ar := strings.Split(s, "@"); len(ar) == 2 {
return ar[0], ar[1]
}
return s, ""
}(handle)
hosts = append(hosts, hh)
handle = nh
}
}
wf := node{}
subject := res
var result vocab.Item
if typ == "acct" {
a, err := LoadActor(h.s, filters.Any(FilterName(handle), FilterURL(handle), FilterID(handle)))
if err != nil {
handleErr(h.l)(r, errors.NewNotFound(err, "resource not found %s", res)).ServeHTTP(w, r)
return
}
result = a
}
if typ == "https" {
ob, err := LoadIRI(h.s, vocab.IRI(res), filters.Any(FilterURL(res), FilterID(res)))
if err != nil {
handleErr(h.l)(r, errors.NewNotFound(err, "resource not found %s", res)).ServeHTTP(w, r)
return
}
result = ob
}
if result == nil || vocab.IsNil(result) {
handleErr(h.l)(r, errors.NotFoundf("resource not found %s", res)).ServeHTTP(w, r)
return
}
id := result.GetID()
wf.Subject = subject
wf.Links = []link{
{
Rel: "self",
Type: "application/activity+json",
Href: id.String(),
},
}
_ = vocab.OnObject(result, func(ob *vocab.Object) error {
if vocab.IsNil(ob.URL) {
return nil
}
urls := make(vocab.IRIs, 0)
if vocab.IsItemCollection(ob.URL) {
_ = vocab.OnItemCollection(ob.URL, func(col *vocab.ItemCollection) error {
for _, it := range col.Collection() {
_ = urls.Append(it.GetLink())
}
return nil
})
} else {
_ = urls.Append(ob.URL.GetLink())
}
for _, u := range urls {
if u.Equals(id, true) {
continue
}
url := u.String()
wf.Aliases = append(wf.Aliases, url)
wf.Links = append(wf.Links, link{
Rel: "https://webfinger.net/rel/profile-page",
Type: "text/html",
Href: url,
})
}
wf.Aliases = append(wf.Aliases, id.String())
return nil
})
dat, _ := json.Marshal(wf)
w.Header().Set("Content-Type", "application/jrd+json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(dat)
h.l.Debugf("%s %s%s %d %s", r.Method, r.Host, r.RequestURI, http.StatusOK, http.StatusText(http.StatusOK))
}
// HandleHostMeta serves /.well-known/host-meta
func (h handler) HandleHostMeta(w http.ResponseWriter, r *http.Request) {
hm := node{
Subject: "",
Aliases: nil,
Links: []link{
{
Rel: "lrdd",
Type: "application/xrd+json",
Template: fmt.Sprintf("https://%s/.well-known/node?resource={uri}", r.Host),
},
},
}
dat, _ := json.Marshal(hm)
w.Header().Set("Content-Type", "application/jrd+json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(dat)
h.l.Debugf("%s %s%s %d %s", r.Method, r.Host, r.RequestURI, http.StatusOK, http.StatusText(http.StatusOK))
}