@@ -11,58 +11,67 @@ import (
1111)
1212
1313/* Handles user login and creates a session */
14- func LoginHandler (w http.ResponseWriter , r * http.Request ) {
14+ func LoginHandler (sessionManager * session.Manager ) http.HandlerFunc {
15+ return func (w http.ResponseWriter , r * http.Request ) {
1516
16- /* POST Request only - specified in routes */
17+ /* POST Request only - specified in routes */
1718
18- /* decode the request body */
19- var user User
20- err := json .NewDecoder (r .Body ).Decode (& user )
21- if err != nil {
22- http .Error (w , "Invalid request body" , http .StatusBadRequest )
23- return
24- }
19+ /* decode the request body */
20+ var user User
21+ err := json .NewDecoder (r .Body ).Decode (& user )
22+ if err != nil {
23+ http .Error (w , "Invalid request body" , http .StatusBadRequest )
24+ return
25+ }
2526
26- /* check if username and password are specified */
27- if user .Username == "" || user .Password == "" {
28- http .Error (w , "Username and password are required" , http .StatusBadRequest )
29- return
30- }
27+ /* check if username and password are specified */
28+ if user .Username == "" || user .Password == "" {
29+ http .Error (w , "Username and password are required" , http .StatusBadRequest )
30+ return
31+ }
3132
32- /* authenticate the user */
33- authStatus := AuthenticateUser (user .Username ,
34- user .Password ,
35- config .BackendConfig .Authentication .LDAPConfig .SearchBase ,
36- )
33+ /* authenticate the user */
34+ authStatus := AuthenticateUser (user .Username ,
35+ user .Password ,
36+ config .BackendConfig .Authentication .LDAPConfig .SearchBase ,
37+ )
3738
38- /* check if authentication is successful */
39- if ! authStatus {
40- zap .L ().Warn ("User with invalid credentials attempted to log in" )
41- http .Error (w , "Invalid credentials" , http .StatusUnauthorized )
42- return
43- }
39+ /* check if authentication is successful */
40+ if ! authStatus {
41+ zap .L ().Warn ("User with invalid credentials attempted to log in" )
42+ http .Error (w , "Invalid credentials" , http .StatusUnauthorized )
43+ return
44+ }
4445
45- /* after building session manager */
46- session .CreateSession (user .Username )
46+ /* after building session manager */
47+ err = sessionManager .CreateSession (user .Username )
48+ if err != nil {
49+ zap .L ().Error ("Error creating session" ,
50+ zap .Error (err ),
51+ )
52+ http .Error (w , "Error creating session" , http .StatusInternalServerError )
53+ return
54+ }
4755
48- /* generate JWT for user interaction */
49- token , err := GenerateJWT (user .Username )
50- if err != nil {
51- zap .L ().Error ("Error generating token" ,
52- zap .Error (err ),
53- )
54- http .Error (w , "Error generating token" , http .StatusInternalServerError )
55- return
56- }
56+ /* generate JWT for user interaction */
57+ token , err := GenerateJWT (user .Username )
58+ if err != nil {
59+ zap .L ().Error ("Error generating token" ,
60+ zap .Error (err ),
61+ )
62+ http .Error (w , "Error generating token" , http .StatusInternalServerError )
63+ return
64+ }
5765
58- /* create auth successful response */
59- response := map [string ]string {"token" : token }
60- w .Header ().Set ("Content-Type" , "application/json" )
61- if err := json .NewEncoder (w ).Encode (response ); err != nil {
62- zap .L ().Error ("Failed to encode response for login request" ,
63- zap .Error (err ),
64- )
65- http .Error (w , "Failed to encode response for login request" , http .StatusInternalServerError )
66- return
66+ /* create auth successful response */
67+ response := map [string ]string {"token" : token }
68+ w .Header ().Set ("Content-Type" , "application/json" )
69+ if err := json .NewEncoder (w ).Encode (response ); err != nil {
70+ zap .L ().Error ("Failed to encode response for login request" ,
71+ zap .Error (err ),
72+ )
73+ http .Error (w , "Failed to encode response for login request" , http .StatusInternalServerError )
74+ return
75+ }
6776 }
6877}
0 commit comments