-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.go
294 lines (243 loc) · 8.2 KB
/
account.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
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
package remote
import (
"context"
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"github.com/shigde/sfu/internal/activitypub/models"
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/activity/streams/vocab"
)
func FetchAccountAsActor(ctx context.Context, req *http.Request) (*models.Actor, error) {
// Do not support redirects.
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("doing request: %w", err)
}
b, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("reading boosy request: %w", err)
}
var rawActivity map[string]interface{}
if err := json.Unmarshal(b, &rawActivity); err != nil {
return nil, fmt.Errorf("unmarshalling request body: %w", err)
}
t, err := streams.ToType(ctx, rawActivity)
if err != nil {
return nil, fmt.Errorf("determinding type: %w", err)
}
accountable, ok := t.(ActivityStreamAccount)
if !ok {
return nil, fmt.Errorf("casting type %T but is not a pub.Activity", t)
}
if accountable.GetJSONLDId() == nil {
return nil, fmt.Errorf("incoming Activity %s did not have required id property set", accountable.GetTypeName())
}
actor, err := createActor(accountable)
if err != nil {
return nil, fmt.Errorf("casting to account activity pub activity")
}
return actor, nil
}
func createActor(accountable ActivityStreamAccount) (*models.Actor, error) {
// first check if we actually already know this account
uriProp := accountable.GetJSONLDId()
if uriProp == nil || !uriProp.IsIRI() {
return nil, errors.New("no id property found on person, or id was not an iri")
}
uri := uriProp.GetIRI()
// we don't know the account, or we're being told to update it, so we need to generate it from the person -- at least we already have the URI!
acct := &models.Actor{}
acct.ActorIri = uri.String()
// Username aka preferredUsername
// We need this one so bail if it's not set.
username, err := extractPreferredUsername(accountable)
if err != nil {
return nil, fmt.Errorf("couldn't extract username: %s", err)
}
acct.PreferredUsername = username
acct.ActorType = accountable.GetTypeName()
// InboxURI
if accountable.GetActivityStreamsInbox() != nil && accountable.GetActivityStreamsInbox().GetIRI() != nil {
acct.InboxIri = accountable.GetActivityStreamsInbox().GetIRI().String()
}
// SharedInboxURI:
// only trust shared inbox if it has at least two domains,
// from the right, in common with the domain of the account
if sharedInboxURI := extractSharedInbox(accountable); sharedInboxURI != nil {
acct.SharedInboxIri = sharedInboxURI.String()
}
// OutboxURI
if accountable.GetActivityStreamsOutbox() != nil && accountable.GetActivityStreamsOutbox().GetIRI() != nil {
acct.OutboxIri = accountable.GetActivityStreamsOutbox().GetIRI().String()
}
// FollowingURI
if accountable.GetActivityStreamsFollowing() != nil && accountable.GetActivityStreamsFollowing().GetIRI() != nil {
acct.FollowingIri = accountable.GetActivityStreamsFollowing().GetIRI().String()
}
// FollowersURI
if accountable.GetActivityStreamsFollowers() != nil && accountable.GetActivityStreamsFollowers().GetIRI() != nil {
acct.FollowersIri = accountable.GetActivityStreamsFollowers().GetIRI().String()
}
// publicKey
pkey, pkeyURL, pkeyOwnerID, err := extractPublicKey(accountable)
if err != nil {
return nil, fmt.Errorf("couldn't get public key for person %s: %s", uri.String(), err)
}
if pkeyOwnerID.String() != acct.ActorIri {
return nil, fmt.Errorf("public key %s was owned by %s and not by %s", pkeyURL, pkeyOwnerID, acct.ActorIri)
}
if acct.PublicKey, err = exportRsaPublicKeyAsPemStr(pkey); err != nil {
return nil, fmt.Errorf("converting puplic key as string get wrong")
}
return acct, nil
}
func extractPreferredUsername(i withPreferredUsername) (string, error) {
u := i.GetActivityStreamsPreferredUsername()
if u == nil || !u.IsXMLSchemaString() {
return "", errors.New("preferredUsername nil or not a string")
}
if u.GetXMLSchemaString() == "" {
return "", errors.New("preferredUsername was empty")
}
return u.GetXMLSchemaString(), nil
}
func extractSharedInbox(withEndpoints withEndpoints) *url.URL {
endpointsProp := withEndpoints.GetActivityStreamsEndpoints()
if endpointsProp == nil {
return nil
}
for iter := endpointsProp.Begin(); iter != endpointsProp.End(); iter = iter.Next() {
if !iter.IsActivityStreamsEndpoints() {
continue
}
endpoints := iter.Get()
if endpoints == nil {
continue
}
sharedInboxProp := endpoints.GetActivityStreamsSharedInbox()
if sharedInboxProp == nil || !sharedInboxProp.IsIRI() {
continue
}
return sharedInboxProp.GetIRI()
}
return nil
}
func extractPublicKey(i withPublicKey) (
*rsa.PublicKey, // pubkey
*url.URL, // pubkey ID
*url.URL, // pubkey owner
error,
) {
pubKeyProp := i.GetW3IDSecurityV1PublicKey()
if pubKeyProp == nil {
return nil, nil, nil, errors.New("public key property was nil")
}
for iter := pubKeyProp.Begin(); iter != pubKeyProp.End(); iter = iter.Next() {
if !iter.IsW3IDSecurityV1PublicKey() {
continue
}
pkey := iter.Get()
if pkey == nil {
continue
}
pubKeyID, err := pub.GetId(pkey)
if err != nil {
continue
}
pubKeyOwnerProp := pkey.GetW3IDSecurityV1Owner()
if pubKeyOwnerProp == nil {
continue
}
pubKeyOwner := pubKeyOwnerProp.GetIRI()
if pubKeyOwner == nil {
continue
}
pubKeyPemProp := pkey.GetW3IDSecurityV1PublicKeyPem()
if pubKeyPemProp == nil {
continue
}
pkeyPem := pubKeyPemProp.Get()
if pkeyPem == "" {
continue
}
block, _ := pem.Decode([]byte(pkeyPem))
if block == nil {
continue
}
var p crypto.PublicKey
switch block.Type {
case "PUBLIC KEY":
p, err = x509.ParsePKIXPublicKey(block.Bytes)
case "RSA PUBLIC KEY":
p, err = x509.ParsePKCS1PublicKey(block.Bytes)
default:
err = fmt.Errorf("unknown block type: %q", block.Type)
}
if err != nil {
return nil, nil, nil, fmt.Errorf("parsing public key from block bytes: %w", err)
}
if p == nil {
return nil, nil, nil, errors.New("returned public key was empty")
}
pubKey, ok := p.(*rsa.PublicKey)
if !ok {
continue
}
return pubKey, pubKeyID, pubKeyOwner, nil
}
return nil, nil, nil, errors.New("couldn't find public key")
}
func exportRsaPublicKeyAsPemStr(pubkey *rsa.PublicKey) (string, error) {
pubkey_bytes, err := x509.MarshalPKIXPublicKey(pubkey)
if err != nil {
return "", err
}
pubkey_pem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubkey_bytes,
},
)
return string(pubkey_pem), nil
}
type ActivityStreamAccount interface {
GetJSONLDId() vocab.JSONLDIdProperty
GetTypeName() string
GetActivityStreamsPreferredUsername() vocab.ActivityStreamsPreferredUsernameProperty
GetActivityStreamsIcon() vocab.ActivityStreamsIconProperty
GetActivityStreamsName() vocab.ActivityStreamsNameProperty
GetActivityStreamsImage() vocab.ActivityStreamsImageProperty
GetActivityStreamsSummary() vocab.ActivityStreamsSummaryProperty
GetActivityStreamsAttachment() vocab.ActivityStreamsAttachmentProperty
SetActivityStreamsSummary(vocab.ActivityStreamsSummaryProperty)
GetTootDiscoverable() vocab.TootDiscoverableProperty
GetActivityStreamsUrl() vocab.ActivityStreamsUrlProperty
GetW3IDSecurityV1PublicKey() vocab.W3IDSecurityV1PublicKeyProperty
GetActivityStreamsInbox() vocab.ActivityStreamsInboxProperty
GetActivityStreamsOutbox() vocab.ActivityStreamsOutboxProperty
GetActivityStreamsFollowing() vocab.ActivityStreamsFollowingProperty
GetActivityStreamsFollowers() vocab.ActivityStreamsFollowersProperty
GetTootFeatured() vocab.TootFeaturedProperty
GetActivityStreamsManuallyApprovesFollowers() vocab.ActivityStreamsManuallyApprovesFollowersProperty
GetActivityStreamsEndpoints() vocab.ActivityStreamsEndpointsProperty
GetActivityStreamsTag() vocab.ActivityStreamsTagProperty
}
type withPreferredUsername interface {
GetActivityStreamsPreferredUsername() vocab.ActivityStreamsPreferredUsernameProperty
}
type withEndpoints interface {
GetActivityStreamsEndpoints() vocab.ActivityStreamsEndpointsProperty
}
type withPublicKey interface {
GetW3IDSecurityV1PublicKey() vocab.W3IDSecurityV1PublicKeyProperty
}