...
1 package bearer
2
3 import (
4 "context"
5 "time"
6 )
7
8
9 type Token struct {
10 Value string
11
12 CanExpire bool
13 Expires time.Time
14 }
15
16
17
18 func (t Token) Expired(now time.Time) bool {
19 if !t.CanExpire {
20 return false
21 }
22 now = now.Round(0)
23 return now.Equal(t.Expires) || now.After(t.Expires)
24 }
25
26
27 type TokenProvider interface {
28 RetrieveBearerToken(context.Context) (Token, error)
29 }
30
31
32
33 type TokenProviderFunc func(context.Context) (Token, error)
34
35
36
37 func (fn TokenProviderFunc) RetrieveBearerToken(ctx context.Context) (Token, error) {
38 return fn(ctx)
39 }
40
41
42
43 type StaticTokenProvider struct {
44 Token Token
45 }
46
47
48 func (s StaticTokenProvider) RetrieveBearerToken(context.Context) (Token, error) {
49 return s.Token, nil
50 }
51
View as plain text