-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
executable file
·139 lines (109 loc) · 3.03 KB
/
auth.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"errors"
"math/rand"
net "net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
const nums = "1234567890"
const letters = "1234567890_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
type auth struct {
ID int `form:"id"`
Code string `form:"code"`
}
// check if user loged in
func Auth() gin.HandlerFunc {
return func(c *gin.Context) {
if _, e := c.Cookie("auth"); e == nil {
c.Next()
return
} else {
token, err := c.Cookie("token")
username, e := c.Cookie("username")
if err == nil && e == nil && DecryptToken(token, c) == username {
c.Next()
return
}
}
c.AbortWithStatus(401)
}
}
// 30 ms speed average
func DecryptToken(token string, c *gin.Context) (username string) {
key := 0
minus := 0
for i, char := range token {
if key == i {
if char%2 == 0 {
username += string(char - 1)
} else {
username += string(char + 1)
}
key += minus + 1
minus += 1
}
}
c.SetSameSite(net.SameSiteNoneMode)
c.SetCookie("auth", "auth", 1800, "/", "."+config.SELF_DOMAIN_NAME, true, true)
return
}
func EncryptToken(username string) (token string) {
for i, char := range username {
if char%2 == 0 {
token += string(char - 1)
} else {
token += string(char + 1)
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, i)
for i := range b {
b[i] = letters[r.Int63()%int64(len(letters))]
}
token += string(b)
}
return
}
// Check code's fit for ensure
func CheckCode(id int, code string, c *gin.Context) error {
if !isCode(code) {
return errors.New("0")
}
var data bson.M
opts := options.FindOne().SetProjection(bson.M{"_id": 1})
if err := DB["ensure"].FindOne(ctx, bson.M{"_id": id, "code": code}, opts).Decode(&data); err != nil {
return errors.New("1")
}
// Delete document in ensure collection, if given code was valid
DB["ensure"].DeleteOne(ctx, bson.M{"_id": id, "code": code})
var user bson.M
opt := options.FindOne().SetProjection(bson.M{"username": 1})
if err := DB["users"].FindOne(ctx, bson.M{"_id": id}, opt).Decode(&user); err != nil {
return errors.New("2")
}
DB["users"].UpdateOne(ctx, bson.M{"_id": id}, bson.D{{Key: "$set", Value: bson.D{
{Key: "status", Value: true},
{Key: "active", Value: true},
}}})
MakeCookies(strconv.Itoa(id), user["username"].(string), 86400*120, c)
return nil
}
// Cookies for auth
func MakeCookies(id, username string, time int, c *gin.Context) {
c.SetSameSite(net.SameSiteNoneMode)
c.SetCookie("token", EncryptToken(username), time, "/", "."+config.SELF_DOMAIN_NAME, true, true)
c.SetCookie("username", username, time, "/", "."+config.SELF_DOMAIN_NAME, true, true)
c.SetCookie("id", id, time, "/", "."+config.SELF_DOMAIN_NAME, true, true)
}
// Make token for auth any email operations or something :)
func MakeCode() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, 6)
for i := range b {
b[i] = nums[r.Int63()%int64(len(nums))]
}
return string(b)
}