Skip to content

Commit 2c26bd0

Browse files
Implemented authentication handler - need to work on session manager (created function template)
1 parent d1bd49f commit 2c26bd0

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ authentication:
3838
address: "ldaps://openldap" # Use the service name from docker-compose
3939
admin_dn: ${LACLM_LDAP_ADMIN_DN}
4040
admin_password: ${LACLM_LDAP_ADMIN_PASSWORD}
41+
search_base: "ou=users,dc=example,dc=com"
4142

4243
backend_security:
4344
jwt_secret_token: ${JWT_SECRET_TOKEN}

config/authentication.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type LDAPConfig struct {
1717
Address string `yaml:"address,omitempty"`
1818
AdminDN string `yaml:"admin_dn,omitempty"`
1919
AdminPassword string `yaml:"admin_password,omitempty"`
20+
SearchBase string `yaml:"search_base,omitempty"`
2021
}
2122

2223
/* normalization function */
@@ -50,6 +51,14 @@ func (l *LDAPConfig) Normalize() error {
5051
Please check the docs for more information:
5152
`))
5253
}
54+
55+
if l.SearchBase == "" {
56+
return errors.New(heredoc.Doc(`
57+
LDAP search base is not specified in the configuration file.
58+
59+
Please check the docs for more information:
60+
`))
61+
}
5362

5463
return nil
5564
}

internal/auth/handler.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
11
package auth
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"go.uber.org/zap"
8+
9+
"github.com/PythonHacker24/linux-acl-management-backend/config"
10+
"github.com/PythonHacker24/linux-acl-management-backend/internal/session"
11+
)
12+
13+
/* Handles user login and creates a session */
14+
func LoginHandler(w http.ResponseWriter, r *http.Request) {
15+
16+
/* POST Request only - specified in routes */
17+
18+
var user User
19+
err := json.NewDecoder(r.Body).Decode(&user)
20+
if err != nil {
21+
http.Error(w, "Invalid request body", http.StatusBadRequest)
22+
return
23+
}
24+
25+
if user.Username == "" || user.Password == "" {
26+
http.Error(w, "Username and password are required", http.StatusBadRequest)
27+
return
28+
}
29+
30+
/* authenticate the user */
31+
authStatus := AuthenticateUser(user.Username,
32+
user.Password,
33+
config.BackendConfig.Authentication.LDAPConfig.SearchBase,
34+
)
35+
if !authStatus {
36+
zap.L().Warn("User with invalid credentials attempted to log in")
37+
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
38+
return
39+
}
40+
41+
/* after building session manager */
42+
session.CreateSession(user.Username)
43+
44+
/* generate JWT for user interaction */
45+
token, err := GenerateJWT(user.Username)
46+
if err != nil {
47+
zap.L().Error("Error generating token",
48+
zap.Error(err),
49+
)
50+
http.Error(w, "Error generating token", http.StatusInternalServerError)
51+
return
52+
}
53+
54+
/* create auth successful response */
55+
response := map[string]string{"token": token}
56+
w.Header().Set("Content-Type", "application/json")
57+
if err := json.NewEncoder(w).Encode(response); err != nil {
58+
zap.L().Error("Failed to encode response",
59+
zap.Error(err),
60+
)
61+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
62+
return
63+
}
64+
}

internal/session/session.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package session
2+
3+
func CreateSession(username string) {
4+
5+
}

0 commit comments

Comments
 (0)