-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rest_gateway] Add authentication to gRPC REST gateway and include un…
…it testing (#1453) Features and improvements 1. JWT authentication for gRPC REST gateway - Integrated JWT authentication for secure communication. - Implemented middleware to validate JWT tokens from HTTP requests' authorization headers. 2. Include unit testing - Added Go tests for the REST gateway to ensure authentication middleware functionality and error handling. Co-authored-by: Zachary Fong <[email protected]> Co-authored-by: Ramon Figueiredo <[email protected]> Co-authored-by: Diego Tavares <[email protected]>
- Loading branch information
1 parent
1219377
commit db5a805
Showing
6 changed files
with
212 additions
and
48 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 @@ | ||
.env |
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
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,88 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJwtMiddleware(t *testing.T) { | ||
jwtSecret := []byte("test_secret") | ||
|
||
// Set up a sample handler to use with the middleware | ||
sampleHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("OK")) | ||
}) | ||
|
||
// Create a test server with the middleware | ||
ts := httptest.NewServer(jwtMiddleware(sampleHandler, jwtSecret)) | ||
defer ts.Close() | ||
|
||
t.Run("Valid Token", func(t *testing.T) { | ||
// Generate a valid token | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"sub": "test_user", | ||
"exp": time.Now().Add(time.Hour).Unix(), | ||
}) | ||
tokenString, err := token.SignedString(jwtSecret) | ||
assert.NoError(t, err) | ||
|
||
// Create a request with the valid token | ||
req, err := http.NewRequest("GET", ts.URL, nil) | ||
assert.NoError(t, err) | ||
req.Header.Set("Authorization", "Bearer "+tokenString) | ||
|
||
// Perform the request | ||
res, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
}) | ||
|
||
t.Run("Missing Token", func(t *testing.T) { | ||
// Create a request without a token | ||
req, err := http.NewRequest("GET", ts.URL, nil) | ||
assert.NoError(t, err) | ||
|
||
// Perform the request | ||
res, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusUnauthorized, res.StatusCode) | ||
}) | ||
|
||
t.Run("Invalid Token", func(t *testing.T) { | ||
// Create a request with an invalid token | ||
req, err := http.NewRequest("GET", ts.URL, nil) | ||
assert.NoError(t, err) | ||
req.Header.Set("Authorization", "Bearer invalid_token") | ||
|
||
// Perform the request | ||
res, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusUnauthorized, res.StatusCode) | ||
}) | ||
|
||
t.Run("Expired Token", func(t *testing.T) { | ||
// Generate an expired token | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"sub": "test_user", | ||
"exp": time.Now().Add(-time.Hour).Unix(), | ||
}) | ||
tokenString, err := token.SignedString(jwtSecret) | ||
assert.NoError(t, err) | ||
|
||
// Create a request with the expired token | ||
req, err := http.NewRequest("GET", ts.URL, nil) | ||
assert.NoError(t, err) | ||
req.Header.Set("Authorization", "Bearer "+tokenString) | ||
|
||
// Perform the request | ||
res, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusUnauthorized, res.StatusCode) | ||
}) | ||
} |
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,8 @@ | ||
package tools | ||
|
||
import ( | ||
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway" | ||
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2" | ||
_ "google.golang.org/grpc/cmd/protoc-gen-go-grpc" | ||
_ "google.golang.org/protobuf/cmd/protoc-gen-go" | ||
) |