| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | package middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:09:32 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/login" | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2022-08-04 20:19:09 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/apikey" | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/contexthandler" | 
					
						
							| 
									
										
										
										
											2022-04-05 02:36:15 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/login/logintest" | 
					
						
							| 
									
										
										
										
											2022-06-28 20:32:25 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/user" | 
					
						
							| 
									
										
										
										
											2022-08-04 19:22:43 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/user/usertest" | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMiddlewareBasicAuth(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 	const id int64 = 12 | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	configure := func(cfg *setting.Cfg) { | 
					
						
							|  |  |  | 		cfg.BasicAuthEnabled = true | 
					
						
							|  |  |  | 		cfg.DisableBruteForceLoginProtection = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:09:32 +08:00
										 |  |  | 	middlewareScenario(t, "Valid API key", func(t *testing.T, sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		const orgID int64 = 2 | 
					
						
							|  |  |  | 		keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-04 20:19:09 +08:00
										 |  |  | 		sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: orgID, Role: models.ROLE_EDITOR, Key: keyhash} | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08: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) | 
					
						
							|  |  |  | 		assert.Equal(t, orgID, sc.context.OrgId) | 
					
						
							|  |  |  | 		assert.Equal(t, models.ROLE_EDITOR, sc.context.OrgRole) | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	}, configure) | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:09:32 +08:00
										 |  |  | 	middlewareScenario(t, "Handle auth", func(t *testing.T, sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		const password = "MyPass" | 
					
						
							|  |  |  | 		const orgID int64 = 2 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 22:31:26 +08:00
										 |  |  | 		sc.mockSQLStore.ExpectedSignedInUser = &models.SignedInUser{OrgId: orgID, UserId: id} | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		authHeader := util.GetBasicAuthHeader("myUser", password) | 
					
						
							|  |  |  | 		sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec() | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		assert.True(t, sc.context.IsSignedIn) | 
					
						
							|  |  |  | 		assert.Equal(t, orgID, sc.context.OrgId) | 
					
						
							|  |  |  | 		assert.Equal(t, id, sc.context.UserId) | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	}, configure) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:09:32 +08:00
										 |  |  | 	middlewareScenario(t, "Auth sequence", func(t *testing.T, sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		const password = "MyPass" | 
					
						
							|  |  |  | 		const salt = "Salt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 14:45:01 +08:00
										 |  |  | 		encoded, err := util.EncodePassword(password, salt) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-28 20:32:25 +08:00
										 |  |  | 		sc.mockSQLStore.ExpectedUser = &user.User{Password: encoded, ID: id, Salt: salt} | 
					
						
							| 
									
										
										
										
											2022-04-06 14:45:01 +08:00
										 |  |  | 		sc.mockSQLStore.ExpectedSignedInUser = &models.SignedInUser{UserId: id} | 
					
						
							| 
									
										
										
										
											2022-08-04 19:22:43 +08:00
										 |  |  | 		login.ProvideService(sc.mockSQLStore, &logintest.LoginServiceFake{}, usertest.NewUserServiceFake()) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		authHeader := util.GetBasicAuthHeader("myUser", password) | 
					
						
							|  |  |  | 		sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec() | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		require.NotNil(t, sc.context) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08:00
										 |  |  | 		assert.True(t, sc.context.IsSignedIn) | 
					
						
							|  |  |  | 		assert.Equal(t, id, sc.context.UserId) | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	}, configure) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:09:32 +08:00
										 |  |  | 	middlewareScenario(t, "Should return error if user is not found", func(t *testing.T, sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-07-20 20:50:06 +08:00
										 |  |  | 		sc.mockSQLStore.ExpectedError = user.ErrUserNotFound | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08: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 18:44:44 +08:00
										 |  |  | 		assert.Equal(t, contexthandler.InvalidUsernamePassword, sc.respJson["message"]) | 
					
						
							|  |  |  | 	}, configure) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:09:32 +08:00
										 |  |  | 	middlewareScenario(t, "Should return error if user & password do not match", func(t *testing.T, sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-07-20 20:50:06 +08:00
										 |  |  | 		sc.mockSQLStore.ExpectedError = user.ErrUserNotFound | 
					
						
							| 
									
										
										
										
											2020-12-03 15:28:54 +08: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 18:44:44 +08:00
										 |  |  | 		assert.Equal(t, contexthandler.InvalidUsernamePassword, sc.respJson["message"]) | 
					
						
							|  |  |  | 	}, configure) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:50:05 +08:00
										 |  |  | } |