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
package participant
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/market"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/httpjson"
)
const MaxSubmitBody = 2 << 20
var ErrUnsigned = errors.New("record is missing a valid badge.blue signature")
type Verifier struct {
Directory identity.Directory
Records market.RecordGetter
}
func (v Verifier) Options() market.VerifyRecordOptions {
opts := market.VerifyRecordOptions{KeysForDID: market.NewDirectoryKeyLister(v.Directory)}
if v.Records != nil {
opts.KeyBoundByBadgeBlueKey = v.BadgeBlueBinding()
}
return opts
}
func (v Verifier) BadgeBlueBinding() func(ctx context.Context, did, key string) (bool, error) {
records := v.Records
return func(ctx context.Context, did, key string) (bool, error) {
return market.KeyBoundByBadgeBlueKey(ctx, records, did, key), nil
}
}
func (v Verifier) VerifyRecord(ctx context.Context, record market.ResolvedRecord) error {
if !market.VerifyRecordSignatures(ctx, record.Value, market.ATURIAuthority(record.URI), v.Options()) {
return ErrUnsigned
}
return nil
}
type Authorizer struct {
Directory identity.Directory
Hostname string
ServiceIDs []string
AudienceDIDs []string
}
func (a *Authorizer) Verify(ctx context.Context, header, lxm string) (string, error) {
if a == nil || a.Directory == nil {
return "", errors.New("participant: the service has no identity directory")
}
if a.Hostname == "" {
return "", errors.New("participant: the service has no service hostname")
}
result, err := market.VerifyServiceAuth(ctx, a.Directory, market.VerifyServiceAuthOptions{
AuthHeader: header,
Hostname: a.Hostname,
LXM: lxm,
ServiceIDs: a.ServiceIDs,
ExtraAudienceDIDs: a.AudienceDIDs,
})
if err != nil {
return "", err
}
return result.IssuerDID, nil
}
func (a *Authorizer) Authorize(ctx context.Context, header, lxm, recordURI string) (string, error) {
issuer, err := a.Verify(ctx, header, lxm)
if err != nil {
return "", err
}
if err := market.AuthorizeRecord(issuer, recordURI); err != nil {
return "", err
}
return issuer, nil
}
func (a *Authorizer) AuthorizeRequest(r *http.Request, lxm, recordURI string) (string, bool, int, error) {
if a == nil {
return "", false, http.StatusInternalServerError, errors.New("participant: no authorizer")
}
issuer, err := a.Authorize(r.Context(), r.Header.Get("Authorization"), lxm, recordURI)
if err != nil {
if errors.Is(err, market.ErrForbidden) {
return "", false, http.StatusForbidden, err
}
return "", false, http.StatusUnauthorized, err
}
return issuer, true, 0, nil
}
func ReadSubmitBody(r *http.Request, out any) error {
dec := json.NewDecoder(io.LimitReader(r.Body, MaxSubmitBody+1))
if err := dec.Decode(out); err != nil {
return fmt.Errorf("the body is not JSON: %w", err)
}
return nil
}
func WriteXRPCError(w http.ResponseWriter, status int, code, message string) {
httpjson.Write(w, status, map[string]any{"error": code, "message": message})
}
func AuthCode(status int) string {
if status == http.StatusForbidden {
return "Forbidden"
}
return "Unauthorized"
}