-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.go
51 lines (42 loc) · 1.09 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
package main
import (
"encoding/json"
"errors"
aesGo "github.com/aasaam/aes-go"
)
type persistToken struct {
Type string `json:"t"`
ClientIdent string `json:"c"`
Username string `json:"u"`
TTL int64 `json:"ttl"`
}
func newPersistToken(typeOfToken string, clientIdent string, username string, ttl int64) *persistToken {
pt := persistToken{
Type: typeOfToken,
ClientIdent: clientIdent,
Username: username,
TTL: ttl,
}
return &pt
}
func newPersistTokenFromString(tokenString string, secret string) (*persistToken, error) {
aes := aesGo.NewAasaamAES(secret)
jsonString := aes.DecryptTTL(tokenString)
if jsonString == "" {
return nil, errors.New("invalid token or expired")
}
var pt persistToken
err := json.Unmarshal([]byte(jsonString), &pt)
if err != nil {
return nil, errors.New("invalid token structure")
}
return &pt, nil
}
func (pt *persistToken) generate(secret string) string {
aes := aesGo.NewAasaamAES(secret)
byteData, err := json.Marshal(&pt)
if err != nil {
return ""
}
return aes.EncryptTTL(string(byteData), pt.TTL)
}