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

Commit 232e75e

Browse files
committed
fix: auth0 setting is not used by JWT verification middleware
1 parent 7399434 commit 232e75e

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

internal/app/config.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ func (c Config) Print() string {
123123
return s
124124
}
125125

126+
func (c Config) Auths() []AuthConfig {
127+
if ac := c.Auth0.AuthConfig(); ac != nil {
128+
return append(c.Auth, *ac)
129+
}
130+
return c.Auth
131+
}
132+
133+
func (c Auth0Config) AuthConfig() *AuthConfig {
134+
domain := c.Domain
135+
if c.Domain == "" {
136+
return nil
137+
}
138+
if !strings.HasPrefix(domain, "https://") && !strings.HasPrefix(domain, "http://") {
139+
domain = "https://" + domain
140+
}
141+
if !strings.HasSuffix(domain, "/") {
142+
domain = domain + "/"
143+
}
144+
aud := []string{}
145+
if c.Audience != "" {
146+
aud = append(aud, c.Audience)
147+
}
148+
return &AuthConfig{
149+
ISS: domain,
150+
AUD: aud,
151+
}
152+
}
153+
126154
type AuthConfig struct {
127155
ISS string
128156
AUD []string
@@ -141,17 +169,6 @@ func (ipd *AuthConfigs) Decode(value string) error {
141169
return fmt.Errorf("invalid identity providers json: %w", err)
142170
}
143171

144-
for i := range providers {
145-
if providers[i].TTL == nil {
146-
providers[i].TTL = new(int)
147-
*providers[i].TTL = 5
148-
}
149-
if providers[i].ALG == nil {
150-
providers[i].ALG = new(string)
151-
*providers[i].ALG = "RS256"
152-
}
153-
}
154-
155172
*ipd = providers
156173
return nil
157174
}

internal/app/config_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package app
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestAuth0Config_AuthConfig(t *testing.T) {
10+
assert.Equal(t, &AuthConfig{
11+
ISS: "https://hoge.auth0.com/",
12+
AUD: []string{"xxx"},
13+
}, Auth0Config{
14+
Domain: "hoge.auth0.com",
15+
Audience: "xxx",
16+
}.AuthConfig())
17+
assert.Nil(t, Auth0Config{
18+
Domain: "",
19+
Audience: "xxx",
20+
}.AuthConfig())
21+
}

internal/app/jwt.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,32 @@ const (
1919
debugUserHeader = "X-Reearth-Debug-User"
2020
contextAuth0Sub contextKey = "auth0Sub"
2121
contextUser contextKey = "reearth_user"
22+
defaultJWTTTL = 5 * time.Minute
2223
)
2324

2425
type MultiValidator []*validator.Validator
2526

2627
func NewMultiValidator(providers []AuthConfig) (MultiValidator, error) {
2728
validators := make([]*validator.Validator, 0, len(providers))
2829
for _, p := range providers {
29-
3030
issuerURL, err := url.Parse(p.ISS)
3131
if err != nil {
3232
return nil, fmt.Errorf("failed to parse the issuer url: %w", err)
3333
}
3434

35-
provider := jwks.NewCachingProvider(issuerURL, time.Duration(*p.TTL)*time.Minute)
35+
var ttl time.Duration
36+
if p.TTL != nil {
37+
ttl = time.Duration(*p.TTL) * time.Minute
38+
} else {
39+
ttl = defaultJWTTTL
40+
}
41+
provider := jwks.NewCachingProvider(issuerURL, ttl)
3642

37-
algorithm := validator.SignatureAlgorithm(*p.ALG)
43+
alg := "RS256"
44+
if p.ALG != nil && *p.ALG != "" {
45+
alg = *p.ALG
46+
}
47+
algorithm := validator.SignatureAlgorithm(alg)
3848

3949
v, err := validator.New(
4050
provider.KeyFunc,
@@ -64,8 +74,7 @@ func (mv MultiValidator) ValidateToken(ctx context.Context, tokenString string)
6474

6575
// Validate the access token and inject the user clams into ctx
6676
func jwtEchoMiddleware(cfg *ServerConfig) echo.MiddlewareFunc {
67-
68-
jwtValidator, err := NewMultiValidator(cfg.Config.Auth)
77+
jwtValidator, err := NewMultiValidator(cfg.Config.Auths())
6978
if err != nil {
7079
log.Fatalf("failed to set up the validator: %v", err)
7180
}
@@ -84,7 +93,6 @@ func parseJwtMiddleware() echo.MiddlewareFunc {
8493

8594
rawClaims := ctx.Value(jwtmiddleware.ContextKey{})
8695
if claims, ok := rawClaims.(*validator.ValidatedClaims); ok {
87-
8896
// attach sub and access token to context
8997
ctx = context.WithValue(ctx, contextAuth0Sub, claims.RegisteredClaims.Subject)
9098
}

0 commit comments

Comments
 (0)