-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
44 lines (39 loc) · 785 Bytes
/
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
package main
import (
"encoding/json"
"os"
)
type Auth struct {
Passwords map[string]string
}
func (auth *Auth) LoadPasswords(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
ErrorLog.Println(err.Error())
return err
}
defer f.Close()
var passwords map[string]string
decoder := json.NewDecoder(f)
if err := decoder.Decode(&passwords); err != nil {
ErrorLog.Println(err.Error())
return err
}
auth.Passwords = passwords
return nil
}
func (auth *Auth) IsPasswordValid(username, password string) bool {
if auth.Passwords == nil {
return false
}
if p, found := auth.Passwords[username]; found && p == password {
return true
}
return false
}
func NewAuth() *Auth {
auth := Auth{
Passwords: make(map[string]string),
}
return &auth
}