2015-05-02 05:06:58 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2021-12-01 08:43:31 -06:00
|
|
|
"context"
|
2020-11-20 12:30:37 -06:00
|
|
|
"fmt"
|
2015-05-02 05:06:58 -05:00
|
|
|
"testing"
|
|
|
|
|
2020-10-23 09:34:35 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-09-02 08:15:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-12-03 01:28:54 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-20 12:30:37 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-05-02 05:06:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareAuth(t *testing.T) {
|
2020-12-03 01:28:54 -06:00
|
|
|
reqSignIn := Auth(&AuthOptions{ReqSignedIn: true})
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "ReqSignIn true and unauthenticated request", func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.m.Get("/secure", reqSignIn, sc.defaultHandler)
|
|
|
|
sc.fakeReq("GET", "/secure").exec()
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
assert.Equal(t, 302, sc.resp.Code)
|
|
|
|
})
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "ReqSignIn true and unauthenticated API request", func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.m.Get("/api/secure", reqSignIn, sc.defaultHandler)
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.fakeReq("GET", "/api/secure").exec()
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
|
|
|
})
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
t.Run("Anonymous auth enabled", func(t *testing.T) {
|
|
|
|
const orgID int64 = 1
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-11 04:44:44 -06:00
|
|
|
configure := func(cfg *setting.Cfg) {
|
|
|
|
cfg.AnonymousEnabled = true
|
|
|
|
cfg.AnonymousOrgName = "test"
|
|
|
|
}
|
2020-12-03 01:28:54 -06:00
|
|
|
|
2021-02-27 11:04:28 -06:00
|
|
|
middlewareScenario(t, "ReqSignIn true and NoAnonynmous true", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
2021-12-28 09:08:07 -06:00
|
|
|
bus.AddHandler("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
2021-02-27 11:04:28 -06:00
|
|
|
query.Result = &models.Org{Id: orgID, Name: "test"}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Get("/api/secure", ReqSignedInNoAnonymous, sc.defaultHandler)
|
|
|
|
sc.fakeReq("GET", "/api/secure").exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
|
|
|
}, configure)
|
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "ReqSignIn true and request with forceLogin in query string", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
2021-12-28 09:08:07 -06:00
|
|
|
bus.AddHandler("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
2020-12-03 01:28:54 -06:00
|
|
|
query.Result = &models.Org{Id: orgID, Name: "test"}
|
2020-10-23 09:34:35 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.m.Get("/secure", reqSignIn, sc.defaultHandler)
|
|
|
|
|
|
|
|
sc.fakeReq("GET", "/secure?forceLogin=true").exec()
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
assert.Equal(t, 302, sc.resp.Code)
|
2020-12-03 01:28:54 -06:00
|
|
|
location, ok := sc.resp.Header()["Location"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "/login", location[0])
|
2020-12-11 04:44:44 -06:00
|
|
|
}, configure)
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "ReqSignIn true and request with same org provided in query string", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
2021-01-07 04:36:13 -06:00
|
|
|
org, err := sc.sqlStore.CreateOrgWithMember(sc.cfg.AnonymousOrgName, 1)
|
|
|
|
require.NoError(t, err)
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.m.Get("/secure", reqSignIn, sc.defaultHandler)
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
sc.fakeReq("GET", fmt.Sprintf("/secure?orgId=%d", org.Id)).exec()
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-12-11 04:44:44 -06:00
|
|
|
}, configure)
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "ReqSignIn true and request with different org provided in query string", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
2021-12-28 09:08:07 -06:00
|
|
|
bus.AddHandler("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
2020-12-03 01:28:54 -06:00
|
|
|
query.Result = &models.Org{Id: orgID, Name: "test"}
|
|
|
|
return nil
|
2020-10-23 09:34:35 -05:00
|
|
|
})
|
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.m.Get("/secure", reqSignIn, sc.defaultHandler)
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.fakeReq("GET", "/secure?orgId=2").exec()
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
assert.Equal(t, 302, sc.resp.Code)
|
2020-12-03 01:28:54 -06:00
|
|
|
location, ok := sc.resp.Header()["Location"]
|
2020-12-04 04:09:32 -06:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "/login", location[0])
|
2020-12-11 04:44:44 -06:00
|
|
|
}, configure)
|
2020-12-03 01:28:54 -06:00
|
|
|
})
|
2020-10-23 09:34:35 -05:00
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Snapshot public mode disabled and unauthenticated request should return 401", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
2021-02-17 02:51:50 -06:00
|
|
|
sc.m.Get("/api/snapshot", func(c *models.ReqContext) {
|
|
|
|
c.IsSignedIn = false
|
|
|
|
}, SnapshotPublicModeOrSignedIn(sc.cfg), sc.defaultHandler)
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.fakeReq("GET", "/api/snapshot").exec()
|
2020-12-04 04:09:32 -06:00
|
|
|
assert.Equal(t, 401, sc.resp.Code)
|
2020-12-03 01:28:54 -06:00
|
|
|
})
|
2019-09-02 08:15:46 -05:00
|
|
|
|
2021-02-17 02:51:50 -06:00
|
|
|
middlewareScenario(t, "Snapshot public mode disabled and authenticated request should return 200", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
|
|
|
sc.m.Get("/api/snapshot", func(c *models.ReqContext) {
|
|
|
|
c.IsSignedIn = true
|
|
|
|
}, SnapshotPublicModeOrSignedIn(sc.cfg), sc.defaultHandler)
|
|
|
|
sc.fakeReq("GET", "/api/snapshot").exec()
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
})
|
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
middlewareScenario(t, "Snapshot public mode enabled and unauthenticated request should return 200", func(
|
|
|
|
t *testing.T, sc *scenarioContext) {
|
2020-12-11 04:44:44 -06:00
|
|
|
sc.cfg.SnapshotPublicMode = true
|
|
|
|
sc.m.Get("/api/snapshot", SnapshotPublicModeOrSignedIn(sc.cfg), sc.defaultHandler)
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.fakeReq("GET", "/api/snapshot").exec()
|
2020-12-04 04:09:32 -06:00
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2015-05-02 05:06:58 -05:00
|
|
|
})
|
|
|
|
}
|
2020-11-20 12:30:37 -06:00
|
|
|
|
|
|
|
func TestRemoveForceLoginparams(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
inp string
|
|
|
|
exp string
|
|
|
|
}{
|
|
|
|
{inp: "/?forceLogin=true", exp: "/?"},
|
|
|
|
{inp: "/d/dash/dash-title?ordId=1&forceLogin=true", exp: "/d/dash/dash-title?ordId=1"},
|
|
|
|
{inp: "/?kiosk&forceLogin=true", exp: "/?kiosk"},
|
|
|
|
{inp: "/d/dash/dash-title?ordId=1&kiosk&forceLogin=true", exp: "/d/dash/dash-title?ordId=1&kiosk"},
|
|
|
|
{inp: "/d/dash/dash-title?ordId=1&forceLogin=true&kiosk", exp: "/d/dash/dash-title?ordId=1&kiosk"},
|
|
|
|
{inp: "/d/dash/dash-title?forceLogin=true&kiosk", exp: "/d/dash/dash-title?&kiosk"},
|
|
|
|
}
|
|
|
|
for i, tc := range tcs {
|
|
|
|
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.exp, removeForceLoginParams(tc.inp))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|