-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
60 lines (46 loc) · 1.17 KB
/
session.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
package middleware
import (
"time"
"github.com/0987363/vsub/models"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
)
var store *sessions.CookieStore
const cookieKey = "token"
func ConnectSession(key string) {
store = sessions.NewCookieStore([]byte(key))
}
func GetToken(c *gin.Context) (*models.Token, error) {
s, err := store.Get(c.Request, cookieKey)
if err != nil {
return nil, err
}
if s.IsNew {
return nil, nil
}
token := models.Token{
Version: s.Values["version"].(string),
UserID: s.Values["user_id"].(string),
Nonce: s.Values["nonce"].(string),
Hmac: s.Values["hmac"].(string),
}
exp := s.Values["expiry"].(string)
token.Expiry, err = time.Parse(time.RFC3339Nano, exp)
if err != nil {
return nil, models.Error("Expiry is invalid.")
}
return &token, nil
}
func SetToken(c *gin.Context, token *models.Token) error {
s, err := store.Get(c.Request, cookieKey)
if err != nil {
return err
}
s.Values["version"] = token.Version
s.Values["user_id"] = token.UserID
s.Values["nonce"] = token.Nonce
s.Values["hmac"] = token.Hmac
s.Values["expiry"] = token.Expiry.Format(time.RFC3339Nano)
s.Save(c.Request, c.Writer)
return nil
}