2019-08-03 14:50:05 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/login"
|
2022-08-04 07:19:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/apikey"
|
2020-12-11 04:44:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/contexthandler"
|
2022-04-04 13:36:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/login/logintest"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2019-08-03 14:50:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2020-12-03 01:28:54 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-03 14:50:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareBasicAuth(t *testing.T) {
|
2020-12-03 01:28:54 -06:00
|
|
|
const id int64 = 12
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-11 04:44:44 -06:00
|
|
|
configure := func(cfg *setting.Cfg) {
|
|
|
|
cfg.BasicAuthEnabled = true
|
|
|
|
cfg.DisableBruteForceLoginProtection = true
|
|
|
|
}
|
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Valid API key", func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
const orgID int64 = 2
|
|
|
|
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
|
|
|
require.NoError(t, err)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: orgID, Role: org.RoleEditor, Key: keyhash}
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
authHeader := util.GetBasicAuthHeader("api_key", "eyJrIjoidjVuQXdwTWFmRlA2em5hUzR1cmhkV0RMUzU1MTFNNDIiLCJuIjoiYXNkIiwiaWQiOjF9")
|
|
|
|
sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec()
|
|
|
|
|
|
|
|
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)
|
2022-08-10 04:56:48 -05:00
|
|
|
assert.Equal(t, org.RoleEditor, sc.context.OrgRole)
|
2020-12-11 04:44:44 -06:00
|
|
|
}, configure)
|
2020-12-03 01:28:54 -06:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Handle auth", func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
const password = "MyPass"
|
|
|
|
const orgID int64 = 2
|
|
|
|
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{OrgID: orgID, UserID: id}
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
authHeader := util.GetBasicAuthHeader("myUser", password)
|
|
|
|
sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec()
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
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)
|
2020-12-11 04:44:44 -06:00
|
|
|
}, configure)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Auth sequence", func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
const password = "MyPass"
|
|
|
|
const salt = "Salt"
|
|
|
|
|
2022-04-06 01:45:01 -05:00
|
|
|
encoded, err := util.EncodePassword(password, salt)
|
|
|
|
require.NoError(t, err)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedUser = &user.User{Password: encoded, ID: id, Salt: salt}
|
|
|
|
sc.userService.ExpectedSignedInUser = &user.SignedInUser{UserID: id}
|
2022-09-01 11:08:42 -05:00
|
|
|
login.ProvideService(sc.mockSQLStore, &logintest.LoginServiceFake{}, nil, sc.userService)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
authHeader := util.GetBasicAuthHeader("myUser", password)
|
|
|
|
sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec()
|
2020-12-11 04:44:44 -06:00
|
|
|
require.NotNil(t, sc.context)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
assert.True(t, sc.context.IsSignedIn)
|
2022-08-11 06:28:55 -05:00
|
|
|
assert.Equal(t, id, sc.context.UserID)
|
2020-12-11 04:44:44 -06:00
|
|
|
}, configure)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Should return error if user is not found", func(t *testing.T, sc *scenarioContext) {
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedError = user.ErrUserNotFound
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.fakeReq("GET", "/")
|
|
|
|
sc.req.SetBasicAuth("user", "password")
|
|
|
|
sc.exec()
|
|
|
|
|
|
|
|
err := json.NewDecoder(sc.resp.Body).Decode(&sc.respJson)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
2020-12-11 04:44:44 -06:00
|
|
|
assert.Equal(t, contexthandler.InvalidUsernamePassword, sc.respJson["message"])
|
|
|
|
}, configure)
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Should return error if user & password do not match", func(t *testing.T, sc *scenarioContext) {
|
2022-08-16 09:08:59 -05:00
|
|
|
sc.userService.ExpectedError = user.ErrUserNotFound
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.fakeReq("GET", "/")
|
|
|
|
sc.req.SetBasicAuth("killa", "gorilla")
|
|
|
|
sc.exec()
|
|
|
|
|
|
|
|
err := json.NewDecoder(sc.resp.Body).Decode(&sc.respJson)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
2020-12-11 04:44:44 -06:00
|
|
|
assert.Equal(t, contexthandler.InvalidUsernamePassword, sc.respJson["message"])
|
|
|
|
}, configure)
|
2019-08-03 14:50:05 -05:00
|
|
|
}
|