-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth.go
75 lines (62 loc) · 2.1 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
// ref https://github.com/jtblin/go-ldap-client
package controllers
import (
"encoding/json"
"log"
"net/http"
ldap "github.com/jtblin/go-ldap-client"
)
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
var client = &ldap.LDAPClient{
Base: "dc=example,dc=com",
Host: "ldap.forumsys.com", // test server ref https://www.forumsys.com/2022/05/10/online-ldap-test-server/
Port: 389,
UseSSL: false,
InsecureSkipVerify: false,
SkipTLS: true, // fix for tls handshake err [maybe Host doesnt support newer TLS]
BindDN: "cn=read-only-admin,dc=example,dc=com",
BindPassword: "password",
UserFilter: "(uid=%s)",
GroupFilter: "(memberUid=%s)",
Attributes: []string{"givenName", "sn", "mail", "uid"},
}
// handles LDAP auth req
func AuthenticateLDAP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
var creds Credentials
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
defer client.Close()
authenticated, user, err := client.Authenticate(creds.Username, creds.Password)
if err != nil {
log.Printf("LDAP Authentication Error: %v", err)
http.Error(w, "Authentication failed due to server error", http.StatusInternalServerError)
return
}
if !authenticated {
log.Printf("Invalid credentials for user %s", creds.Username)
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
groups, err := client.GetGroupsOfUser(creds.Username)
if err != nil {
log.Printf("Error fetching groups for user %s: %v", creds.Username, err)
http.Error(w, "Authenticated but failed to retrieve groups", http.StatusInternalServerError)
return
}
response := map[string]interface{}{
"message": "Authenticated successfully",
"user": user,
"groups": groups,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}