Skip to content

Commit

Permalink
Tabs, not spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Smith committed Nov 14, 2023
1 parent c54b37c commit 95ed047
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 83 deletions.
16 changes: 8 additions & 8 deletions api-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
50 changes: 25 additions & 25 deletions lambda/shared/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ func (l lpaStoreClaims) Validate() error {

if iss == sirius {
emailRegex := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
if !emailRegex.MatchString(sub) {
return errors.New("Subject is not a valid email")
}
}

if iss == mrlpa {
uidRegex := regexp.MustCompile("^.+$")
if !uidRegex.MatchString(sub) {
return errors.New("Subject is not a valid UID")
}
}

return nil
if !emailRegex.MatchString(sub) {
return errors.New("Subject is not a valid email")
}
}

if iss == mrlpa {
uidRegex := regexp.MustCompile("^.+$")
if !uidRegex.MatchString(sub) {
return errors.New("Subject is not a valid UID")
}
}

return nil
}

type JWTVerifier struct {
Expand All @@ -92,19 +92,19 @@ func NewJWTVerifier() JWTVerifier {
func (v JWTVerifier) VerifyToken(tokenStr string) error {
lsc := lpaStoreClaims{}

parsedToken, err := jwt.ParseWithClaims(tokenStr, &lsc, func(token *jwt.Token) (interface{}, error) {
parsedToken, err := jwt.ParseWithClaims(tokenStr, &lsc, func(token *jwt.Token) (interface{}, error) {
return v.secretKey, nil
})
})

if err != nil {
return err
}
if err != nil {
return err
}

if !parsedToken.Valid {
return fmt.Errorf("Invalid JWT")
}
if !parsedToken.Valid {
return fmt.Errorf("Invalid JWT")
}

return nil
return nil
}

var bearerRegexp = regexp.MustCompile("^Bearer:[ ]+")
Expand All @@ -113,10 +113,10 @@ var bearerRegexp = regexp.MustCompile("^Bearer:[ ]+")
func (v JWTVerifier) VerifyHeader(event events.APIGatewayProxyRequest) error {
jwtHeaders := GetEventHeader("X-Jwt-Authorization", event)

if len(jwtHeaders) > 0 {
tokenStr := bearerRegexp.ReplaceAllString(jwtHeaders[0], "")
if len(jwtHeaders) > 0 {
tokenStr := bearerRegexp.ReplaceAllString(jwtHeaders[0], "")
return v.VerifyToken(tokenStr)
}
}

return errors.New("No JWT authorization header present")
}
100 changes: 50 additions & 50 deletions lambda/shared/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
}

Expand All @@ -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{
Expand Down

0 comments on commit 95ed047

Please sign in to comment.