-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.go
61 lines (56 loc) · 1.26 KB
/
cookies.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
package main
import (
"log"
"net/http"
"strconv"
"time"
)
func addCookie(w http.ResponseWriter, name, value, path string) {
cookie := http.Cookie{
Name: name,
Value: value,
Path: path,
Secure: true,
MaxAge: 0,
HttpOnly: true,
}
http.SetCookie(w, &cookie)
}
func getCookie(r *http.Request, name string) (string, error) {
cookie, err := r.Cookie(name)
if err != nil {
log.Println("[ERROR]: ", err)
return "", err
}
return cookie.Value, nil
}
func generateToken(user User, t time.Time) string {
// valid for 7 day
var duratuin int64 = 604800
token := uint64(user.ID) + uint64(t.Unix()+duratuin)
strToken := strconv.FormatUint(token, 10)
formatedID := strconv.FormatUint(user.ID, 10)
strToken = formatedID + strToken
// println("new token: ", strToken,"-- len: ",len(strToken))
return strToken
}
func verifyToken(token string) bool {
id := token[:18]
idInt, err := strconv.ParseUint(id, 10, 64)
if err != nil {
log.Println("[ERROR]: ", err)
return false
}
token = token[18:]
tokenUint, err := strconv.ParseUint(token, 10, 64)
if err != nil {
log.Println("[ERROR]: ", err)
return false
}
to := idInt + uint64(time.Now().Unix())
if tokenUint < to {
log.Println("[WARNING]: Token expired")
return false
}
return true
}