forked from openfaas/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
48 lines (38 loc) · 1.13 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
package sdk
import (
"time"
)
// expiryDelta determines how much earlier a token should be considered
// expired than its actual expiration time. It is used to avoid late
// expirations due to client-server time mismatches.
const expiryDelta = 10 * time.Second
// Token represents an OpenFaaS ID token
type Token struct {
// IDToken is the OIDC access token that authorizes and authenticates
// the requests.
IDToken string
// Expiry is the expiration time of the access token.
//
// A zero value means the token never expires.
Expiry time.Time
}
// Expired reports whether the token is expired, and will start
// to return false 10s before the actual expiration time.
func (t *Token) Expired() bool {
if t.Expiry.IsZero() {
return false
}
return t.Expiry.Round(0).Add(-expiryDelta).Before(time.Now())
}
type tokenJSON struct {
AccessToken string `json:"access_token"`
IDToken string `json:"id_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
func (t *tokenJSON) expiry() (exp time.Time) {
if v := t.ExpiresIn; v != 0 {
return time.Now().Add(time.Duration(v) * time.Second)
}
return
}