Skip to content

Commit 4c7c542

Browse files
committed
Use JWT secret for JWT, operator secret for JWS
1 parent 5899d96 commit 4c7c542

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

api/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ func (a *API) extractBearerToken(w http.ResponseWriter, r *http.Request) (string
3535
}
3636

3737
func (a *API) parseJWTClaims(bearer string, r *http.Request) (context.Context, error) {
38+
config := getConfig(r.Context())
3839
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
3940
token, err := p.ParseWithClaims(bearer, &GatewayClaims{}, func(token *jwt.Token) (interface{}, error) {
40-
return []byte(a.config.OperatorToken), nil
41+
return []byte(config.JWT.Secret), nil
4142
})
4243
if err != nil {
4344
return nil, unauthorizedError("Invalid token: %v", err)

api/middleware.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66

77
"github.com/dgrijalva/jwt-go"
8-
"github.com/netlify/git-gateway/conf"
98
"github.com/netlify/git-gateway/models"
109
)
1110

@@ -31,41 +30,39 @@ func (a *API) loadJWSSignatureHeader(w http.ResponseWriter, r *http.Request) (co
3130

3231
func (a *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (context.Context, error) {
3332
ctx := r.Context()
33+
3434
signature := getSignature(ctx)
3535
if signature == "" {
3636
return nil, badRequestError("Operator signature missing")
3737
}
3838

39-
var config *conf.Configuration
40-
var instanceID string
41-
4239
claims := NetlifyMicroserviceClaims{}
4340
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
4441
_, err := p.ParseWithClaims(signature, &claims, func(token *jwt.Token) (interface{}, error) {
45-
instanceID = claims.InstanceID
46-
if instanceID == "" {
47-
return nil, badRequestError("Instance ID is missing")
48-
}
42+
return []byte(a.config.OperatorToken), nil
43+
})
44+
if err != nil {
45+
return nil, badRequestError("Operator microservice signature is invalid: %v", err)
46+
}
4947

50-
logEntrySetField(r, "instance_id", instanceID)
51-
logEntrySetField(r, "netlify_id", claims.NetlifyID)
52-
instance, err := a.db.GetInstance(instanceID)
53-
if err != nil {
54-
if models.IsNotFoundError(err) {
55-
return nil, notFoundError("Unable to locate site configuration")
56-
}
57-
return nil, internalServerError("Database error loading instance").WithInternalError(err)
58-
}
48+
instanceID := claims.InstanceID
49+
if instanceID == "" {
50+
return nil, badRequestError("Instance ID is missing")
51+
}
5952

60-
config, err = instance.Config()
61-
if err != nil {
62-
return nil, internalServerError("Error loading environment config").WithInternalError(err)
53+
logEntrySetField(r, "instance_id", instanceID)
54+
logEntrySetField(r, "netlify_id", claims.NetlifyID)
55+
instance, err := a.db.GetInstance(instanceID)
56+
if err != nil {
57+
if models.IsNotFoundError(err) {
58+
return nil, notFoundError("Unable to locate site configuration")
6359
}
60+
return nil, internalServerError("Database error loading instance").WithInternalError(err)
61+
}
6462

65-
return []byte(config.JWT.Secret), nil
66-
})
63+
config, err := instance.Config()
6764
if err != nil {
68-
return nil, badRequestError("Operator microservice signature is invalid: %v", err)
65+
return nil, internalServerError("Error loading environment config").WithInternalError(err)
6966
}
7067

7168
ctx = withNetlifyID(ctx, claims.NetlifyID)

0 commit comments

Comments
 (0)