-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
5 changed files
with
209 additions
and
134 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,81 @@ | ||
package appx | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/auth0/go-jwt-middleware/v2/validator" | ||
"github.com/reearth/reearthx/log" | ||
"github.com/reearth/reearthx/util" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
type JWTValidator interface { | ||
ValidateToken(ctx context.Context, tokenString string) (interface{}, error) | ||
} | ||
|
||
// JWTValidatorWithError wraps "validator.Validator and attach iss and aud to the error message to make it easy to track errors. | ||
type JWTValidatorWithError struct { | ||
validator *validator.Validator | ||
iss string | ||
aud []string | ||
} | ||
|
||
func NewJWTValidatorWithError( | ||
keyFunc func(context.Context) (interface{}, error), | ||
signatureAlgorithm validator.SignatureAlgorithm, | ||
issuerURL string, | ||
audience []string, | ||
opts ...validator.Option, | ||
) (*JWTValidatorWithError, error) { | ||
validator, err := validator.New( | ||
keyFunc, | ||
signatureAlgorithm, | ||
issuerURL, | ||
audience, | ||
opts..., | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &JWTValidatorWithError{ | ||
validator: validator, | ||
iss: issuerURL, | ||
aud: slices.Clone(audience), | ||
}, nil | ||
} | ||
|
||
func (v *JWTValidatorWithError) ValidateToken(ctx context.Context, token string) (interface{}, error) { | ||
res, err := v.validator.ValidateToken(ctx, token) | ||
if err != nil { | ||
err = fmt.Errorf("invalid JWT: iss=%s aud=%v err=%w", v.iss, v.aud, err) | ||
} | ||
return res, err | ||
} | ||
|
||
type JWTMultipleValidator []JWTValidator | ||
|
||
func NewJWTMultipleValidator(providers []JWTProvider) (JWTMultipleValidator, error) { | ||
return util.TryMap(providers, func(p JWTProvider) (JWTValidator, error) { | ||
return p.validator() | ||
}) | ||
} | ||
|
||
// ValidateToken Trys to validate the token with each validator | ||
// NOTE: the last validation error only is returned | ||
func (mv JWTMultipleValidator) ValidateToken(ctx context.Context, tokenString string) (res interface{}, err error) { | ||
for _, v := range mv { | ||
var err2 error | ||
res, err2 = v.ValidateToken(ctx, tokenString) | ||
if err2 == nil { | ||
err = nil | ||
return | ||
} | ||
err = errors.Join(err, err2) | ||
} | ||
|
||
log.Debugfc(ctx, "auth: invalid JWT token: %s", tokenString) | ||
log.Errorfc(ctx, "auth: invalid JWT token: %v", err) | ||
return | ||
} |
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,124 @@ | ||
package appx | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/auth0/go-jwt-middleware/v2/validator" | ||
"github.com/golang-jwt/jwt" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/reearth/reearthx/util" | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/square/go-jose.v2" | ||
jwt2 "gopkg.in/square/go-jose.v2/jwt" | ||
) | ||
|
||
func TestMultiValidator(t *testing.T) { | ||
key := lo.Must(rsa.GenerateKey(rand.Reader, 2048)) | ||
|
||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
httpmock.RegisterResponder( | ||
http.MethodGet, | ||
"https://example.com/.well-known/openid-configuration", | ||
util.DR(httpmock.NewJsonResponder(http.StatusOK, map[string]string{"jwks_uri": "https://example.com/jwks"})), | ||
) | ||
httpmock.RegisterResponder( | ||
http.MethodGet, | ||
"https://example2.com/.well-known/openid-configuration", | ||
util.DR(httpmock.NewJsonResponder(http.StatusOK, map[string]string{"jwks_uri": "https://example.com/jwks"})), | ||
) | ||
httpmock.RegisterResponder( | ||
http.MethodGet, | ||
"https://example.com/jwks", | ||
httpmock.NewBytesResponder(http.StatusOK, lo.Must(json.Marshal(jose.JSONWebKeySet{ | ||
Keys: []jose.JSONWebKey{ | ||
{KeyID: "0", Key: &key.PublicKey, Algorithm: jwt.SigningMethodRS256.Name}, | ||
}, | ||
}))), | ||
) | ||
|
||
v, err := NewJWTMultipleValidator([]JWTProvider{ | ||
{ISS: "https://example.com/", AUD: []string{"a", "b"}, ALG: &jwt.SigningMethodRS256.Name}, | ||
{ISS: "https://example2.com/", AUD: []string{"c"}, ALG: &jwt.SigningMethodRS256.Name}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
expiry := time.Now().Add(time.Hour * 24).Unix() | ||
claims := jwt.MapClaims{ | ||
"exp": expiry, | ||
"iss": "https://example.com/", | ||
"sub": "subsub", | ||
"aud": []string{"a", "b"}, | ||
"name": "aaa", | ||
"nickname": "bbb", | ||
"email": "ccc", | ||
"email_verified": true, | ||
} | ||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) | ||
token.Header["kid"] = "0" | ||
tokenString := lo.Must(token.SignedString(key)) | ||
|
||
claims2 := jwt.MapClaims{ | ||
"exp": expiry, | ||
"iss": "https://example2.com/", | ||
"sub": "subsub2", | ||
"aud": "c", | ||
"name": "aaa", | ||
"nickname": "bbb", | ||
} | ||
token2 := jwt.NewWithClaims(jwt.SigningMethodRS256, claims2) | ||
token2.Header["kid"] = "0" | ||
tokenString2 := lo.Must(token2.SignedString(key)) | ||
|
||
claims3 := jwt.MapClaims{ | ||
"exp": expiry, | ||
"iss": "https://example3.com/", | ||
"aud": "c", | ||
} | ||
token3 := jwt.NewWithClaims(jwt.SigningMethodRS256, claims3) | ||
token3.Header["kid"] = "0" | ||
tokenString3 := lo.Must(token3.SignedString(key)) | ||
|
||
res, err := v.ValidateToken(context.Background(), tokenString) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &validator.ValidatedClaims{ | ||
CustomClaims: &customClaims{ | ||
Name: "aaa", | ||
Nickname: "bbb", | ||
Email: "ccc", | ||
EmailVerified: lo.ToPtr(true), | ||
}, | ||
RegisteredClaims: validator.RegisteredClaims{ | ||
Issuer: "https://example.com/", | ||
Subject: "subsub", | ||
Audience: []string{"a", "b"}, | ||
Expiry: expiry, | ||
}, | ||
}, res) | ||
|
||
res2, err := v.ValidateToken(context.Background(), tokenString2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &validator.ValidatedClaims{ | ||
CustomClaims: &customClaims{ | ||
Name: "aaa", | ||
Nickname: "bbb", | ||
}, | ||
RegisteredClaims: validator.RegisteredClaims{ | ||
Issuer: "https://example2.com/", | ||
Subject: "subsub2", | ||
Audience: []string{"c"}, | ||
Expiry: expiry, | ||
}, | ||
}, res2) | ||
|
||
res3, err := v.ValidateToken(context.Background(), tokenString3) | ||
assert.ErrorIs(t, err, jwt2.ErrInvalidIssuer) | ||
assert.Nil(t, res3) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/reearth/reearthx | ||
|
||
go 1.19 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/99designs/gqlgen v0.17.12 | ||
|