-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
51 lines (45 loc) · 1.5 KB
/
token.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
package service
import (
"context"
"log/slog"
"time"
"github.com/antihax/goesi"
"github.com/ErikKalkoken/evebuddy/internal/api/sso"
"github.com/ErikKalkoken/evebuddy/internal/model"
)
// getValidToken returns a valid token for a character. Convenience function.
func (s *Service) getValidToken(ctx context.Context, characterID int32) (model.Token, error) {
t, err := s.r.GetToken(ctx, characterID)
if err != nil {
return model.Token{}, err
}
if err := s.ensureValidToken(ctx, &t); err != nil {
return model.Token{}, err
}
return t, nil
}
// ensureValidToken will automatically try to refresh a token that is already or about to become invalid.
func (s *Service) ensureValidToken(ctx context.Context, t *model.Token) error {
if !t.RemainsValid(time.Second * 60) {
slog.Debug("Need to refresh token", "characterID", t.CharacterID)
rawToken, err := sso.RefreshToken(s.httpClient, t.RefreshToken)
if err != nil {
return err
}
t.AccessToken = rawToken.AccessToken
t.RefreshToken = rawToken.RefreshToken
t.ExpiresAt = rawToken.ExpiresAt
err = s.r.UpdateOrCreateToken(ctx, t)
if err != nil {
return err
}
slog.Info("Token refreshed", "characterID", t.CharacterID)
}
return nil
}
// contextWithToken returns a new context with the ESI access token included
// so it can be used to authenticate requests with the goesi library.
func contextWithToken(ctx context.Context, accessToken string) context.Context {
ctx = context.WithValue(ctx, goesi.ContextAccessToken, accessToken)
return ctx
}