-
Notifications
You must be signed in to change notification settings - Fork 0
/
localauthmodule.go
57 lines (50 loc) · 1.55 KB
/
localauthmodule.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
package session
import (
"fmt"
"io/ioutil"
"encoding/json"
)
type localAuth struct {
localAuthFileData []fileFormat
}
type fileFormat struct {
Username string `json:"username"`
Password string `json:"password"`
Active bool `json:"active"`
}
// NewLdap function initializes the ldap module
func NewLocalAuth() *localAuth {
return &localAuth{
localAuthFileData: getSuperAdminUsers(LocalAuthFileLoc),
}
}
func getSuperAdminUsers(filepath string) []fileFormat {
fmt.Println("Inside getSuperAdmin")
fmt.Println("filepath to read from =", filepath)
file, e := ioutil.ReadFile(filepath)
if e != nil {
fmt.Println("Error in reading file")
return []fileFormat{}
}
var jsondata []fileFormat
json.Unmarshal(file, &jsondata)
fmt.Println("json data read successfully")
for _, element := range jsondata{
fmt.Println("user = ",element.Username, "password = ", element.Password)
}
return jsondata
}
func (l *localAuth) authenticate(cred Credentials) (LoginResponse, error) {
fmt.Println("Inside localAuth authenticate")
fmt.Println("username=", cred.Username, "Password=", cred.Password)
fmt.Println("Local auth file data=")
for _, element := range l.localAuthFileData {
fmt.Println("user = ",element.Username, "password = ", element.Password)
if (element.Username == cred.Username){
if(element.Password == cred.Password && element.Active==true){
return LoginResponse{Authenticated: true, Message: "success"}, nil
}
}
}
return LoginResponse{Authenticated: false, Message: "Invalid username or password"}, nil
}