-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
113 lines (99 loc) · 2.9 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
package auth
import (
"errors"
"io/ioutil"
"log"
)
//Creates a new accounts with the given username and password
func Register(username string, password string) error {
//Logs if necessray
if Settings.DebugLevel >= 1 {
log.Println("Registering", username, "width password", password)
}
//prepares the account
account := Account{}
account.Salt = getRandomString(512)
account.Password = mySha512(password + account.Salt)
account.Username = username
//opens the file in which the account will be stored
a, err := ioutil.ReadFile(Settings.AccountsFile)
if err != nil {
log.Println(err)
return err
}
//converts the accounts file into a go struct
accounts := parseAccounts(a)
//adds the account to the accounts strcut
err = accounts.addAccount(account)
if err != nil {
log.Println(err)
return err
}
//writes the accounts to the accounst file
accounts.writeToFile(Settings.AccountsFile)
return nil
}
//returns a token for a user with a given username and password and ip if you
//have set CheckIp in the config file to true, otherwise use "" or "0.0.0.0" as
//ip
func Login(username string, password string, ip string) (Token, error) {
if Settings.DebugLevel >= 1 {
log.Println("Logging", username, "width password", password, "on ip", ip)
}
//reads the accounts file
a, err := ioutil.ReadFile(Settings.AccountsFile)
if err != nil {
log.Println(err)
return Token{}, err
}
//converts the accounts file to a golang struct
accounts := parseAccounts(a)
//gets the accounts with the specified username
account, err := accounts.getAccount(username)
if err != nil {
log.Println(err)
return Token{}, err
}
//checks for the password to be valid
if account.checkPassword(password) {
//gets the token of the user
tok := account.getToken(ip)
//if there is no token, the function above creates one, so we have to write
//the changes that could have been made
err = accounts.setAccount(*account)
if err != nil {
log.Fatal(err)
}
accounts.writeToFile(Settings.AccountsFile)
return tok, nil
}
return Token{}, errors.New("Invalid password")
}
//returns true or false depending on wether a token is valid for a user
//and it's ip if you have set CheckIp in the config file to true, otherwise use
//"" or "0.0.0.0" as ip
func ValidateToken(username string, ip string, token string) (bool, error) {
if Settings.DebugLevel >= 1 {
log.Println("Validating", token, "with username", username, "and ip", ip)
}
//opens the accounts file
a, err := ioutil.ReadFile(Settings.AccountsFile)
if err != nil {
log.Println(err)
return false, err
}
//converts the accounts file to a golang struct
accounts := parseAccounts(a)
//gets the requested account
account, err := accounts.getAccount(username)
if err != nil {
log.Println(err)
return false, err
}
//check wether it's token is valid or not
if account.getToken(ip).Token == token {
return true, nil
} else {
return false, nil
}
}