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
package participant
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/market"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/xrpc"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/pds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/ttlcache"
)
const (
ServiceAuthLifetime = 5 * time.Minute
ServiceAuthRefreshMargin = 30 * time.Second
)
type PDSServiceAuth struct {
Open func(ctx context.Context) (pds.ServiceAuth, error)
DID string
Lifetime time.Duration
}
var _ xrpc.Minter = PDSServiceAuth{}
func (a PDSServiceAuth) ServiceAuth(ctx context.Context, audience, lxm string) (string, error) {
if a.Open == nil || a.DID == "" {
return "", errors.New("participant: the PDS service-auth handle names no account")
}
minter, err := a.Open(ctx)
if err != nil {
return "", err
}
lifetime := a.Lifetime
if lifetime <= 0 {
lifetime = ServiceAuthLifetime
}
return minter.ServiceAuth(ctx, a.DID, audience, lxm, time.Now().Add(lifetime))
}
type SessionServiceAuth struct {
Endpoint string
HTTP *http.Client
Auth xrpc.Auth
Lifetime time.Duration
}
var _ xrpc.Minter = SessionServiceAuth{}
func (m SessionServiceAuth) ServiceAuth(ctx context.Context, audience, lxm string) (string, error) {
if strings.TrimSpace(m.Endpoint) == "" {
return "", errors.New("participant: the session service-auth has no PDS URL")
}
lifetime := m.Lifetime
if lifetime <= 0 {
lifetime = ServiceAuthLifetime
}
client := &xrpc.Client{Host: m.Endpoint, HTTP: m.HTTP, Auth: m.Auth}
return xrpc.GetServiceAuth(ctx, client, audience, lxm, time.Now().Add(lifetime))
}
type Tokens struct {
minter xrpc.Minter
timeout time.Duration
cache *ttlcache.Cache[string, string]
}
func NewTokens(minter xrpc.Minter, timeout time.Duration) *Tokens {
return &Tokens{minter: minter, timeout: timeout, cache: ttlcache.New[string, string](ttlcache.Options{})}
}
func (t *Tokens) Token(ctx context.Context, host, lxm string) (string, error) {
if t == nil || t.minter == nil {
return "", errors.New("participant: no service-auth minter is configured")
}
aud := market.ServiceDIDForHost(host)
return t.cache.GetOrLoad(ctx, aud+"\x00"+lxm, func(ctx context.Context) (string, time.Duration, error) {
requested := time.Now().Add(ServiceAuthLifetime)
callCtx := ctx
if t.timeout > 0 {
var cancel context.CancelFunc
callCtx, cancel = context.WithTimeout(ctx, t.timeout)
defer cancel()
}
token, err := t.minter.ServiceAuth(callCtx, aud, lxm)
if err != nil {
return "", 0, fmt.Errorf("getServiceAuth %s: %w", aud, err)
}
ttl := time.Until(CacheExpiry(token, requested))
if ttl <= 0 {
ttl = -1
}
return token, ttl, nil
})
}
func (t *Tokens) Invalidate(host, lxm string) {
if t == nil {
return
}
t.cache.Delete(market.ServiceDIDForHost(host) + "\x00" + lxm)
}
func (t *Tokens) Source(host, lxm string) xrpc.Source[string] {
return tokenSource{tokens: t, host: host, lxm: lxm}
}
type tokenSource struct {
tokens *Tokens
host string
lxm string
}
func (s tokenSource) Get(ctx context.Context) (string, error) {
return s.tokens.Token(ctx, s.host, s.lxm)
}
func (s tokenSource) Invalidate() {
s.tokens.Invalidate(s.host, s.lxm)
}
func CacheExpiry(token string, requested time.Time) time.Time {
fallback := requested.Add(-ServiceAuthRefreshMargin)
parts := strings.Split(token, ".")
if len(parts) < 2 {
return fallback
}
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return fallback
}
var claims struct {
Exp int64 `json:"exp"`
}
if json.Unmarshal(payload, &claims) != nil || claims.Exp == 0 {
return fallback
}
expires := time.Unix(claims.Exp, 0).Add(-ServiceAuthRefreshMargin)
if expires.After(requested) {
return fallback
}
return expires
}