Skip to content

Commit

Permalink
fix(idtoken): fix cache expiration process
Browse files Browse the repository at this point in the history
Signed-off-by: Rintaro Okamura <[email protected]>
  • Loading branch information
rinx committed Jul 6, 2024
1 parent 22d90eb commit 8ec2100
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/service/google/idtoken/idtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Response struct {

type tokenSource struct {
tokenSource oauth2.TokenSource
expire time.Time
expiration time.Time
}

type Server interface {
Expand Down Expand Up @@ -164,12 +164,14 @@ func (s *server) newTokenSource(aud string) (oauth2.TokenSource, error) {
return nil, err
}

exp := time.Now().Add(s.tsCacheDuration)

s.tss[aud] = tokenSource{
tokenSource: nts,
expire: time.Now().Add(s.tsCacheDuration),
expiration: exp,
}

slog.Info("add tokensource cache", "audience", aud)
slog.Info("add tokensource cache", "audience", aud, "expiration", exp.Format(time.RFC3339Nano))

return nts, nil
}
Expand Down Expand Up @@ -222,10 +224,10 @@ func (s *server) refreshCache() error {
defer s.tssMu.Unlock()

for aud, ts := range s.tss {
if ts.expire.After(time.Now()) {
if time.Now().After(ts.expiration) {
delete(s.tss, aud)

slog.Info("delete tokensource cache", "audience", aud)
slog.Info("delete tokensource cache", "audience", aud, "expiration", ts.expiration)
}
}

Expand Down

0 comments on commit 8ec2100

Please sign in to comment.