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
package xrpc
import (
"context"
"errors"
"net/http"
"strings"
"sync"
"sync/atomic"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/lease"
)
var ErrNoCredentials = errors.New("xrpc: no credentials are available")
var _ Source[string] = (*lease.Source[string])(nil)
type Source[T any] interface {
Get(ctx context.Context) (T, error)
Invalidate()
}
type Auth interface {
Apply(req *http.Request) error
Invalidate()
}
type Verdict int
const (
Done Verdict = iota
RetryNonce
RetryAuth
)
type Challenger interface {
Challenge(resp *http.Response) Verdict
}
func StatusRetry(a Auth) Auth {
return statusRetry{Auth: a}
}
type statusRetry struct {
Auth
}
type TokenFunc func(ctx context.Context) (string, error)
func Static(token string) Source[string] {
return staticSource(token)
}
type staticSource string
func (s staticSource) Get(context.Context) (string, error) { return string(s), nil }
func (staticSource) Invalidate() {}
func Refreshing(get, refresh TokenFunc) Source[string] {
return &refreshingSource{get: get, refresh: refresh}
}
type refreshingSource struct {
get TokenFunc
refresh TokenFunc
stale atomic.Bool
}
func (s *refreshingSource) Get(ctx context.Context) (string, error) {
if s.stale.Swap(false) {
if s.refresh == nil {
return "", ErrNoCredentials
}
if _, err := s.refresh(ctx); err != nil {
return "", err
}
}
if s.get == nil {
return "", ErrNoCredentials
}
return s.get(ctx)
}
func (s *refreshingSource) Invalidate() { s.stale.Store(true) }
type Bearer struct {
Tokens Source[string]
}
func (b *Bearer) Apply(req *http.Request) error {
return applyBearer(req, b.Tokens)
}
func (b *Bearer) Invalidate() { b.Tokens.Invalidate() }
type ServiceAuth struct {
Tokens Source[string]
}
func NewServiceAuth(minter Minter, aud, lxm string) *ServiceAuth {
return &ServiceAuth{Tokens: &mintedSource{minter: minter, aud: aud, lxm: lxm}}
}
func (s *ServiceAuth) Apply(req *http.Request) error {
return applyBearer(req, s.Tokens)
}
func (s *ServiceAuth) Invalidate() { s.Tokens.Invalidate() }
type mintedSource struct {
minter Minter
aud string
lxm string
}
func (m *mintedSource) Get(ctx context.Context) (string, error) {
if m.minter == nil {
return "", ErrNoCredentials
}
return m.minter.ServiceAuth(ctx, m.aud, m.lxm)
}
func (*mintedSource) Invalidate() {}
func applyBearer(req *http.Request, tokens Source[string]) error {
if tokens == nil {
return ErrNoCredentials
}
token, err := tokens.Get(req.Context())
if err != nil {
return err
}
if token == "" {
return ErrNoCredentials
}
req.Header.Set("Authorization", "Bearer "+token)
return nil
}
type DPoPProof struct {
AccessToken string
Proof string
}
type DPoPProver interface {
ProveDPoP(ctx context.Context, method, target, accessToken, nonce string) (DPoPProof, error)
}
type DPoPProverFunc func(ctx context.Context, method, target, accessToken, nonce string) (DPoPProof, error)
func (f DPoPProverFunc) ProveDPoP(ctx context.Context, method, target, accessToken, nonce string) (DPoPProof, error) {
return f(ctx, method, target, accessToken, nonce)
}
type DPoP struct {
Tokens Source[string]
Prover DPoPProver
mu sync.Mutex
nonces map[string]string
}
func (d *DPoP) Apply(req *http.Request) error {
if d.Prover == nil {
return ErrNoCredentials
}
ctx := req.Context()
token := ""
if d.Tokens != nil {
var err error
token, err = d.Tokens.Get(ctx)
if err != nil {
return err
}
}
proof, err := d.Prover.ProveDPoP(ctx, req.Method, req.URL.String(), token, d.nonce(origin(req)))
if err != nil {
return err
}
if proof.AccessToken != "" {
token = proof.AccessToken
}
if token == "" {
return ErrNoCredentials
}
req.Header.Set("Authorization", "DPoP "+token)
if proof.Proof != "" {
req.Header.Set("DPoP", proof.Proof)
}
return nil
}
func (d *DPoP) Invalidate() {
if d.Tokens != nil {
d.Tokens.Invalidate()
}
}
func (d *DPoP) Challenge(resp *http.Response) Verdict {
nonce := resp.Header.Get("DPoP-Nonce")
if nonce != "" && resp.Request != nil {
d.mu.Lock()
if d.nonces == nil {
d.nonces = map[string]string{}
}
d.nonces[origin(resp.Request)] = nonce
d.mu.Unlock()
}
invalidToken := strings.Contains(resp.Header.Get("WWW-Authenticate"), "invalid_token")
if nonce != "" && !invalidToken && (resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusUnauthorized) {
return RetryNonce
}
if resp.StatusCode == http.StatusUnauthorized {
return RetryAuth
}
return Done
}
func (d *DPoP) nonce(origin string) string {
d.mu.Lock()
defer d.mu.Unlock()
return d.nonces[origin]
}
func origin(req *http.Request) string {
if req == nil || req.URL == nil {
return ""
}
return req.URL.Scheme + "://" + req.URL.Host
}