Skip to content

Commit

Permalink
oauthlib: add method to set a token as expired
Browse files Browse the repository at this point in the history
This can be used by backends to trigger a refresh of an access token if
they detect an invalid token.
  • Loading branch information
Impelon authored and ncw committed Jul 8, 2022
1 parent 62bcc84 commit 53400d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/oauthutil/oauthutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ func (ts *TokenSource) Invalidate() {
ts.mu.Unlock()
}

// Expire marks the token as expired
//
// This also marks the token in the config file as expired, if it is the same one
func (ts *TokenSource) Expire() error {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.token.Expiry = time.Now().Add(time.Hour * (-1)) // expire token
t, err := GetToken(ts.name, ts.m)
if err != nil {
return err
}
if t.AccessToken == ts.token.AccessToken {
err = PutToken(ts.name, ts.m, ts.token, false)
}
return err
}

// timeToExpiry returns how long until the token expires
//
// Call with the lock held
Expand Down
5 changes: 5 additions & 0 deletions lib/oauthutil/renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ func (r *Renew) Stop() {
func (r *Renew) Invalidate() {
r.ts.Invalidate()
}

// Expire expires the token source
func (r *Renew) Expire() error {
return r.ts.Expire()
}

0 comments on commit 53400d7

Please sign in to comment.