This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
214 lines (191 loc) · 4.19 KB
/
cache.go
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
package otgo
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
)
// renewer ...
type renewer interface {
RLock()
RUnlock()
Lock()
Unlock()
value() interface{}
shouldRenew() bool
renew(context.Context, *OTClient) error
}
type cache struct {
mu sync.RWMutex
kv map[string]renewer
new func(OTID) renewer
}
func newCache(fn func(OTID) renewer) *cache {
return &cache{
kv: make(map[string]renewer),
new: fn,
}
}
// Get ...
func (r *cache) Get(id OTID) renewer {
key := id.String()
r.mu.RLock()
val, ok := r.kv[key]
r.mu.RUnlock()
if !ok {
r.mu.Lock()
defer r.mu.Unlock()
val, ok = r.kv[key]
if !ok {
val = r.new(id)
r.kv[key] = val
}
}
return val
}
func resolve(ctx context.Context, obj renewer, oc *OTClient) (interface{}, error) {
obj.RLock()
v := obj.value()
if !obj.shouldRenew() {
obj.RUnlock()
return v, nil
}
obj.RUnlock()
obj.Lock()
defer obj.Unlock()
v = obj.value()
if !obj.shouldRenew() {
return v, nil
}
if err := obj.renew(ctx, oc); err != nil {
return nil, err
}
return obj.value(), nil
}
type domainRenewer struct {
sync.RWMutex
td TrustDomain
ks *JWKSet
expiresAt time.Time
endpoint string
}
// DomainConfig ...
type DomainConfig struct {
OTID OTID
JWKSet *JWKSet
Endpoint string
}
// Resolve ...
func (r *domainRenewer) Resolve(ctx context.Context, oc *OTClient) (*DomainConfig, error) {
obj, err := resolve(ctx, r, oc)
if err != nil {
return nil, err
}
return obj.(*DomainConfig), nil
}
func (r *domainRenewer) value() interface{} {
return &DomainConfig{
OTID: r.td.OTID(),
JWKSet: r.ks,
Endpoint: r.endpoint,
}
}
func (r *domainRenewer) shouldRenew() bool {
return r.endpoint == "" || r.ks == nil || time.Now().After(r.expiresAt)
}
type domainConfigProxy struct {
OTID OTID `json:"otid"`
Keys []json.RawMessage `json:"keys"`
KeysRefreshHint int64 `json:"keysRefreshHint"`
ServiceEndpoints []string `json:"serviceEndpoints"`
ks JWKSet
}
func (r *domainRenewer) renew(ctx context.Context, oc *OTClient) error {
res := &domainConfigProxy{}
err := oc.HTTPClient.Do(ctx, "GET", r.td.ConfigURL(), nil, nil, res)
if err != nil {
return err
}
if !res.OTID.Equal(r.td.OTID()) {
return fmt.Errorf("invalid OT-Auth config with %s, need %s", res.OTID.String(), r.td.OTID().String())
}
bs := make([][]byte, 0, len(res.Keys))
for _, b := range res.Keys {
bs = append(bs, []byte(b))
}
res.ks.Keys, err = ParseKeys(bs...)
if err != nil {
return err
}
if r.endpoint == "" || !stringsHas(res.ServiceEndpoints, r.endpoint) {
endpoint, err := SelectEndpoints(ctx, res.ServiceEndpoints, oc.HTTPClient)
if err != nil {
return err
}
r.endpoint = endpoint
}
r.ks = &res.ks
if res.KeysRefreshHint > 1 {
r.expiresAt = time.Now().Add(time.Duration(res.KeysRefreshHint) * time.Second)
} else {
r.expiresAt = time.Now().Add(time.Hour)
}
return nil
}
type serviceRenewer struct {
sync.RWMutex
otid OTID
vid *OTVID
endpoint string
}
// ServiceConfig ...
type ServiceConfig struct {
OTVID *OTVID // subject' OTVID to access the service
Endpoint string // service's endpoint
}
// Resolve ...
func (r *serviceRenewer) Resolve(ctx context.Context, oc *OTClient) (*ServiceConfig, error) {
obj, err := resolve(ctx, r, oc)
if err != nil {
return nil, err
}
return obj.(*ServiceConfig), nil
}
func (r *serviceRenewer) value() interface{} {
return &ServiceConfig{
OTVID: r.vid,
Endpoint: r.endpoint,
}
}
func (r *serviceRenewer) shouldRenew() bool {
return r.endpoint == "" || r.vid == nil || r.vid.ShouldRenew()
}
func (r *serviceRenewer) renew(ctx context.Context, oc *OTClient) error {
output, err := oc.Sign(ctx, SignInput{
Subject: oc.sub,
Audience: r.otid,
})
if err != nil {
return err
}
r.vid, err = ParseOTVIDInsecure(output.OTVID)
if err != nil {
return err
}
if r.endpoint == "" || !stringsHas(output.ServiceEndpoints, r.endpoint) {
r.endpoint, err = SelectEndpoints(ctx, output.ServiceEndpoints, oc.HTTPClient)
if err != nil {
return err
}
}
return nil
}
func stringsHas(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}