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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package pds
import (
"errors"
"net/http"
"net/mail"
"net/url"
"strings"
"time"
"github.com/bluesky-social/indigo/atproto/atcrypto"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/oauth"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/repo"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/serviceauth"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/httpjson"
)
type createAccountRequest struct {
Handle string `json:"handle"`
Email string `json:"email,omitempty"`
Password string `json:"password,omitempty"`
InviteCode string `json:"inviteCode,omitempty"`
DID string `json:"did,omitempty"`
RecoveryKey string `json:"recoveryKey,omitempty"`
PLCOp map[string]any `json:"plcOp,omitempty"`
}
type createSessionRequest struct {
Identifier string `json:"identifier,omitempty"`
Password string `json:"password,omitempty"`
}
type sessionOutput struct {
AccessJWT string `json:"accessJwt"`
RefreshJWT string `json:"refreshJwt"`
Handle string `json:"handle"`
DID string `json:"did"`
Email string `json:"email,omitempty"`
Active bool `json:"active"`
DIDDoc any `json:"didDoc,omitempty"`
}
type getSessionOutput struct {
DID string `json:"did"`
Handle string `json:"handle"`
Email string `json:"email,omitempty"`
EmailConfirmed bool `json:"emailConfirmed,omitempty"`
Active bool `json:"active"`
DIDDoc any `json:"didDoc,omitempty"`
}
type describeServerOutput struct {
DID string `json:"did"`
AvailableUserDomains []string `json:"availableUserDomains"`
InviteCodeRequired bool `json:"inviteCodeRequired"`
PhoneVerificationRequired bool `json:"phoneVerificationRequired"`
Contact *serverContact `json:"contact,omitempty"`
}
type serverContact struct {
Email string `json:"email,omitempty"`
}
type createAppPasswordRequest struct {
Name string `json:"name"`
Privileged bool `json:"privileged,omitempty"`
}
type revokeAppPasswordRequest struct {
Name string `json:"name"`
}
type appPasswordOutput struct {
Name string `json:"name"`
Password string `json:"password,omitempty"`
Privileged bool `json:"privileged,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
type appPasswordView struct {
Name string `json:"name"`
Privileged bool `json:"privileged,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
type listAppPasswordsOutput struct {
Passwords []appPasswordView `json:"passwords"`
}
func (s *Server) handleDescribeServer(w http.ResponseWriter, _ *http.Request) {
domains := append([]string(nil), s.cfg.AvailableUserDomains...)
if len(domains) == 0 {
domain := "localhost"
if s.cfg.PublicBaseURL != "" {
if u, err := url.Parse(s.cfg.PublicBaseURL); err == nil && u.Hostname() != "" {
domain = u.Hostname()
}
}
domains = []string{domain}
}
out := describeServerOutput{
DID: s.serviceDID,
AvailableUserDomains: domains,
}
if s.cfg.ContactEmail != "" {
out.Contact = &serverContact{Email: s.cfg.ContactEmail}
}
httpjson.Write(w, http.StatusOK, out)
}
func (s *Server) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if s.mode != ModeFull || s.store == nil {
writeXRPCError(w, http.StatusForbidden, "Forbidden", "account creation is disabled")
return
}
var req createAccountRequest
if err := s.decodeJSON(w, r, &req); err != nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "invalid JSON")
return
}
handle, err := syntax.ParseHandle(strings.ToLower(strings.TrimSpace(req.Handle)))
if err != nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidHandle", "invalid handle")
return
}
if req.Password == "" {
writeXRPCError(w, http.StatusBadRequest, "InvalidPassword", "password is required")
return
}
email := strings.TrimSpace(req.Email)
if email != "" {
addr, err := mail.ParseAddress(email)
if err != nil || !strings.EqualFold(addr.Address, email) {
writeXRPCError(w, http.StatusBadRequest, "InvalidEmail", "invalid email")
return
}
}
if s.keys == nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "repo key store is not configured")
return
}
key, err := atcrypto.GeneratePrivateKeyK256()
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalServerError", "failed to generate signing key")
return
}
pub, err := key.PublicKey()
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalServerError", "failed to derive signing key")
return
}
signingDIDKey := pub.DIDKey()
var did syntax.DID
if req.DID != "" {
if s.plc != nil && !s.cfg.AllowExplicitDID {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "DID must be created through the PLC directory")
return
}
if !s.cfg.AllowExplicitDID {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "explicit DID creation is not allowed")
return
}
did, err = syntax.ParseDID(req.DID)
if err != nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "invalid DID")
return
}
} else {
if s.plc == nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "PLC directory is not configured")
return
}
result, err := s.plc.SubmitGenesis(ctx, PLCGenesisRequest{
Handle: handle.String(),
Service: s.publicURL(r),
SigningKey: signingDIDKey,
RotationKey: s.cfg.RotationKey,
VerificationMethods: map[string]string{"atproto": signingDIDKey},
AlsoKnownAs: []string{"at://" + handle.String()},
})
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "PLC genesis failed: "+err.Error())
return
}
did, err = syntax.ParseDID(result.DID.String())
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "PLC returned an invalid DID")
return
}
}
acct, err := s.store.CreateAccount(ctx, NewAccount{
DID: did,
Handle: handle.String(),
Email: email,
Password: req.Password,
CreatedAt: time.Now().UTC(),
})
if err != nil {
switch {
case errors.Is(err, ErrHandleTaken):
writeXRPCError(w, http.StatusBadRequest, "HandleNotAvailable", "handle is not available")
case errors.Is(err, ErrAccountExists):
writeXRPCError(w, http.StatusBadRequest, "DidExists", "DID is already registered")
default:
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to create account")
}
return
}
if err := s.keys.PutRepoKey(ctx, did, key); err != nil {
_ = s.store.DeleteAccount(ctx, did)
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to store repo signing key")
return
}
if _, err := s.repos.CreateRepo(ctx, did, key); err != nil {
_ = s.keys.DeleteRepoKey(ctx, did)
_ = s.store.DeleteAccount(ctx, did)
if errors.Is(err, repo.ErrRepoExists) {
writeXRPCError(w, http.StatusBadRequest, "DidExists", "repo already exists")
return
}
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to create repo")
return
}
pair, err := s.createSession(ctx, acct, false, false)
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to create session")
return
}
httpjson.Write(w, http.StatusOK, sessionOutput{
AccessJWT: pair.Access,
RefreshJWT: pair.Refresh,
Handle: acct.Handle,
DID: acct.DID.String(),
Email: acct.Email,
Active: true,
DIDDoc: s.didDocument(ctx, acct.DID),
})
}
func (s *Server) handleCreateSession(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var req createSessionRequest
if err := s.decodeJSON(w, r, &req); err != nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "invalid JSON")
return
}
if req.Identifier == "" || req.Password == "" {
if user, pass, ok := r.BasicAuth(); ok {
req.Identifier = user
req.Password = pass
}
}
if req.Identifier == "" || req.Password == "" {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "identifier and password are required")
return
}
acct, appPassword, privileged, err := s.store.VerifyPassword(ctx, req.Identifier, req.Password)
if err != nil {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "invalid identifier or password")
return
}
pair, err := s.createSession(ctx, acct, appPassword, privileged)
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to create session")
return
}
httpjson.Write(w, http.StatusOK, sessionOutput{
AccessJWT: pair.Access,
RefreshJWT: pair.Refresh,
Handle: acct.Handle,
DID: acct.DID.String(),
Email: acct.Email,
Active: true,
DIDDoc: s.didDocument(ctx, acct.DID),
})
}
func (s *Server) handleRefreshSession(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
scheme, token, ok := serviceauth.SplitAuthorization(r.Header.Get("Authorization"))
if !ok || !strings.EqualFold(scheme, "Bearer") {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "refresh token required")
return
}
claims, err := s.parseToken(token, scopeRefresh)
if err != nil {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "invalid refresh token")
return
}
sess, err := s.store.GetLegacySessionByRefresh(ctx, claims.ID)
if err != nil || sess == nil || sess.RevokedAt != nil || !sess.ExpiresAt.After(time.Now()) {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "invalid refresh token")
return
}
if sess.ID != claims.SID || sess.DID.String() != claims.Issuer {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "invalid refresh token")
return
}
acct, err := s.store.GetAccount(ctx, sess.DID)
if err != nil || acct == nil || !acct.Active {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "account is not active")
return
}
pair, err := s.issueTokenPair(sess.DID, sess.ID)
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to issue token")
return
}
if err := s.store.RotateLegacySession(ctx, sess.ID, pair.RefreshJTI, pair.RefreshExpires); err != nil {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "invalid refresh token")
return
}
httpjson.Write(w, http.StatusOK, sessionOutput{
AccessJWT: pair.Access,
RefreshJWT: pair.Refresh,
Handle: acct.Handle,
DID: acct.DID.String(),
Email: acct.Email,
Active: true,
DIDDoc: s.didDocument(ctx, acct.DID),
})
}
func (s *Server) handleDeleteSession(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if sess, ok := oauth.SessionFromContext(r.Context()); ok && sess != nil {
if err := s.store.RevokeSession(ctx, sess.ID, time.Now()); err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to revoke session")
return
}
httpjson.Write(w, http.StatusOK, map[string]any{})
return
}
scheme, token, ok := serviceauth.SplitAuthorization(r.Header.Get("Authorization"))
if !ok || !strings.EqualFold(scheme, "Bearer") {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "refresh token required")
return
}
claims, err := s.parseToken(token, scopeRefresh)
if err != nil {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "invalid refresh token")
return
}
if err := s.store.RevokeLegacySessionByRefresh(ctx, claims.ID, time.Now()); err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to revoke session")
return
}
httpjson.Write(w, http.StatusOK, map[string]any{})
}
func (s *Server) handleGetSession(w http.ResponseWriter, r *http.Request, info *authInfo) {
ctx := r.Context()
acct, err := s.store.GetAccount(ctx, info.DID)
if err != nil || acct == nil {
writeXRPCError(w, http.StatusUnauthorized, "AuthenticationRequired", "account not found")
return
}
httpjson.Write(w, http.StatusOK, getSessionOutput{
DID: acct.DID.String(),
Handle: acct.Handle,
Email: acct.Email,
EmailConfirmed: acct.Email != "",
Active: acct.Active,
DIDDoc: s.didDocument(ctx, acct.DID),
})
}
func (s *Server) handleCreateAppPassword(w http.ResponseWriter, r *http.Request, info *authInfo) {
ctx := r.Context()
if info.AppPassword {
writeXRPCError(w, http.StatusForbidden, "Forbidden", "app passwords cannot manage app passwords")
return
}
var req createAppPasswordRequest
if err := s.decodeJSON(w, r, &req); err != nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "invalid JSON")
return
}
password, err := GenerateAppPassword()
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to generate app password")
return
}
app, err := s.store.CreateAppPassword(ctx, info.DID, req.Name, password, req.Privileged)
if err != nil {
if errors.Is(err, ErrAppPasswordExists) {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "app password name already exists")
return
}
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", err.Error())
return
}
httpjson.Write(w, http.StatusOK, appPasswordOutput{
Name: app.Name,
Password: password,
Privileged: app.Privileged,
CreatedAt: app.CreatedAt,
})
}
func (s *Server) handleListAppPasswords(w http.ResponseWriter, r *http.Request, info *authInfo) {
ctx := r.Context()
if info.AppPassword {
writeXRPCError(w, http.StatusForbidden, "Forbidden", "app passwords cannot list app passwords")
return
}
list, err := s.store.ListAppPasswords(ctx, info.DID)
if err != nil {
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to list app passwords")
return
}
out := listAppPasswordsOutput{Passwords: make([]appPasswordView, 0, len(list))}
for _, app := range list {
out.Passwords = append(out.Passwords, appPasswordView{
Name: app.Name,
Privileged: app.Privileged,
CreatedAt: app.CreatedAt,
})
}
httpjson.Write(w, http.StatusOK, out)
}
func (s *Server) handleRevokeAppPassword(w http.ResponseWriter, r *http.Request, info *authInfo) {
ctx := r.Context()
if info.AppPassword {
writeXRPCError(w, http.StatusForbidden, "Forbidden", "app passwords cannot revoke app passwords")
return
}
var req revokeAppPasswordRequest
if err := s.decodeJSON(w, r, &req); err != nil {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "invalid JSON")
return
}
if err := s.store.RevokeAppPassword(ctx, info.DID, req.Name, time.Now()); err != nil {
if errors.Is(err, ErrAppPasswordMissing) {
writeXRPCError(w, http.StatusBadRequest, "InvalidRequest", "app password not found")
return
}
writeXRPCError(w, http.StatusInternalServerError, "InternalError", "failed to revoke app password")
return
}
httpjson.Write(w, http.StatusOK, map[string]any{})
}