Rename auth.Database methods
This commit is contained in:
parent
8051f159f1
commit
fadf989158
|
|
@ -14,10 +14,10 @@ const (
|
||||||
|
|
||||||
// The Database interface for encapsulating database access.
|
// The Database interface for encapsulating database access.
|
||||||
type Database interface {
|
type Database interface {
|
||||||
GetApplicationById(id string) *model.Application
|
GetApplicationByID(id string) *model.Application
|
||||||
GetClientById(id string) *model.Client
|
GetClientByID(id string) *model.Client
|
||||||
GetUserByName(name string) *model.User
|
GetUserByName(name string) *model.User
|
||||||
GetUserById(id uint) *model.User
|
GetUserByID(id uint) *model.User
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth is the provider for authentication middleware
|
// Auth is the provider for authentication middleware
|
||||||
|
|
@ -25,17 +25,17 @@ type Auth struct {
|
||||||
DB Database
|
DB Database
|
||||||
}
|
}
|
||||||
|
|
||||||
type authenticate func(tokenId string, user *model.User) (success bool, userId uint)
|
type authenticate func(tokenID string, user *model.User) (success bool, userId uint)
|
||||||
|
|
||||||
// RequireAdmin returns a gin middleware which requires a client token or basic authentication header to be supplied
|
// RequireAdmin returns a gin middleware which requires a client token or basic authentication header to be supplied
|
||||||
// with the request. Also the authenticated user must be an administrator.
|
// with the request. Also the authenticated user must be an administrator.
|
||||||
func (a *Auth) RequireAdmin() gin.HandlerFunc {
|
func (a *Auth) RequireAdmin() gin.HandlerFunc {
|
||||||
return a.requireToken(func(tokenId string, user *model.User) (bool, uint) {
|
return a.requireToken(func(tokenID string, user *model.User) (bool, uint) {
|
||||||
if user != nil {
|
if user != nil {
|
||||||
return user.Admin, user.ID
|
return user.Admin, user.ID
|
||||||
}
|
}
|
||||||
if token := a.DB.GetClientById(tokenId); token != nil {
|
if token := a.DB.GetClientByID(tokenID); token != nil {
|
||||||
return a.DB.GetUserById(token.UserID).Admin, token.UserID
|
return a.DB.GetUserByID(token.UserID).Admin, token.UserID
|
||||||
}
|
}
|
||||||
return false, 0
|
return false, 0
|
||||||
})
|
})
|
||||||
|
|
@ -44,11 +44,11 @@ func (a *Auth) RequireAdmin() gin.HandlerFunc {
|
||||||
// RequireClient returns a gin middleware which requires a client token or basic authentication header to be supplied
|
// RequireClient returns a gin middleware which requires a client token or basic authentication header to be supplied
|
||||||
// with the request.
|
// with the request.
|
||||||
func (a *Auth) RequireClient() gin.HandlerFunc {
|
func (a *Auth) RequireClient() gin.HandlerFunc {
|
||||||
return a.requireToken(func(tokenId string, user *model.User) (bool, uint) {
|
return a.requireToken(func(tokenID string, user *model.User) (bool, uint) {
|
||||||
if user != nil {
|
if user != nil {
|
||||||
return true, user.ID
|
return true, user.ID
|
||||||
}
|
}
|
||||||
if token := a.DB.GetClientById(tokenId); token != nil {
|
if token := a.DB.GetClientByID(tokenID); token != nil {
|
||||||
return true, token.UserID
|
return true, token.UserID
|
||||||
}
|
}
|
||||||
return false, 0
|
return false, 0
|
||||||
|
|
@ -57,11 +57,11 @@ func (a *Auth) RequireClient() gin.HandlerFunc {
|
||||||
|
|
||||||
// RequireApplicationToken returns a gin middleware which requires an application token to be supplied with the request.
|
// RequireApplicationToken returns a gin middleware which requires an application token to be supplied with the request.
|
||||||
func (a *Auth) RequireApplicationToken() gin.HandlerFunc {
|
func (a *Auth) RequireApplicationToken() gin.HandlerFunc {
|
||||||
return a.requireToken(func(tokenId string, user *model.User) (bool, uint) {
|
return a.requireToken(func(tokenID string, user *model.User) (bool, uint) {
|
||||||
if user != nil {
|
if user != nil {
|
||||||
return false, 0
|
return false, 0
|
||||||
}
|
}
|
||||||
if token := a.DB.GetApplicationById(tokenId); token != nil {
|
if token := a.DB.GetApplicationByID(tokenID); token != nil {
|
||||||
return true, token.UserID
|
return true, token.UserID
|
||||||
}
|
}
|
||||||
return false, 0
|
return false, 0
|
||||||
|
|
|
||||||
|
|
@ -26,15 +26,15 @@ func (s *AuthenticationSuite) SetupSuite() {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
s.DB = &authmock.MockDatabase{}
|
s.DB = &authmock.MockDatabase{}
|
||||||
s.auth = &Auth{s.DB}
|
s.auth = &Auth{s.DB}
|
||||||
s.DB.On("GetClientById", "clienttoken").Return(&model.Client{ID: "clienttoken", UserID: 1, Name: "android phone"})
|
s.DB.On("GetClientByID", "clienttoken").Return(&model.Client{ID: "clienttoken", UserID: 1, Name: "android phone"})
|
||||||
s.DB.On("GetClientById", "clienttoken_admin").Return(&model.Client{ID: "clienttoken", UserID: 2, Name: "android phone2"})
|
s.DB.On("GetClientByID", "clienttoken_admin").Return(&model.Client{ID: "clienttoken", UserID: 2, Name: "android phone2"})
|
||||||
s.DB.On("GetClientById", mock.Anything).Return(nil)
|
s.DB.On("GetClientByID", mock.Anything).Return(nil)
|
||||||
s.DB.On("GetApplicationById", "apptoken").Return(&model.Application{ID: "apptoken", UserID: 1, Name: "backup server", Description: "irrelevant"})
|
s.DB.On("GetApplicationByID", "apptoken").Return(&model.Application{ID: "apptoken", UserID: 1, Name: "backup server", Description: "irrelevant"})
|
||||||
s.DB.On("GetApplicationById", "apptoken_admin").Return(&model.Application{ID: "apptoken", UserID: 2, Name: "backup server", Description: "irrelevant"})
|
s.DB.On("GetApplicationByID", "apptoken_admin").Return(&model.Application{ID: "apptoken", UserID: 2, Name: "backup server", Description: "irrelevant"})
|
||||||
s.DB.On("GetApplicationById", mock.Anything).Return(nil)
|
s.DB.On("GetApplicationByID", mock.Anything).Return(nil)
|
||||||
|
|
||||||
s.DB.On("GetUserById", uint(1)).Return(&model.User{ID: 1, Name: "irrelevant", Admin: false})
|
s.DB.On("GetUserByID", uint(1)).Return(&model.User{ID: 1, Name: "irrelevant", Admin: false})
|
||||||
s.DB.On("GetUserById", uint(2)).Return(&model.User{ID: 2, Name: "irrelevant", Admin: true})
|
s.DB.On("GetUserByID", uint(2)).Return(&model.User{ID: 2, Name: "irrelevant", Admin: true})
|
||||||
|
|
||||||
s.DB.On("GetUserByName", "existing").Return(&model.User{Name: "existing", Pass: CreatePassword("pw")})
|
s.DB.On("GetUserByName", "existing").Return(&model.User{Name: "existing", Pass: CreatePassword("pw")})
|
||||||
s.DB.On("GetUserByName", "admin").Return(&model.User{Name: "admin", Pass: CreatePassword("pw"), Admin: true})
|
s.DB.On("GetUserByName", "admin").Return(&model.User{Name: "admin", Pass: CreatePassword("pw"), Admin: true})
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ type MockDatabase struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetApplicationById provides a mock function with given fields: id
|
// GetApplicationById provides a mock function with given fields: id
|
||||||
func (_m *MockDatabase) GetApplicationById(id string) *model.Application {
|
func (_m *MockDatabase) GetApplicationByID(id string) *model.Application {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 *model.Application
|
var r0 *model.Application
|
||||||
|
|
@ -26,7 +26,7 @@ func (_m *MockDatabase) GetApplicationById(id string) *model.Application {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetClientById provides a mock function with given fields: id
|
// GetClientById provides a mock function with given fields: id
|
||||||
func (_m *MockDatabase) GetClientById(id string) *model.Client {
|
func (_m *MockDatabase) GetClientByID(id string) *model.Client {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 *model.Client
|
var r0 *model.Client
|
||||||
|
|
@ -42,7 +42,7 @@ func (_m *MockDatabase) GetClientById(id string) *model.Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserById provides a mock function with given fields: id
|
// GetUserById provides a mock function with given fields: id
|
||||||
func (_m *MockDatabase) GetUserById(id uint) *model.User {
|
func (_m *MockDatabase) GetUserByID(id uint) *model.User {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 *model.User
|
var r0 *model.User
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ func (s *UtilSuite) BeforeTest(suiteName, testName string) {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UtilSuite) Test_getId() {
|
func (s *UtilSuite) Test_getID() {
|
||||||
s.expectUserIDWith(&model.User{ID: 2}, 0, 2)
|
s.expectUserIDWith(&model.User{ID: 2}, 0, 2)
|
||||||
s.expectUserIDWith(nil, 5, 5)
|
s.expectUserIDWith(nil, 5, 5)
|
||||||
assert.Panics(s.T(), func() {
|
assert.Panics(s.T(), func() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue