-
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.
- Loading branch information
Elliot Smith
committed
Nov 14, 2023
1 parent
c54b37c
commit 95ed047
Showing
3 changed files
with
83 additions
and
83 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 |
---|---|---|
|
@@ -52,14 +52,14 @@ func main() { | |
secretKey := []byte(*jwtSecret) | ||
|
||
claims := jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
tokenString, _ := token.SignedString(secretKey) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
tokenString, _ := token.SignedString(secretKey) | ||
|
||
req.Header.Add("X-Jwt-Authorization", fmt.Sprintf("Bearer: %s", tokenString)) | ||
} | ||
|
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 |
---|---|---|
|
@@ -6,8 +6,8 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
@@ -18,11 +18,11 @@ var verifier = JWTVerifier{ | |
} | ||
|
||
func createToken(claims jwt.MapClaims) string { | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
|
||
tokenString, _ := token.SignedString(secretKey) | ||
tokenString, _ := token.SignedString(secretKey) | ||
|
||
return tokenString | ||
return tokenString | ||
} | ||
|
||
func TestVerifyEmptyJwt(t *testing.T) { | ||
|
@@ -32,11 +32,11 @@ func TestVerifyEmptyJwt(t *testing.T) { | |
|
||
func TestVerifyExpInPast(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * -24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.makeregister", | ||
"sub": "M-3467-89QW-ERTY", | ||
}) | ||
"exp": time.Now().Add(time.Hour * -24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.makeregister", | ||
"sub": "M-3467-89QW-ERTY", | ||
}) | ||
|
||
err := verifier.VerifyToken(token) | ||
|
||
|
@@ -48,11 +48,11 @@ func TestVerifyExpInPast(t *testing.T) { | |
|
||
func TestVerifyIatInFuture(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * 24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * 24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
|
||
err := verifier.VerifyToken(token) | ||
|
||
|
@@ -64,11 +64,11 @@ func TestVerifyIatInFuture(t *testing.T) { | |
|
||
func TestVerifyIssuer(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "daadsdaadsadsads", | ||
"sub": "[email protected]", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "daadsdaadsadsads", | ||
"sub": "[email protected]", | ||
}) | ||
|
||
err := verifier.VerifyToken(token) | ||
|
||
|
@@ -80,11 +80,11 @@ func TestVerifyIssuer(t *testing.T) { | |
|
||
func TestVerifyBadEmailForSiriusIssuer(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "", | ||
}) | ||
|
||
err := verifier.VerifyToken(token) | ||
|
||
|
@@ -96,11 +96,11 @@ func TestVerifyBadEmailForSiriusIssuer(t *testing.T) { | |
|
||
func TestVerifyBadUIDForMRLPAIssuer(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.makeregister", | ||
"sub": "", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.makeregister", | ||
"sub": "", | ||
}) | ||
|
||
err := verifier.VerifyToken(token) | ||
|
||
|
@@ -112,29 +112,29 @@ func TestVerifyBadUIDForMRLPAIssuer(t *testing.T) { | |
|
||
func TestVerifyGoodJwt(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
|
||
err := verifier.VerifyToken(token) | ||
err := verifier.VerifyToken(token) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestNewJWTVerifier(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
|
||
os.Setenv("JWT_SECRET_KEY", string(secretKey)) | ||
newVerifier := NewJWTVerifier() | ||
os.Unsetenv("JWT_SECRET_KEY") | ||
os.Setenv("JWT_SECRET_KEY", string(secretKey)) | ||
newVerifier := NewJWTVerifier() | ||
os.Unsetenv("JWT_SECRET_KEY") | ||
|
||
err := newVerifier.VerifyToken(token) | ||
err := newVerifier.VerifyToken(token) | ||
assert.Nil(t, err) | ||
} | ||
|
||
|
@@ -152,11 +152,11 @@ func TestVerifyHeaderNoJWTHeader(t *testing.T) { | |
|
||
func TestVerifyHeader(t *testing.T) { | ||
token := createToken(jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"iat": time.Now().Add(time.Hour * -24).Unix(), | ||
"iss": "opg.poas.sirius", | ||
"sub": "[email protected]", | ||
}) | ||
|
||
event := events.APIGatewayProxyRequest{ | ||
MultiValueHeaders: map[string][]string{ | ||
|