-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.go
152 lines (136 loc) · 3.63 KB
/
tokens.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tokens
import (
"sync"
"time"
sha "github.com/go-corelibs/shasum"
"github.com/go-enjin/be/pkg/crypto"
"github.com/go-enjin/be/pkg/feature"
"github.com/go-enjin/be/pkg/log"
"github.com/go-enjin/be/pkg/maps"
)
var (
DefaultRandomBytes = 128
DefaultDuration time.Duration = time.Minute * 10
)
type instance struct {
value string
shasum string
expires time.Time
}
type registry struct {
alias map[string]string
cache map[string]map[string]instance
numBytes int
duration time.Duration
sync.RWMutex
}
// New creates a new feature.TokenFactory with the given expiration duration, if the expires value is less than 1s, the
// package default of 10m is used
func New(expires time.Duration) (factory feature.TokenFactory) {
if expires < 1 {
expires = DefaultDuration
}
factory = ®istry{
numBytes: DefaultRandomBytes,
duration: expires,
alias: make(map[string]string),
cache: make(map[string]map[string]instance),
}
return
}
func (r *registry) randomValue() (value string) {
var err error
if value, err = crypto.RandomValue(r.numBytes); err != nil {
log.ErrorF("crypto.RandomValue error: %v", err)
}
return
}
func (r *registry) expire() {
r.Lock()
defer r.Unlock()
now := time.Now()
prune := make(map[string][]instance)
for key, values := range r.cache {
for _, token := range values {
if now.After(token.expires) {
prune[key] = append(prune[key], token)
}
}
}
for key, values := range prune {
for _, token := range values {
delete(r.cache[key], token.value)
delete(r.alias, token.shasum)
}
}
}
func (r *registry) verify(key, value string) (valid bool) {
r.expire()
r.Lock()
defer r.Unlock()
var present bool
var token instance
if len(value) == 10 {
if v, aliased := r.alias[value]; aliased {
value = v
}
}
if token, present = r.cache[key][value]; present {
valid = time.Now().Before(token.expires)
}
if valid {
// purge all tokens for key
delete(r.cache, key)
} else if present {
// delete just the value
delete(r.cache[key], value)
delete(r.alias, token.shasum)
}
return
}
func (r *registry) get(key string, duration time.Duration) (value, shasum string) {
r.expire()
r.Lock()
defer r.Unlock()
if duration < 1 {
duration = r.duration
}
value = r.randomValue()
shasum, _ = sha.BriefSum(value)
maps.MakeTypedKey(key, r.cache)
r.cache[key][value] = instance{
value: value,
shasum: shasum,
expires: time.Now().Add(duration),
}
r.alias[shasum] = value
return
}
// VerifyToken will validate and evict the given nonce value
func (r *registry) VerifyToken(key, value string) (valid bool) {
valid = r.verify(key, value)
return
}
// CreateToken will add a new nonce associated with the given key
func (r *registry) CreateToken(key string) (value, shasum string) {
value, shasum = r.get(key, r.duration)
return
}
// CreateTokenWith will add a new token with the given duration, associated with the given key
func (r *registry) CreateTokenWith(key string, duration time.Duration) (value, shasum string) {
value, shasum = r.get(key, duration)
return
}