-
Notifications
You must be signed in to change notification settings - Fork 0
/
ams.go
66 lines (55 loc) · 1.33 KB
/
ams.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
package AMS
import (
"crypto/sha1"
"encoding/base64"
token "github.com/meanOs/TokenMaster"
"net/http"
"strconv"
"time"
)
func makehash(data string) string {
hasher := sha1.New()
hasher.Write([]byte(data))
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
// Don't forget to init beatrix in main code.
func Init(mongoUri, mysqlURI string) {
URI = mongoUri
SQL_URI = mysqlURI
token.Init(mongoUri)
initDb()
}
func GetToken(login, password, ip string) (int, string) {
if login == "" || password == "" {
return http.StatusBadRequest, ""
}
hashed, uid := GetPasswordHashed(login, password)
if hashed == "" || uid == "" {
return http.StatusBadRequest, ""
}
if makehash(password) == hashed {
code, t := token.NewToken(ip, uid)
return code, t.TokenId
} else {
return http.StatusUnauthorized, ""
}
}
func Register(name, username, login, avatarurl, pwd, website, email string) bool {
var acc = Account{
Login: login,
Password: makehash(pwd),
Username: username,
Name: name,
AvatarURL: avatarurl,
Developer: false,
Patreon: false,
Registered: time.Now().Format(time.RFC1123Z),
Website: website,
Email: email,
}
if CheckIfExists(email) {
return false
}
acc.UID = makehash(acc.Password + login + acc.Registered + strconv.Itoa(random(1000, 2000)))
return UpdateDB(acc)
}