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
package atproto
import (
"cmp"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/oauthclient"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pdsclient"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/conds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/ttlcache"
recordclient "github.com/johnandersen777/socialweb-computer-kcp/internal/policyengine/atproto"
)
const (
ComponentKind = "Pds"
ComponentName = "pds"
ConditionReady = "Ready"
ReasonRevoked = "Revoked"
ReasonWaitingForCredential = "WaitingForCredential"
ReasonVaultUnavailable = "VaultUnavailable"
requestTimeout = 5 * time.Second
minRefreshAfter = time.Second
)
type EndpointFunc func(workspace, kind, name string) (string, bool)
type VaultFunc func(workspace string) SecretStore
type TLSFunc func(ctx context.Context, workspace string, id *v1alpha1.MarketIdentity) (*tls.Config, error)
type Config struct {
Endpoints EndpointFunc
Vault VaultFunc
Resolver *pdsclient.Resolver
HTTPClient *http.Client
TLS TLSFunc
Sessions SessionsFunc
OAuth OAuthOptions
PLCURL string
RecordHTTP *http.Client
Log *slog.Logger
}
type Result struct {
Status v1alpha1.MarketIdentityStatus
RequeueAfter time.Duration
}
type Accounts struct {
endpoints EndpointFunc
vault VaultFunc
log *slog.Logger
http *http.Client
resolver *pdsclient.Resolver
tls TLSFunc
sessions SessionsFunc
oauth OAuthOptions
states oauthclient.StateStore
qr *ttlcache.Cache[string, *qrTransfer]
browser *ttlcache.Cache[string, browserIssue]
held heldAccounts
records *recordclient.Client
refs *ttlcache.Cache[refKey, refVerdict]
verified *ttlcache.Cache[refKey, refVerdict]
}
func New(cfg Config) *Accounts {
log := cfg.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
client := cfg.HTTPClient
if client == nil {
client = &http.Client{Timeout: requestTimeout}
}
resolver := cfg.Resolver
if resolver == nil {
resolver = &pdsclient.Resolver{HTTPClient: client, Timeout: requestTimeout}
}
states := cfg.OAuth.States
if states == nil {
states = &oauthclient.MemoryStateStore{}
}
records := recordclient.NewClient()
records.HTTP = cmp.Or(cfg.RecordHTTP, records.HTTP)
records.PLCURL = cmp.Or(strings.TrimSpace(cfg.PLCURL), records.PLCURL)
return &Accounts{endpoints: cfg.Endpoints, vault: cfg.Vault, log: log, http: client, resolver: resolver, tls: cfg.TLS, sessions: cfg.Sessions, oauth: cfg.OAuth,
states: states, qr: ttlcache.New[string, *qrTransfer](ttlcache.Options{}), browser: ttlcache.New[string, browserIssue](ttlcache.Options{}),
records: records, refs: newRefCache(), verified: newVerifiedCache()}
}
func (a *Accounts) reconcile(ctx context.Context, workspace string, id *v1alpha1.MarketIdentity, status v1alpha1.MarketIdentityStatus) (Result, error) {
generation := id.Generation
if id.Spec.Mode == v1alpha1.MarketIdentityModeLocalRepo && strings.TrimSpace(status.Did) != "" && status.ObservedGeneration >= generation &&
status.Phase != "" && status.Phase != v1alpha1.MarketIdentityPhasePending {
if status.Phase == v1alpha1.MarketIdentityPhaseReady || status.Phase == v1alpha1.MarketIdentityPhaseAwaitingApproval {
settleAssociation(&status, generation, id.Spec.Association, "the PDS reports "+status.Did)
}
return Result{Status: status}, nil
}
switch id.Spec.Mode {
case v1alpha1.MarketIdentityModeLocalRepo:
return a.localRepo(ctx, workspace, id, generation, status)
case v1alpha1.MarketIdentityModePdsAccount:
return a.pdsAccount(ctx, workspace, id, generation, status)
case v1alpha1.MarketIdentityModeOAuthBrowser:
return a.oauthBrowser(ctx, workspace, id, generation, status)
case v1alpha1.MarketIdentityModeOAuthQr:
return a.oauthQr(ctx, workspace, id, generation, status)
case v1alpha1.MarketIdentityModeOAuthDelegated:
return a.oauthDelegated(ctx, workspace, id, generation, status)
case v1alpha1.MarketIdentityModeOAuthSessionProxy:
return a.sessionProxy(ctx, workspace, id, generation, status)
case "":
return pending(status, generation, "UnknownMode", "the MarketIdentity has no mode; no DID is recorded"), nil
default:
return pending(status, generation, "UnknownMode", fmt.Sprintf("unknown mode %q; no DID is recorded", id.Spec.Mode)), nil
}
}
func (a *Accounts) Forget(workspace, name string) {
a.qr.Delete(qrKey(workspace, name))
a.browser.Delete(qrKey(workspace, name))
a.held.forget(workspace, name)
}
func (a *Accounts) localRepo(ctx context.Context, workspace string, id *v1alpha1.MarketIdentity, generation int64, status v1alpha1.MarketIdentityStatus) (Result, error) {
endpoint := ""
if a.endpoints != nil {
if url, ok := a.endpoints(workspace, ComponentKind, ComponentName); ok {
endpoint = strings.TrimSpace(url)
}
}
if endpoint == "" {
return pending(status, generation, "PdsNotReady", "waiting for the Pds/pds component endpoint in workspace "+workspace), nil
}
did, err := a.resolveDID(ctx, workspace, endpoint)
if err != nil {
return pending(status, generation, "PdsNotReady", "waiting for the PDS at "+endpoint+": "+err.Error()),
fmt.Errorf("marketidentity: the PDS at %s: %w", endpoint, err)
}
composed := strings.TrimSpace(id.Spec.Did)
if composed == "" {
status.Did = ""
return pending(status, generation, "AccountPending", "the PDS at "+endpoint+" ("+did+") is ready; waiting for the root to compose the account DID"), nil
}
status.Phase = v1alpha1.MarketIdentityPhaseReady
status.ObservedGeneration = generation
status.Did = composed
if handle := strings.TrimSpace(id.Spec.Handle); handle != "" {
status.Handle = handle
}
if pdsURL := strings.TrimSpace(id.Spec.PdsURL); pdsURL != "" {
status.PdsURL = pdsURL
}
setAssociated(&status, generation, id.Spec.Association)
settleAssociation(&status, generation, id.Spec.Association, "the PDS at "+endpoint+" reports "+did)
return Result{Status: status}, nil
}
func settleAssociation(status *v1alpha1.MarketIdentityStatus, generation int64, mode v1alpha1.AssociationMode, ready string) {
if mode != v1alpha1.AssociationSkip && !associatedTrue(status) {
status.Phase = v1alpha1.MarketIdentityPhaseAwaitingApproval
status.Message = "the PDS is ready; waiting for the operator's verified association claim"
setReady(status, generation, false, "AwaitingAssociation", status.Message)
return
}
status.Phase, status.Message = v1alpha1.MarketIdentityPhaseReady, ""
setReady(status, generation, true, "Ready", ready)
}
func (a *Accounts) resolveDID(ctx context.Context, workspace, endpoint string) (string, error) {
pds, err := a.localPDS(workspace, endpoint)
if err != nil {
return "", err
}
info, err := pds.Describe(ctx)
if err != nil {
return "", err
}
did := strings.TrimSpace(info.DID.String())
if did == "" {
return "", fmt.Errorf("the PDS reported no did")
}
return did, nil
}
func (a *Accounts) localPDS(workspace, endpoint string) (pdsclient.PDS, error) {
endpoint = strings.TrimSpace(endpoint)
if !strings.Contains(endpoint, "://") {
endpoint = "http://" + endpoint
}
if a.resolver.Handlers != nil {
return a.resolver.ResolveLocal(pdsclient.LocalRef{Workspace: workspace, Kind: ComponentKind, Name: ComponentName, Endpoint: endpoint})
}
return pdsclient.NewRemote(pdsclient.RemoteConfig{BaseURL: endpoint, HTTPClient: a.resolver.HTTPClient, TLS: a.resolver.TLS,
Tokens: a.resolver.Tokens, DPoP: a.resolver.DPoP, Timeout: opt.Positive(a.resolver.Timeout, requestTimeout)})
}
func (a *Accounts) vaultFor(workspace string) SecretStore {
if a.vault == nil {
return nil
}
return a.vault(workspace)
}
func pending(status v1alpha1.MarketIdentityStatus, generation int64, reason, message string) Result {
status.Phase = v1alpha1.MarketIdentityPhasePending
status.ObservedGeneration = generation
status.Message = message
setReady(&status, generation, false, reason, message)
return Result{Status: status}
}
func failed(status v1alpha1.MarketIdentityStatus, generation int64, reason, message string) Result {
status.Phase = v1alpha1.MarketIdentityPhaseFailed
status.ObservedGeneration = generation
status.Message = message
setReady(&status, generation, false, reason, message)
return Result{Status: status}
}
func setReady(status *v1alpha1.MarketIdentityStatus, generation int64, ready bool, reason, message string) {
conds.Set(&status.Conditions, generation, ConditionReady, ready, reason, message)
}
func refreshAfter(token string) time.Duration {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return 0
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return 0
}
var claims struct {
ExpiresAt int64 `json:"exp"`
}
if json.Unmarshal(raw, &claims) != nil || claims.ExpiresAt == 0 {
return 0
}
return max(time.Until(time.Unix(claims.ExpiresAt, 0).Add(-oauthclient.AccessSkew)), minRefreshAfter)
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return strings.TrimSpace(value)
}
}
return ""
}