mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-24 19:32:20 +00:00
Make Register method uses Login method
This commit is contained in:
parent
5a9be3d516
commit
cf9fade28e
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
type Backend interface {
|
type Backend interface {
|
||||||
Register(username, password, device string) (user User, token string, err *models.ApiError)
|
Register(username, password, device string) (user User, token string, err *models.ApiError)
|
||||||
Login(username, password, device string) (token string, err *models.ApiError)
|
Login(username, password, device string) (user User, token string, err *models.ApiError)
|
||||||
GetUserByToken(token string) (user User)
|
GetUserByToken(token string) (user User)
|
||||||
GetRoomByID(id string) Room
|
GetRoomByID(id string) Room
|
||||||
Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError)
|
Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError)
|
||||||
|
@ -28,45 +28,42 @@ func NewBackend(hostname string) *Backend {
|
|||||||
|
|
||||||
func (backend *Backend) Register(username, password, device string) (user internal.User, token string, err *models.ApiError) {
|
func (backend *Backend) Register(username, password, device string) (user internal.User, token string, err *models.ApiError) {
|
||||||
backend.mutex.Lock()
|
backend.mutex.Lock()
|
||||||
defer backend.mutex.Unlock()
|
|
||||||
|
|
||||||
if _, ok := backend.data[username]; ok {
|
if _, ok := backend.data[username]; ok {
|
||||||
|
backend.mutex.Unlock()
|
||||||
return nil, "", internal.NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
|
return nil, "", internal.NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
|
||||||
}
|
}
|
||||||
|
|
||||||
token = newToken(defaultTokenSize)
|
|
||||||
|
|
||||||
user = &User{
|
user = &User{
|
||||||
name: username,
|
name: username,
|
||||||
password: password,
|
password: password,
|
||||||
Tokens: map[string]Token{
|
Tokens: make(map[string]Token),
|
||||||
token: {
|
backend: backend}
|
||||||
Device: device}},
|
|
||||||
backend: backend}
|
|
||||||
|
|
||||||
backend.data[username] = user
|
backend.data[username] = user
|
||||||
|
|
||||||
return user, token, nil
|
backend.mutex.Unlock()
|
||||||
|
return backend.Login(username, password, device)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (backend *Backend) Login(username, password, device string) (token string, err *models.ApiError) {
|
func (backend *Backend) Login(username, password, device string) (user internal.User, token string, err *models.ApiError) {
|
||||||
backend.mutex.Lock()
|
backend.mutex.Lock()
|
||||||
defer backend.mutex.Unlock()
|
defer backend.mutex.Unlock()
|
||||||
|
|
||||||
user, ok := backend.data[username]
|
user, ok := backend.data[username]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", internal.NewError(models.M_FORBIDDEN, "wrong username")
|
return nil, "", internal.NewError(models.M_FORBIDDEN, "wrong username")
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.Password() != password {
|
if user.Password() != password {
|
||||||
return "", internal.NewError(models.M_FORBIDDEN, "wrong password")
|
return nil, "", internal.NewError(models.M_FORBIDDEN, "wrong password")
|
||||||
}
|
}
|
||||||
|
|
||||||
token = newToken(defaultTokenSize)
|
token = newToken(defaultTokenSize)
|
||||||
|
|
||||||
backend.data[username].(*User).Tokens[token] = Token{Device: device}
|
backend.data[username].(*User).Tokens[token] = Token{Device: device}
|
||||||
|
|
||||||
return token, nil
|
return user, token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) {
|
func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) {
|
||||||
|
@ -49,7 +49,7 @@ func TestLogin(t *testing.T) {
|
|||||||
_, _, err := backend.Register(userName, password, "")
|
_, _, err := backend.Register(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
token, err := backend.Login(userName, password, "")
|
_, token, err := backend.Login(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotZero(t, token)
|
assert.NotZero(t, token)
|
||||||
}
|
}
|
||||||
@ -65,10 +65,10 @@ func TestLoginWithWrongCredentials(t *testing.T) {
|
|||||||
_, _, err := backend.Register(userName, password, "")
|
_, _, err := backend.Register(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
_, err = backend.Login(userName, "wrong password", "")
|
_, _, err = backend.Login(userName, "wrong password", "")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
_, err = backend.Login("wrong user name", password, "")
|
_, _, err = backend.Login("wrong user name", password, "")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ func TestLogout(t *testing.T) {
|
|||||||
user, _, err := backend.Register(userName, password, "")
|
user, _, err := backend.Register(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
token, err := backend.Login(userName, password, "")
|
_, token, err := backend.Login(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotZero(t, token)
|
assert.NotZero(t, token)
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ func TestLogoutWithWrongToken(t *testing.T) {
|
|||||||
user, _, err := backend.Register(userName, password, "")
|
user, _, err := backend.Register(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
token, err := backend.Login(userName, password, "")
|
_, token, err := backend.Login(userName, password, "")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotZero(t, token)
|
assert.NotZero(t, token)
|
||||||
|
|
||||||
@ -185,7 +185,7 @@ func TestLogoutAll(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Len(t, user.Devices(), 1)
|
assert.Len(t, user.Devices(), 1)
|
||||||
|
|
||||||
_, err = backend.Login(userName, password, "dev2")
|
_, _, err = backend.Login(userName, password, "dev2")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Len(t, user.Devices(), 2)
|
assert.Len(t, user.Devices(), 2)
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
request.Identifier.User = strings.TrimPrefix(request.Identifier.User, "@")
|
request.Identifier.User = strings.TrimPrefix(request.Identifier.User, "@")
|
||||||
}
|
}
|
||||||
|
|
||||||
token, apiErr := currServer.Backend.Login(request.Identifier.User, request.Password, request.DeviceID)
|
_, token, apiErr := currServer.Backend.Login(request.Identifier.User, request.Password, request.DeviceID)
|
||||||
if apiErr != nil {
|
if apiErr != nil {
|
||||||
errorResponse(w, *apiErr, http.StatusForbidden, "")
|
errorResponse(w, *apiErr, http.StatusForbidden, "")
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user