55 "net/http"
66 "time"
77
8- "github.com/PythonHacker24/linux-acl-management-backend/internal/token"
98 "go.uber.org/zap"
9+
10+ "github.com/PythonHacker24/linux-acl-management-backend/internal/token"
1011)
1112
1213/* logging middleware for http requests */
@@ -32,11 +33,14 @@ func LoggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
3233 })
3334}
3435
35- /* authentication middleware for http requests */
36+ /*
37+ authentication middleware for http requests
38+ return username and sessionID with context
39+ */
3640func AuthenticationMiddleware (next http.HandlerFunc ) http.HandlerFunc {
3741 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
3842 /* authenticate the request through JWT */
39- username , err := token .ExtractUsernameFromRequest (r )
43+ username , sessionID , err := token .ExtractDataFromRequest (r )
4044 if err != nil {
4145 zap .L ().Error ("Error during authentication" ,
4246 zap .Error (err ),
@@ -47,8 +51,9 @@ func AuthenticationMiddleware(next http.HandlerFunc) http.HandlerFunc {
4751 /* set the header with the username */
4852 r .Header .Set ("X-User" , username )
4953
50- /* pass username as context */
51- ctx := context .WithValue (r .Context (), "username" , username )
54+ /* pass username and sessionID as context */
55+ ctx := context .WithValue (r .Context (), ContextKeyUsername , username )
56+ ctx = context .WithValue (ctx , ContextKeySessionID , sessionID )
5257
5358 /* return the handler */
5459 next (w , r .WithContext (ctx ))
0 commit comments