-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckToken.go
113 lines (91 loc) · 2.31 KB
/
checkToken.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"database/sql"
"fmt"
"net/http"
"strings"
"time"
"net"
)
//////////////////update login token function//////////////////
func updatetoken(token string, userid string) {
db, err := sql.Open("mysql", sqlcred)
if err != nil {
panic(err.Error())
}
defer db.Close()
change, err := db.Prepare("UPDATE login SET session= ?, sessiontime=NOW() where id= ? ")
if err != nil {
panic(err.Error())
}
defer change.Close()
if _, err := change.Exec(token,userid); err != nil {
panic(err.Error())
}
}
//////////////////delete admin login token after X minutes function//////////////////
func deletetoken() {
db, err := sql.Open("mysql", sqlcred)
if err != nil {
panic(err.Error())
}
defer db.Close()
change, err := db.Query("UPDATE login SET session=\"\" where rechte = 1 and sessiontime < (NOW() - interval 10 minute) ")
if err != nil {
panic(err.Error())
}
defer change.Close()
}
//////////////////checktoken function//////////////////
func checktoken(w http.ResponseWriter, r *http.Request) string{
ip,_,_ := net.SplitHostPort(r.RemoteAddr)
agent := r.Header.Get("User-Agent")
t := time.Now()
formatted := fmt.Sprintf("%02d.%02d.%d", t.Day(), t.Month(), t.Year())
tokena := `l`+md5hash(formatted + " " + ip + " " + agent)+``
var (
id string
rechte string
username string
password string
l bool
)
c, err := r.Cookie(tokena)
if err != nil {
//panic(err.Error())
} else {
value := c.Value
splitsting := strings.Split(value, "_")
db, err := sql.Open("mysql", sqlcred)
if err != nil {
panic(err.Error())
}
defer db.Close()
err = db.QueryRow("SELECT id,rechte,username,password FROM login where id = ?", splitsting[0]).Scan(&id,&rechte,&username,&password)
if err != nil {
panic(err.Error())
}
if (id != "") {
token := md5hash(formatted + " " + ip + " " + agent + " " + username + " " + password)
if (token == splitsting[1]) {
c := http.Cookie{
Name: tokena,
Value: value,
MaxAge: 3600}
http.SetCookie(w, &c)
updatetoken(token, splitsting[0])
deletetoken()
l = true
} else {
deletetoken()
logoutHandler(w, r)
l = false
}
}
}
if l == true {
return ``+id+`_`+rechte+``
} else {
return `0_0`
}
}