-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
60 lines (53 loc) · 1.24 KB
/
password.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
package main
import (
"golang.org/x/crypto/bcrypt"
"net/http"
)
func HashPassword(sPassword string) (string, error) {
bPass, err := bcrypt.GenerateFromPassword([]byte(sPassword), 14)
return string(bPass), err
}
func CheckPasswordHash(sPassword, sHash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(sHash), []byte(sPassword))
return err == nil
}
func ValidUser(sUser, sPassword string, slcUsers []AppUser) bool {
for _, v := range slcUsers {
if sUser == v.User && CheckPasswordHash(sPassword, v.Password) {
return true
}
}
return false
}
func RequiresLogin(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !IsLoggedIn(r) {
http.Redirect(w, r, "/login", 302)
return
}
handler(w, r)
}
}
func UserAllowedURL(slcUsers []AppUser, sUser, sUrl string) bool {
for _, v := range slcUsers {
if v.User == sUser {
for _, url := range v.Url {
if url == sUrl {
return true
}
}
}
}
return false
}
func HomeUrl(users []AppUser, user string) string {
if user == "ADMIN" {
return "/dashboard"
}
for _, u := range users {
if u.User == user {
return u.Url[0] + "?o=" + user
}
}
return ""
}