-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_manager.go
214 lines (174 loc) Β· 5.77 KB
/
token_manager.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 avs
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log/slog"
"strings"
"sync/atomic"
"time"
"github.com/aerospike/avs-client-go/protos"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// grpcTokenManager is responsible for managing authentication tokens and refreshing
// them when necessary.
//
//nolint:govet // We will favor readability over field alignment
type grpcTokenManager struct {
username string
password string
token atomic.Value
refreshTime atomic.Value
logger *slog.Logger
stopRefreshChan chan struct{}
refreshScheduled bool
}
// newGrpcJWTToken creates a new tokenManager instance with the provided username, password, and logger.
func newGrpcJWTToken(username, password string, logger *slog.Logger) *grpcTokenManager {
logger.WithGroup("jwt")
logger.Debug("creating new token manager")
return &grpcTokenManager{
username: username,
password: password,
logger: logger,
stopRefreshChan: make(chan struct{}),
}
}
// Close stops the scheduled token refresh and closes the token manager.
func (tm *grpcTokenManager) Close() {
if tm.refreshScheduled {
tm.logger.Debug("stopping scheduled token refresh")
tm.stopRefreshChan <- struct{}{}
<-tm.stopRefreshChan
}
tm.logger.Debug("closed")
}
// setRefreshTimeFromTTL sets the refresh time based on the provided time-to-live (TTL) duration.
func (tm *grpcTokenManager) setRefreshTimeFromTTL(ttl time.Duration) {
tm.refreshTime.Store(time.Now().Add(ttl))
}
// RefreshToken refreshes the authentication token using the provided gRPC client connection.
// It returns a boolean indicating if the token was successfully refreshed and
// an error if any. It is not thread safe.
func (tm *grpcTokenManager) RefreshToken(ctx context.Context, conn *connection) error {
// We only want one goroutine to refresh the token at a time
resp, err := conn.authClient.Authenticate(ctx, &protos.AuthRequest{
Credentials: createUserPassCredential(tm.username, tm.password),
})
if err != nil {
return fmt.Errorf("%s: %w", "failed to authenticate", err)
}
claims := strings.Split(resp.GetToken(), ".")
if len(claims) < 3 {
return fmt.Errorf("failed to authenticate: missing either header, payload, or signature")
}
decClaims, err := base64.RawURLEncoding.DecodeString(claims[1])
if err != nil {
return fmt.Errorf("%s: %w", "failed to authenticate", err)
}
tokenMap := make(map[string]any, 8)
err = json.Unmarshal(decClaims, &tokenMap)
if err != nil {
return fmt.Errorf("%s: %w", "failed to authenticate", err)
}
expiryToken, ok := tokenMap["exp"].(float64)
if !ok {
return fmt.Errorf("failed to authenticate: unable to find exp in token")
}
iat, ok := tokenMap["iat"].(float64)
if !ok {
return fmt.Errorf("failed to authenticate: unable to find iat in token")
}
ttl := time.Duration(expiryToken-iat) * time.Second
if ttl <= 0 {
return fmt.Errorf("failed to authenticate: jwt ttl is less than 0")
}
tm.logger.DebugContext(
ctx,
"successfully parsed token",
slog.Float64("exp", expiryToken),
slog.Float64("iat", iat),
slog.Duration("ttl", ttl),
)
// Set expiry based on local clock.
tm.setRefreshTimeFromTTL(ttl)
tm.token.Store("Bearer " + resp.GetToken())
return nil
}
// ScheduleRefresh schedules the token refresh using the provided function to
// get the gRPC client connection. This is not threadsafe. It should only be
// called once.
func (tm *grpcTokenManager) ScheduleRefresh(getConn func() (*connection, error)) {
if tm.refreshScheduled {
tm.logger.Warn("refresh already scheduled")
}
tm.logger.Debug("scheduling token refresh")
tm.refreshScheduled = true
timer := time.NewTimer(0)
go func() {
for {
connClients, err := getConn()
if err != nil {
tm.logger.Warn("failed to refresh token", slog.Any("error", err))
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
err = tm.RefreshToken(ctx, connClients)
if err != nil {
tm.logger.Warn("failed to refresh token", slog.Any("error", err))
}
cancel()
waitFor := time.Until(tm.refreshTime.Load().(time.Time)) - time.Second*5
tm.logger.Debug("waiting to refresh token", slog.Duration("waitTime", waitFor))
timer.Reset(waitFor)
select {
case <-timer.C:
case <-tm.stopRefreshChan:
tm.refreshScheduled = false
timer.Stop()
tm.stopRefreshChan <- struct{}{}
tm.logger.Debug("stopped scheduled token refresh")
return
}
}
}()
}
// RequireTransportSecurity returns true to indicate that transport security is required.
func (tm *grpcTokenManager) RequireTransportSecurity() bool {
return true
}
// UnaryInterceptor returns the grpc unary client interceptor that attaches the token to outgoing requests.
func (tm *grpcTokenManager) UnaryInterceptor() grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
return invoker(tm.attachToken(ctx), method, req, reply, cc, opts...)
}
}
// StreamInterceptor returns the grpc stream client interceptor that attaches the token to outgoing requests.
func (tm *grpcTokenManager) StreamInterceptor() grpc.StreamClientInterceptor {
return func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
return streamer(tm.attachToken(ctx), desc, cc, method, opts...)
}
}
// attachToken attaches the authentication token to the outgoing context.
func (tm *grpcTokenManager) attachToken(ctx context.Context) context.Context {
rawToken := tm.token.Load()
if rawToken == nil {
return ctx
}
return metadata.AppendToOutgoingContext(ctx, "Authorization", tm.token.Load().(string))
}