-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
45 lines (38 loc) · 870 Bytes
/
token.go
File metadata and controls
45 lines (38 loc) · 870 Bytes
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
package main
import (
"bytes"
"encoding/json"
"time"
)
const (
DAY = time.Hour * 24
PASSWORD = DAY * 3
EMAIL = DAY * 7
ROBOT = time.Second * 5
)
type Token struct {
User int64 `json:"user_id,string"`
Scenario string `json:"scenario"`
Token string `json:"token"`
Meta map[string]string `json:"meta,omitempty"`
}
func (token *Token) MarshalBinary() (data []byte, err error) {
return json.Marshal(token)
}
func (token *Token) UnmarshalBinary(data []byte) error {
buffer := bytes.NewBuffer(data)
decoder := json.NewDecoder(buffer)
return decoder.Decode(token)
}
func (token *Token) Key() string {
return "tempus_" + token.Token
}
func (token *Token) Expires() time.Duration {
if "email" == token.Scenario {
return EMAIL
}
if "robot" == token.Scenario {
return ROBOT
}
return PASSWORD
}