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
package pds
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
)
const (
CapabilityAccount = "pds.account"
CapabilitySession = "pds.session"
CapabilitySync = "repo.read"
CapabilityServiceAuth = "pds.serviceauth"
)
var (
ErrNotFound = errors.New("pds: not found")
ErrUnsupported = errors.New("pds: the backend does not support this call")
)
type Info struct {
DID string
AvailableUserDomains []string
Version string
URL string
}
type AccountRequest struct {
Handle string
Email string
Credentials commonv1.SecretRef
}
type Account struct {
DID string
Handle string
Email string
Active bool
SigningKey string
}
type SessionRequest struct {
Identifier string
Credentials commonv1.SecretRef
}
type Session struct {
DID string
Handle string
Active bool
}
type Commit struct {
CID string
Rev string
}
type Op struct {
Action string
Path string
CID string
Prev string
}
type Event struct {
Seq int64
Repo string
Commit string
Rev string
Since string
Time time.Time
Ops []Op
TooBig bool
}
type Accounts interface {
Describe(ctx context.Context) (Info, error)
CreateAccount(ctx context.Context, req AccountRequest) (Account, error)
Account(ctx context.Context, did string) (Account, error)
}
type Sessions interface {
SignIn(ctx context.Context, req SessionRequest) (Session, error)
Session(ctx context.Context, did string) (Session, error)
SignOut(ctx context.Context, did string) error
}
type ServiceAuth interface {
ServiceAuth(ctx context.Context, did, audience, lxm string, expires time.Time) (string, error)
}
type ScopeGrants interface {
GrantedScopes(did string) ([]string, bool)
}
type Sync interface {
LatestCommit(ctx context.Context, did string) (Commit, error)
GetRepo(ctx context.Context, did, since string) (io.ReadCloser, error)
SubscribeRepos(ctx context.Context, cursor *int64, fn func(Event) error) error
}
type Verdict int
const (
VerdictDone Verdict = iota
VerdictRetryNonce
VerdictRetryAuth
)
type Authenticator interface {
DID() string
Apply(req *http.Request) error
Invalidate()
Challenge(resp *http.Response) Verdict
}
type Transport interface {
Address() string
Client() *http.Client
TLS() *tls.Config
}
func AccountDID(scope []string) (string, error) {
var did string
for _, entry := range scope {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
if did != "" && entry != did {
return "", fmt.Errorf("pds: a repo handle serves one account; the scope names %v", scope)
}
did = entry
}
if !strings.HasPrefix(did, "did:") {
return "", fmt.Errorf("pds: a repo handle needs the account DID as its pds.account scope, got %v", scope)
}
return did, nil
}