Skip to content

Commit 5d78245

Browse files
Updated middleware for username and session ID as context parameters
1 parent 22cd1cc commit 5d78245

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

api/middleware/middleware.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
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+
*/
3640
func 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

Comments
 (0)