@@ -11,7 +11,7 @@ import (
1111 "github.com/PythonHacker24/linux-acl-management-backend/internal/token"
1212)
1313
14- /* Handles user login and creates a session */
14+ /* handles user login and creates a session */
1515func LoginHandler (sessionManager * session.Manager ) http.HandlerFunc {
1616 return func (w http.ResponseWriter , r * http.Request ) {
1717
@@ -44,6 +44,11 @@ func LoginHandler(sessionManager *session.Manager) http.HandlerFunc {
4444 return
4545 }
4646
47+ /*
48+ check if the session already exists in the manager.
49+ if it exists, refresh it's timer and return a jwt token
50+ */
51+
4752 /* create session for the user */
4853 sessionID , err := sessionManager .CreateSession (user .Username , r .RemoteAddr , r .UserAgent ())
4954 if err != nil {
@@ -76,3 +81,46 @@ func LoginHandler(sessionManager *session.Manager) http.HandlerFunc {
7681 }
7782 }
7883}
84+
85+ /* handles user logout and expire session */
86+ func LogoutHandler (sessionManager * session.Manager ) http.HandlerFunc {
87+ return func (w http.ResponseWriter , r * http.Request ) {
88+
89+ /* authenticate the request through JWT */
90+ username , _ , err := token .ExtractDataFromRequest (r )
91+ if err != nil {
92+ zap .L ().Info ("Error during token extraction in logout" ,
93+ zap .Error (err ),
94+ )
95+ http .Error (w , "Error during token extraction in logout" , http .StatusInternalServerError )
96+ return
97+ }
98+
99+ err = sessionManager .ExpireSession (username )
100+ if err != nil {
101+ zap .L ().Error ("Failed to expire session during logout" ,
102+ zap .Error (err ),
103+ )
104+ http .Error (w , "Failed to expire session during logout" , http .StatusInternalServerError )
105+ return
106+ }
107+
108+ w .WriteHeader (http .StatusOK )
109+ }
110+ }
111+
112+ /* validate a token */
113+ func ValidateToken (w http.ResponseWriter , r * http.Request ) {
114+
115+ /* authenticate the request through JWT */
116+ _ , _ , err := token .ExtractDataFromRequest (r )
117+ if err != nil {
118+ zap .L ().Info ("Error during authentication" ,
119+ zap .Error (err ),
120+ )
121+ http .Error (w , "Authentication Failed" , http .StatusInternalServerError )
122+ return
123+ }
124+
125+ w .WriteHeader (http .StatusOK )
126+ }
0 commit comments