-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add AuthMiddleware middleware to server (#42)
- Loading branch information
1 parent
42cef10
commit 1099b98
Showing
7 changed files
with
202 additions
and
11 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
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
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,37 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
//go:generate moq -out mock_auth.go . Authentication | ||
|
||
type Authentication interface { | ||
ValidateToken(ctx context.Context, token string) (bool, error) | ||
} | ||
|
||
// AuthMiddleware creates an http middleware that validates auth tokens in requests. | ||
// | ||
// It takes an Authentication interface and returns a middleware function that checks | ||
// for valid Authorization header tokens, responding with 401 Unauthorized if | ||
// validation fails. | ||
func AuthMiddleware(auth Authentication) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
token := r.Header.Get("Authorization") | ||
if token == "" { | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
valid, err := auth.ValidateToken(r.Context(), token) | ||
if err != nil || !valid { | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
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,59 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/glass-cms/glasscms/internal/server/middleware" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAuthMiddleware(t *testing.T) { | ||
mockAuth := &middleware.AuthenticationMock{} | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
middleware := middleware.AuthMiddleware(mockAuth) | ||
wrappedHandler := middleware(handler) | ||
|
||
t.Run("Valid Token", func(t *testing.T) { | ||
mockAuth.ValidateTokenFunc = func(_ context.Context, _ string) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("Authorization", "valid-token") | ||
w := httptest.NewRecorder() | ||
|
||
wrappedHandler.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
}) | ||
|
||
t.Run("Invalid Token", func(t *testing.T) { | ||
mockAuth.ValidateTokenFunc = func(_ context.Context, _ string) (bool, error) { | ||
return false, nil | ||
} | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("Authorization", "invalid-token") | ||
w := httptest.NewRecorder() | ||
|
||
wrappedHandler.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, http.StatusUnauthorized, w.Code) | ||
}) | ||
|
||
t.Run("Missing Token", func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
w := httptest.NewRecorder() | ||
|
||
wrappedHandler.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, http.StatusUnauthorized, w.Code) | ||
}) | ||
} |
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,2 @@ | ||
// Package middleware contains application specific middleware. | ||
package middleware |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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