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
package pdsclient
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/ipfs/go-cid"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/market"
)
var (
ErrInvalidConfig = errors.New("pdsclient: invalid configuration")
ErrInvalidRef = errors.New("pdsclient: invalid reference")
ErrInvalidRequest = errors.New("pdsclient: invalid request")
ErrUnexpectedResponse = errors.New("pdsclient: unexpected response")
ErrAuthenticationRequired = errors.New("pdsclient: authentication required")
ErrIdentityMismatch = errors.New("pdsclient: identity mismatch")
ErrUnavailable = errors.New("pdsclient: unavailable")
ErrClosed = errors.New("pdsclient: stream closed")
ErrProtocol = errors.New("pdsclient: protocol error")
)
type TokenSource interface {
AccessToken(ctx context.Context) (string, error)
}
type TokenRefresher interface {
RefreshAccessToken(ctx context.Context) (string, error)
}
type DPoPNonceSetter interface {
SetDPoPNonce(ctx context.Context, host, nonce string) error
}
const TokenExpirySkew = 30 * time.Second
func TokenExpiry(token string) (time.Time, bool) {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return time.Time{}, false
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return time.Time{}, false
}
var claims struct {
ExpiresAt int64 `json:"exp"`
}
if json.Unmarshal(raw, &claims) != nil || claims.ExpiresAt == 0 {
return time.Time{}, false
}
return time.Unix(claims.ExpiresAt, 0), true
}
func TokenLive(token string) bool {
expires, ok := TokenExpiry(token)
if !ok {
return false
}
return time.Now().Add(TokenExpirySkew).Before(expires)
}
type DPoPSource interface {
DPoPProof(ctx context.Context, method, url, accessToken string) (string, error)
}
type TokenSourceFunc func(ctx context.Context) (string, error)
func (f TokenSourceFunc) AccessToken(ctx context.Context) (string, error) {
if f == nil {
return "", fmt.Errorf("%w: nil token source", ErrInvalidConfig)
}
return f(ctx)
}
type DPoPSourceFunc func(ctx context.Context, method, url, accessToken string) (string, error)
func (f DPoPSourceFunc) DPoPProof(ctx context.Context, method, url, accessToken string) (string, error) {
if f == nil {
return "", fmt.Errorf("%w: nil dpop source", ErrInvalidConfig)
}
return f(ctx, method, url, accessToken)
}
type Info struct {
DID syntax.DID
AvailableUserDomains []string
Version string
}
type RepoPath struct {
Repo syntax.DID
Collection syntax.NSID
Rkey syntax.RecordKey
}
type Record struct {
URI syntax.ATURI
CID cid.Cid
Value json.RawMessage
}
type WriteOptions struct {
SwapCommit *cid.Cid
SwapRecord *cid.Cid
Validate *bool
}
type CommitRef struct {
CID cid.Cid
Rev syntax.TID
}
type WriteResult struct {
URI syntax.ATURI
CID cid.Cid
Commit *CommitRef
}
type ListRecordsRequest struct {
Repo syntax.DID
Collection syntax.NSID
Cursor string
Limit int
Reverse bool
}
type ListRecordsResult struct {
Records []Record
Cursor string
}
type Session struct {
DID syntax.DID
Handle string
Email string
AccessJWT string
RefreshJWT string
Active bool
}
type CreateAccountRequest struct {
Identifier string
Password string
Email string
InviteCode string
}
func (s Session) String() string {
return fmt.Sprintf("pdsclient.Session{did:%s handle:%s}", s.DID, s.Handle)
}
type RepoOp struct {
Action string
Path string
CID *cid.Cid
Prev *cid.Cid
}
type RepoEvent struct {
Seq int64
Repo syntax.DID
Commit cid.Cid
Rev syntax.TID
Since *syntax.TID
PrevData *cid.Cid
Ops []RepoOp
Blobs []cid.Cid
Blocks []byte
Time time.Time
TooBig bool
}
type RepoEventStream interface {
Next(ctx context.Context) (RepoEvent, error)
Cursor() int64
Close() error
}
type PDS interface {
Describe(ctx context.Context) (Info, error)
GetRecord(ctx context.Context, path RepoPath, expected *cid.Cid) (Record, error)
CreateRecord(ctx context.Context, path RepoPath, record []byte, opts WriteOptions) (WriteResult, error)
PutRecord(ctx context.Context, path RepoPath, record []byte, opts WriteOptions) (WriteResult, error)
DeleteRecord(ctx context.Context, path RepoPath, opts WriteOptions) error
ListRecords(ctx context.Context, req ListRecordsRequest) (ListRecordsResult, error)
GetRepo(ctx context.Context, did syntax.DID, since *syntax.TID) (io.ReadCloser, error)
GetLatestCommit(ctx context.Context, did syntax.DID) (cid.Cid, syntax.TID, error)
SubscribeRepos(ctx context.Context, cursor *int64) (RepoEventStream, error)
CreateSession(ctx context.Context, identifier, password string) (Session, error)
CreateAccount(ctx context.Context, req CreateAccountRequest) (Session, error)
GetSession(ctx context.Context) (Session, error)
DeleteSession(ctx context.Context) error
}
type XRPCError struct {
StatusCode int
Code string
Message string
Wait time.Duration
}
func (e *XRPCError) HTTPStatus() int { return e.StatusCode }
func (e *XRPCError) RetryAfter() time.Duration { return e.Wait }
func (e *XRPCError) Error() string {
if e == nil {
return "pdsclient: <nil>"
}
if e.Code == "" {
return fmt.Sprintf("pdsclient: xrpc status %d: %s", e.StatusCode, e.Message)
}
return fmt.Sprintf("pdsclient: xrpc %s: %s (status %d)", e.Code, e.Message, e.StatusCode)
}
func ParseRepoPath(repo, collection, rkey string) (RepoPath, error) {
did, err := syntax.ParseDID(repo)
if err != nil {
return RepoPath{}, fmt.Errorf("%w: repo %q: %v", market.ErrInvalidRef, repo, err)
}
nsid, err := syntax.ParseNSID(collection)
if err != nil {
return RepoPath{}, fmt.Errorf("%w: collection %q: %v", market.ErrInvalidRef, collection, err)
}
path := RepoPath{Repo: did, Collection: nsid}
if rkey != "" {
key, err := syntax.ParseRecordKey(rkey)
if err != nil {
return RepoPath{}, fmt.Errorf("%w: rkey %q: %v", market.ErrInvalidRef, rkey, err)
}
path.Rkey = key
}
return path, nil
}