2021-03-31 10:40:44 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-07 07:00:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-01-13 10:15:22 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-03-31 10:40:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/contexthandler"
|
2022-07-20 07:50:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2021-03-31 10:40:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareJWTAuth(t *testing.T) {
|
2022-09-07 07:00:33 -05:00
|
|
|
const myEmail = "vladimir@example.com"
|
2021-03-31 10:40:44 -05:00
|
|
|
const id int64 = 12
|
|
|
|
const orgID int64 = 2
|
|
|
|
|
|
|
|
configure := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthEnabled = true
|
|
|
|
cfg.JWTAuthHeaderName = "x-jwt-assertion"
|
|
|
|
}
|
|
|
|
|
2022-09-09 04:05:58 -05:00
|
|
|
configureAuthHeader := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthEnabled = true
|
|
|
|
cfg.JWTAuthHeaderName = "Authorization"
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:40:44 -05:00
|
|
|
configureUsernameClaim := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthUsernameClaim = "foo-username"
|
|
|
|
}
|
|
|
|
|
|
|
|
configureEmailClaim := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthEmailClaim = "foo-email"
|
|
|
|
}
|
|
|
|
|
2022-01-13 10:15:22 -06:00
|
|
|
configureAutoSignUp := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthAutoSignUp = true
|
|
|
|
}
|
|
|
|
|
2022-09-07 07:00:33 -05:00
|
|
|
configureRole := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthEmailClaim = "sub"
|
|
|
|
cfg.JWTAuthRoleAttributePath = "role"
|
|
|
|
}
|
|
|
|
|
|
|
|
configureRoleStrict := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthRoleAttributeStrict = true
|
|
|
|
}
|
|
|
|
|
|
|
|
configureRoleAllowAdmin := func(cfg *setting.Cfg) {
|
|
|
|
cfg.JWTAuthAllowAssignGrafanaAdmin = true
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:40:44 -05:00
|
|
|
token := "some-token"
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token with valid login claim", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
myUsername := "vladimir"
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
2022-01-13 10:15:22 -06:00
|
|
|
"sub": myUsername,
|
2021-03-31 10:40:44 -05:00
|
|
|
"foo-username": myUsername,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Login: myUsername}
|
2021-03-31 10:40:44 -05:00
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
2022-08-11 06:28:55 -05:00
|
|
|
assert.Equal(t, orgID, sc.context.OrgID)
|
|
|
|
assert.Equal(t, id, sc.context.UserID)
|
2021-03-31 10:40:44 -05:00
|
|
|
assert.Equal(t, myUsername, sc.context.Login)
|
|
|
|
}, configure, configureUsernameClaim)
|
|
|
|
|
2022-09-09 04:05:58 -05:00
|
|
|
middlewareScenario(t, "Valid token with bearer in authorization header", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
myUsername := "vladimir"
|
|
|
|
// We can ignore gosec G101 since this does not contain any credentials.
|
|
|
|
// nolint:gosec
|
|
|
|
myToken := "some.jwt.token"
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = myToken
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myUsername,
|
|
|
|
"foo-username": myUsername,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Login: myUsername}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader("Bearer " + myToken).exec()
|
|
|
|
assert.Equal(t, verifiedToken, myToken)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
|
|
|
assert.Equal(t, orgID, sc.context.OrgID)
|
|
|
|
assert.Equal(t, id, sc.context.UserID)
|
|
|
|
assert.Equal(t, myUsername, sc.context.Login)
|
|
|
|
}, configureAuthHeader, configureUsernameClaim)
|
|
|
|
|
2021-03-31 10:40:44 -05:00
|
|
|
middlewareScenario(t, "Valid token with valid email claim", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
2022-01-13 10:15:22 -06:00
|
|
|
"sub": myEmail,
|
2021-03-31 10:40:44 -05:00
|
|
|
"foo-email": myEmail,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail}
|
2021-03-31 10:40:44 -05:00
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
2022-08-11 06:28:55 -05:00
|
|
|
assert.Equal(t, orgID, sc.context.OrgID)
|
|
|
|
assert.Equal(t, id, sc.context.UserID)
|
2021-03-31 10:40:44 -05:00
|
|
|
assert.Equal(t, myEmail, sc.context.Email)
|
|
|
|
}, configure, configureEmailClaim)
|
|
|
|
|
2022-01-13 10:15:22 -06:00
|
|
|
middlewareScenario(t, "Valid token with no user and auto_sign_up disabled", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"name": "Vladimir Example",
|
|
|
|
"foo-email": myEmail,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedError = user.ErrUserNotFound
|
2022-01-13 10:15:22 -06:00
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
|
|
|
assert.Equal(t, contexthandler.UserNotFound, sc.respJson["message"])
|
|
|
|
}, configure, configureEmailClaim)
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token with no user and auto_sign_up enabled", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"name": "Vladimir Example",
|
|
|
|
"foo-email": myEmail,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail}
|
2022-01-13 10:15:22 -06:00
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
2022-08-11 06:28:55 -05:00
|
|
|
assert.Equal(t, orgID, sc.context.OrgID)
|
|
|
|
assert.Equal(t, id, sc.context.UserID)
|
2022-01-13 10:15:22 -06:00
|
|
|
assert.Equal(t, myEmail, sc.context.Email)
|
|
|
|
}, configure, configureEmailClaim, configureAutoSignUp)
|
|
|
|
|
2021-03-31 10:40:44 -05:00
|
|
|
middlewareScenario(t, "Valid token without a login claim", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
2022-01-13 10:15:22 -06:00
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": "baz",
|
|
|
|
"foo": "bar",
|
|
|
|
}, nil
|
2021-03-31 10:40:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
|
|
|
assert.Equal(t, contexthandler.InvalidJWT, sc.respJson["message"])
|
|
|
|
}, configure, configureUsernameClaim)
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token without a email claim", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
2022-01-13 10:15:22 -06:00
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": "baz",
|
|
|
|
"foo": "bar",
|
|
|
|
}, nil
|
2021-03-31 10:40:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
|
|
|
assert.Equal(t, contexthandler.InvalidJWT, sc.respJson["message"])
|
|
|
|
}, configure, configureEmailClaim)
|
|
|
|
|
2022-09-07 07:00:33 -05:00
|
|
|
middlewareScenario(t, "Valid token with role", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"role": "Editor",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail, OrgRole: org.RoleEditor}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
|
|
|
assert.Equal(t, org.RoleEditor, sc.context.OrgRole)
|
|
|
|
}, configure, configureAutoSignUp, configureRole)
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token with invalid role", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"role": "test",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail, OrgRole: org.RoleViewer}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
|
|
|
assert.Equal(t, org.RoleViewer, sc.context.OrgRole)
|
|
|
|
}, configure, configureAutoSignUp, configureRole)
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token with invalid role in strict mode", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"role": "test",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail, OrgRole: org.RoleViewer}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
|
|
|
assert.Equal(t, contexthandler.InvalidRole, sc.respJson["message"])
|
|
|
|
}, configure, configureAutoSignUp, configureRole, configureRoleStrict)
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token with grafana admin role not allowed", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"role": "GrafanaAdmin",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail, OrgRole: org.RoleAdmin}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
|
|
|
assert.Equal(t, org.RoleAdmin, sc.context.OrgRole)
|
|
|
|
assert.False(t, sc.context.IsGrafanaAdmin)
|
|
|
|
}, configure, configureAutoSignUp, configureRole)
|
|
|
|
|
|
|
|
middlewareScenario(t, "Valid token with grafana admin role allowed", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return models.JWTClaims{
|
|
|
|
"sub": myEmail,
|
|
|
|
"role": "GrafanaAdmin",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id, OrgID: orgID, Email: myEmail, OrgRole: org.RoleAdmin, IsGrafanaAdmin: true}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
|
|
|
assert.Equal(t, org.RoleAdmin, sc.context.OrgRole)
|
|
|
|
assert.True(t, sc.context.IsGrafanaAdmin)
|
|
|
|
}, configure, configureAutoSignUp, configureRole, configureRoleAllowAdmin)
|
|
|
|
|
2021-03-31 10:40:44 -05:00
|
|
|
middlewareScenario(t, "Invalid token", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
var verifiedToken string
|
|
|
|
sc.jwtAuthService.VerifyProvider = func(ctx context.Context, token string) (models.JWTClaims, error) {
|
|
|
|
verifiedToken = token
|
|
|
|
return nil, errors.New("token is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/").withJWTAuthHeader(token).exec()
|
|
|
|
assert.Equal(t, verifiedToken, token)
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
|
|
|
assert.Equal(t, contexthandler.InvalidJWT, sc.respJson["message"])
|
|
|
|
}, configure, configureUsernameClaim)
|
|
|
|
}
|