-
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to extract client connection user id from http header (#730)
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/centrifugal/centrifuge" | ||
) | ||
|
||
// UserHeaderAuth is a middleware that extracts the value of user ID from the specific header | ||
// and sets connection credentials. | ||
func UserHeaderAuth(userHeaderName string) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
userID := r.Header.Get(userHeaderName) | ||
if userID != "" { | ||
ctx := centrifuge.SetCredentials(r.Context(), ¢rifuge.Credentials{ | ||
UserID: userID, | ||
}) | ||
r = r.WithContext(ctx) | ||
} | ||
// Call the next handler | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/centrifugal/centrifuge" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func userHeaderAuthTestHandler(t *testing.T, userMustBeSet bool) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
cred, ok := centrifuge.GetCredentials(r.Context()) | ||
if userMustBeSet { | ||
require.True(t, ok, "credentials should be set") | ||
require.Equal(t, "123", cred.UserID, "user ID should be set correctly") | ||
} else { | ||
require.False(t, ok) | ||
} | ||
_, _ = w.Write([]byte("OK")) | ||
}) | ||
} | ||
|
||
func TestUserHeaderAuthWithUserID(t *testing.T) { | ||
middleware := UserHeaderAuth("X-User-ID") | ||
req := httptest.NewRequest("GET", "/test", nil) | ||
req.Header.Set("X-User-ID", "123") | ||
rr := httptest.NewRecorder() | ||
|
||
handler := middleware(userHeaderAuthTestHandler(t, true)) | ||
handler.ServeHTTP(rr, req) | ||
|
||
require.Equal(t, http.StatusOK, rr.Code, "status code should be 200 OK") | ||
} | ||
|
||
func TestUserHeaderAuthWithoutUserID(t *testing.T) { | ||
middleware := UserHeaderAuth("X-User-ID") | ||
req := httptest.NewRequest("GET", "/test", nil) | ||
rr := httptest.NewRecorder() | ||
|
||
handler := middleware(userHeaderAuthTestHandler(t, false)) | ||
handler.ServeHTTP(rr, req) | ||
|
||
require.Equal(t, http.StatusOK, rr.Code, "status code should be 200 OK") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters