Check for valid serviceaccount JWT token before inspecting claims

parent 751a93b8
...@@ -135,8 +135,6 @@ func (j *jwtTokenAuthenticator) AuthenticateToken(token string) (user.Info, bool ...@@ -135,8 +135,6 @@ func (j *jwtTokenAuthenticator) AuthenticateToken(token string) (user.Info, bool
return key, nil return key, nil
}) })
claims, _ := parsedToken.Claims.(jwt.MapClaims)
if err != nil { if err != nil {
switch err := err.(type) { switch err := err.(type) {
case *jwt.ValidationError: case *jwt.ValidationError:
...@@ -160,6 +158,8 @@ func (j *jwtTokenAuthenticator) AuthenticateToken(token string) (user.Info, bool ...@@ -160,6 +158,8 @@ func (j *jwtTokenAuthenticator) AuthenticateToken(token string) (user.Info, bool
// If we get here, we have a token with a recognized signature // If we get here, we have a token with a recognized signature
claims, _ := parsedToken.Claims.(jwt.MapClaims)
// Make sure we issued the token // Make sure we issued the token
iss, _ := claims[IssuerClaim].(string) iss, _ := claims[IssuerClaim].(string)
if iss != Issuer { if iss != Issuer {
......
...@@ -225,6 +225,12 @@ func TestTokenGenerateAndValidate(t *testing.T) { ...@@ -225,6 +225,12 @@ func TestTokenGenerateAndValidate(t *testing.T) {
getter := serviceaccountcontroller.NewGetterFromClient(tc.Client) getter := serviceaccountcontroller.NewGetterFromClient(tc.Client)
authenticator := serviceaccount.JWTTokenAuthenticator(tc.Keys, tc.Client != nil, getter) authenticator := serviceaccount.JWTTokenAuthenticator(tc.Keys, tc.Client != nil, getter)
// An invalid, non-JWT token should always fail
if _, ok, err := authenticator.AuthenticateToken("invalid token"); err != nil || ok {
t.Errorf("%s: Expected err=nil, ok=false for non-JWT token", k)
continue
}
user, ok, err := authenticator.AuthenticateToken(token) user, ok, err := authenticator.AuthenticateToken(token)
if (err != nil) != tc.ExpectedErr { if (err != nil) != tc.ExpectedErr {
t.Errorf("%s: Expected error=%v, got %v", k, tc.ExpectedErr, err) t.Errorf("%s: Expected error=%v, got %v", k, tc.ExpectedErr, err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment