@@ -17,7 +17,6 @@ import (
17
17
18
18
"github.com/davecgh/go-spew/spew"
19
19
"github.com/go-openapi/strfmt"
20
- "github.com/golang-jwt/jwt/v4"
21
20
log "github.com/sirupsen/logrus"
22
21
"gopkg.in/tomb.v2"
23
22
@@ -247,70 +246,13 @@ func NewAPIC(ctx context.Context, config *csconfig.OnlineApiClientCfg, dbClient
247
246
return ret , err
248
247
}
249
248
250
- // loadAPICToken attempts to retrieve and validate a JWT token from the local database.
251
- // It returns the token string, its expiration time, and a boolean indicating whether the token is valid.
252
- //
253
- // A token is considered valid if:
254
- // - it exists in the database,
255
- // - it is a properly formatted JWT with an "exp" claim,
256
- // - it is not expired or near expiry.
257
- func loadAPICToken (ctx context.Context , db * database.Client ) (string , time.Time , bool ) {
258
- token , err := db .GetConfigItem (ctx , "apic_token" )
259
- if err != nil {
260
- log .Debugf ("error fetching token from DB: %s" , err )
261
- return "" , time.Time {}, false
262
- }
263
-
264
- if token == "" {
265
- log .Debug ("no token found in DB" )
266
- return "" , time.Time {}, false
267
- }
268
-
269
- parser := new (jwt.Parser )
270
-
271
- tok , _ , err := parser .ParseUnverified (token , jwt.MapClaims {})
272
- if err != nil {
273
- log .Debugf ("error parsing token: %s" , err )
274
- return "" , time.Time {}, false
275
- }
276
-
277
- claims , ok := tok .Claims .(jwt.MapClaims )
278
- if ! ok {
279
- log .Debugf ("error parsing token claims: %s" , err )
280
- return "" , time.Time {}, false
281
- }
282
-
283
- expFloat , ok := claims ["exp" ].(float64 )
284
- if ! ok {
285
- log .Debug ("token missing 'exp' claim" )
286
- return "" , time.Time {}, false
287
- }
288
-
289
- exp := time .Unix (int64 (expFloat ), 0 )
290
- if time .Now ().UTC ().After (exp .Add (- 1 * time .Minute )) {
291
- log .Debug ("auth token expired" )
292
- return "" , time.Time {}, false
293
- }
294
-
295
- return token , exp , true
296
- }
297
-
298
- // saveAPICToken stores the given JWT token in the local database under the "apic_token" config item.
299
- func saveAPICToken (ctx context.Context , db * database.Client , token string ) error {
300
- if err := db .SetConfigItem (ctx , "apic_token" , token ); err != nil {
301
- return fmt .Errorf ("saving token to db: %w" , err )
302
- }
303
-
304
- return nil
305
- }
306
-
307
249
// Authenticate ensures the API client is authorized to communicate with the CAPI.
308
250
// It attempts to reuse a previously saved JWT token from the database, falling back to
309
251
// an authentication request if the token is missing, invalid, or expired.
310
252
//
311
253
// If a new token is obtained, it is saved back to the database for caching.
312
254
func (a * apic ) Authenticate (ctx context.Context , config * csconfig.OnlineApiClientCfg ) error {
313
- if token , exp , valid := loadAPICToken (ctx , a . dbClient ); valid {
255
+ if token , exp , valid := a . dbClient . LoadAPICToken (ctx , log . StandardLogger () ); valid {
314
256
log .Debug ("using valid token from DB" )
315
257
316
258
a .apiClient .GetClient ().Transport .(* apiclient.JWTTransport ).Token = token
@@ -343,7 +285,7 @@ func (a *apic) Authenticate(ctx context.Context, config *csconfig.OnlineApiClien
343
285
344
286
a .apiClient .GetClient ().Transport .(* apiclient.JWTTransport ).Token = authResp .Token
345
287
346
- return saveAPICToken ( ctx , a .dbClient , authResp .Token )
288
+ return a .dbClient . SaveAPICToken ( ctx , authResp .Token )
347
289
}
348
290
349
291
// keep track of all alerts in cache and push it to CAPI every PushInterval.
0 commit comments