Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support token validation against multiple issuers and audiences #219

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func loadEnv(basePath *string, loadFromHome bool) (*Env, error) {
SecretsManager: viper.GetString("SECRETS_MANAGER"),
Auth: &AuthConfig{
WellKnown: viper.GetString("TOKEN_WELL_KNOWN"),
Audience: viper.GetString("TOKEN_AUDIENCE"),
Issuer: viper.GetString("TOKEN_ISSUER"),
Audience: viper.GetStringSlice("TOKEN_AUDIENCE"),
Issuer: viper.GetStringSlice("TOKEN_ISSUER"),
Middleware: viper.GetString("AUTHORIZATION_MIDDLEWARE"),
},
DlJwtConfig: &DatalayerJwtConfig{
Expand Down
4 changes: 2 additions & 2 deletions internal/conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func TestParseEnvFromFile(t *testing.T) {

g.It("should have correct auth values", func() {
g.Assert(env.Auth.WellKnown).Equal("https://example.io/jwks/.well-known/jwks.json")
g.Assert(env.Auth.Audience).Equal("https://example.io")
g.Assert(env.Auth.Issuer).Equal("https://example.io")
g.Assert(env.Auth.Audience).Equal([]string{"https://example.io"})
g.Assert(env.Auth.Issuer).Equal([]string{"https://example.io"})
g.Assert(env.Auth.Middleware).Equal("noop")

})
Expand Down
4 changes: 2 additions & 2 deletions internal/conf/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type Env struct {

type AuthConfig struct {
WellKnown string
Audience string
Issuer string
Audience []string
Issuer []string
Middleware string
}

Expand Down
4 changes: 2 additions & 2 deletions internal/web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func setupJWT(env *conf.Env, core *security.ServiceCore, skipper func(c echo.Con
// if node security is enabled
if env.Auth.Middleware == "local" {
config.NodePublicKey = core.NodeInfo.KeyPairs[0].PublicKey
config.Issuer = "node:" + core.NodeInfo.NodeId
config.Audience = "node:" + core.NodeInfo.NodeId
config.Issuer = []string{"node:" + core.NodeInfo.NodeId}
config.Audience = []string{"node:" + core.NodeInfo.NodeId}
}

return middlewares.JWTHandler(config)
Expand Down
18 changes: 14 additions & 4 deletions internal/web/middlewares/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type (

Cache *jwk.Cache
Wellknown string
Audience string
Issuer string
Audience []string
Issuer []string

// This is set if Node security is enabled
NodePublicKey *rsa.PublicKey
Expand Down Expand Up @@ -158,12 +158,22 @@ func (config *JwtConfig) ValidateToken(auth string) (*jwt.Token, error) {
audience := config.Audience
issuer := config.Issuer

checkAud := claims.VerifyAudience(audience, false)
checkAud := false
for _, aud := range audience {
if claims.VerifyAudience(aud, false) {
checkAud = true
rompetroll marked this conversation as resolved.
Show resolved Hide resolved
}
}
if !checkAud {
err = errors.New("invalid audience")
}

checkIss := claims.VerifyIssuer(issuer, false)
checkIss := false
for _, iss := range issuer {
if claims.VerifyIssuer(iss, false) {
checkIss = true
}
}
if !checkIss {
err = errors.New("invalid issuer")
}
Expand Down