/
token.go
134 lines (114 loc) · 2.91 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package secure
import (
"strings"
"sync"
"time"
"github.com/google/uuid"
)
const (
TOKEN_TYPE_BEARER = "Bearer"
TOKEN_TYPE_REFRESH = "Refresh"
)
// Token is used to authenticate a user.
// All fields are private so will not be modified outside of this package.
type Token struct {
id string
ttype string // token type
realm string
client string
subject string
scope []string
issuedAt time.Time
expiresAt time.Time
}
// NewToken creates a new token.
func NewToken(ttype, realm, client, subject string, scope []string) *Token {
return &Token{
ttype: ttype,
realm: realm,
client: client,
subject: subject,
scope: scope,
}
}
func (t *Token) Realm() string {
return t.realm
}
func (t *Token) Client() string {
return t.client
}
func (t *Token) Subject() string {
return t.subject
}
func (t *Token) Scope() []string {
return t.scope
}
func (t *Token) HasScope(scope string) bool {
if t != nil {
for _, s := range t.scope {
if strings.EqualFold(s, scope) {
return true
}
}
}
return false
}
func (t *Token) IssuedAt() time.Time {
return t.issuedAt
}
func (t *Token) ExpiresAt() time.Time {
return t.expiresAt
}
func (t *Token) IsExpired() bool {
return !t.expiresAt.IsZero() && t.expiresAt.After(time.Now().UTC())
}
// TokenStore used to manage tokens.
type TokenStore interface {
// Issue issues a new token with the given ttl.
Issue(token *Token, ttl time.Duration) (string, error)
// Renew renews the token and returns the new one.
Renew(value string, ttl time.Duration) (string, error)
// Verify verifies the token and returns the token if valid.
Verify(value string) (*Token, error)
// Revoke revokes the token and returns the token if revoked.
Revoke(value string) (*Token, error)
}
// InMemoryTokenStore is an in-memory token store.
type InMemoryTokenStore struct {
tokens sync.Map
}
var _ TokenStore = (*InMemoryTokenStore)(nil)
func (s *InMemoryTokenStore) Issue(token *Token, ttl time.Duration) (string, error) {
token.id = uuid.NewString()
token.issuedAt = time.Now().UTC()
token.expiresAt = token.issuedAt.Add(ttl)
s.tokens.Store(token.id, token)
return token.id, nil
}
func (s *InMemoryTokenStore) Renew(value string, ttl time.Duration) (string, error) {
if value, exists := s.tokens.Load(value); exists {
token := value.(*Token)
token.issuedAt = time.Now().UTC()
token.expiresAt = token.issuedAt.Add(ttl)
return token.id, nil
}
return "", ErrInvalidToken
}
func (s *InMemoryTokenStore) Verify(value string) (*Token, error) {
if value, exists := s.tokens.Load(value); exists {
token := value.(*Token)
if token.IsExpired() {
s.tokens.Delete(token.id)
return token, ErrInvalidToken
}
return token, nil
}
return nil, ErrInvalidToken
}
func (s *InMemoryTokenStore) Revoke(value string) (*Token, error) {
if value, exists := s.tokens.LoadAndDelete(value); exists {
token := value.(*Token)
return token, nil
}
return nil, nil
}