Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: auth audiences were unintentionally required
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Apr 20, 2022
1 parent 9851004 commit 7ec76aa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,13 @@ func (c Auth0Config) AuthConfig() *AuthConfig {
return nil
}
domain := prepareUrl(c.Domain)
var aud []string
if len(c.Audience) > 0 {
aud = []string{c.Audience}
}
return &AuthConfig{
ISS: domain,
AUD: []string{c.Audience},
AUD: aud,
ClientID: &c.ClientID,
}
}
Expand Down
34 changes: 32 additions & 2 deletions internal/app/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"testing"

"github.com/reearth/reearth-backend/pkg/auth"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,13 +24,42 @@ func TestAuth0Config_AuthConfig(t *testing.T) {
}

func TestReadConfig(t *testing.T) {
clientID := auth.ClientID
localAuth := AuthConfig{
ISS: "http://localhost:8080",
AUD: []string{"http://localhost:8080"},
ClientID: &clientID,
}

cfg, err := ReadConfig(false)
assert.NoError(t, err)
assert.Nil(t, cfg.Auth)
assert.Equal(t, []AuthConfig{localAuth}, cfg.Auths())

t.Setenv("REEARTH_AUTH", `[{"iss":"bar"}]`)
t.Setenv("REEARTH_AUTH_ISS", "hoge")
t.Setenv("REEARTH_AUTH_AUD", "foo")
cfg, err := ReadConfig(false)
cfg, err = ReadConfig(false)
assert.NoError(t, err)
assert.Equal(t, AuthConfigs([]AuthConfig{{ISS: "bar"}}), cfg.Auth)
assert.Equal(t, []AuthConfig{
{ISS: "hoge"}, // REEARTH_AUTH_*
localAuth, // local auth srv
{ISS: "bar"}, // REEARTH_AUTH
}, cfg.Auths())
assert.Equal(t, "hoge", cfg.Auth_ISS)
assert.Equal(t, "", cfg.Auth_AUD)

t.Setenv("REEARTH_AUTH_AUD", "foo")
t.Setenv("REEARTH_AUTH0_DOMAIN", "foo")
t.Setenv("REEARTH_AUTH0_CLIENTID", clientID)
cfg, err = ReadConfig(false)
assert.NoError(t, err)
assert.Equal(t, []AuthConfig{
{ISS: "https://foo", ClientID: &clientID}, // Auth0
{ISS: "hoge", AUD: []string{"foo"}}, // REEARTH_AUTH_*
localAuth, // local auth srv
{ISS: "bar"}, // REEARTH_AUTH
}, cfg.Auths())
assert.Equal(t, "foo", cfg.Auth_AUD)
}

Expand Down
9 changes: 8 additions & 1 deletion internal/app/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ func NewMultiValidator(providers []AuthConfig) (MultiValidator, error) {
}
algorithm := validator.SignatureAlgorithm(alg)

var aud []string
if p.AUD != nil {
aud = p.AUD
} else {
aud = []string{}
}

v, err := validator.New(
provider.KeyFunc,
algorithm,
issuerURL.String(),
p.AUD,
aud,
validator.WithCustomClaims(func() validator.CustomClaims {
return &customClaims{}
}),
Expand Down

0 comments on commit 7ec76aa

Please sign in to comment.