-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoken.go
More file actions
50 lines (43 loc) · 1.05 KB
/
token.go
File metadata and controls
50 lines (43 loc) · 1.05 KB
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
package ink
import (
"fmt"
"time"
"crypto/rand"
)
var tokenMap map[string]typeToken
type typeToken struct {
time time.Time
data map[string]interface{}
}
func GUID() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
func (ctx *Context) TokenGet(key string) interface{} {
tokenId := ctx.Req.Header.Get("Token")
if token, ok := tokenMap[tokenId]; ok {
if value, ok := token.data[key]; ok {
return value
}
}
return nil
}
func (ctx *Context) TokenSet(key string, value interface{}) {
tokenId := ctx.Req.Header.Get("Token")
token, ok := tokenMap[tokenId]
if ok {
token.data[key] = value
}
}
func (ctx *Context) TokenNew() string {
if len(tokenMap) == 0 {
tokenMap = make(map[string]typeToken)
}
tokenId := GUID()
token := new(typeToken)
token.data = make(map[string]interface{})
tokenMap[tokenId] = *token
ctx.Req.Header.Set("Token", tokenId)
return tokenId
}