mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-05 02:01:03 +00:00
Move Logout() to user method
This commit is contained in:
parent
92f2b43f56
commit
f0d2ea7f17
@ -10,7 +10,6 @@ import (
|
||||
type Backend interface {
|
||||
Register(username, password, device string) (user User, token string, err *models.ApiError)
|
||||
Login(username, password, device string) (token string, err *models.ApiError)
|
||||
Logout(token string) *models.ApiError
|
||||
GetUserByToken(token string) (user User)
|
||||
Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError)
|
||||
}
|
||||
@ -33,5 +32,6 @@ type User interface {
|
||||
LeaveRoom(room Room) *models.ApiError
|
||||
SetTopic(room Room, topic string) *models.ApiError
|
||||
SendMessage(room Room, text string) *models.ApiError
|
||||
Logout(token string)
|
||||
LogoutAll()
|
||||
}
|
||||
|
@ -69,22 +69,6 @@ func (backend *Backend) Login(username, password, device string) (token string,
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (backend *Backend) Logout(token string) *models.ApiError {
|
||||
backend.mutex.Lock()
|
||||
defer backend.mutex.Unlock()
|
||||
|
||||
for _, user := range backend.data {
|
||||
for userToken, _ := range user.(*User).Tokens {
|
||||
if userToken == token {
|
||||
delete(user.(*User).Tokens, token)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return internal.NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct
|
||||
}
|
||||
|
||||
func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) {
|
||||
backend.mutex.Lock()
|
||||
defer backend.mutex.Unlock()
|
||||
|
@ -78,15 +78,14 @@ func TestLogout(t *testing.T) {
|
||||
password = "password1"
|
||||
)
|
||||
|
||||
_, _, err := backend.Register(userName, password, "")
|
||||
user, _, err := backend.Register(userName, password, "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
token, err := backend.Login(userName, password, "")
|
||||
assert.Nil(t, err)
|
||||
assert.NotZero(t, token)
|
||||
|
||||
err = backend.Logout(token)
|
||||
assert.Nil(t, err)
|
||||
user.Logout(token)
|
||||
}
|
||||
|
||||
func TestLogoutWithWrongToken(t *testing.T) {
|
||||
@ -97,13 +96,12 @@ func TestLogoutWithWrongToken(t *testing.T) {
|
||||
password = "password1"
|
||||
)
|
||||
|
||||
_, _, err := backend.Register(userName, password, "")
|
||||
user, _, err := backend.Register(userName, password, "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
token, err := backend.Login(userName, password, "")
|
||||
assert.Nil(t, err)
|
||||
assert.NotZero(t, token)
|
||||
|
||||
err = backend.Logout("worng token")
|
||||
assert.NotNil(t, err)
|
||||
user.Logout("worng token")
|
||||
}
|
||||
|
@ -155,6 +155,10 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError
|
||||
return nil
|
||||
}
|
||||
|
||||
func (user *User) Logout(token string) {
|
||||
delete(user.Tokens, token)
|
||||
}
|
||||
|
||||
func (user *User) LogoutAll() {
|
||||
user.Tokens = make(map[string]Token)
|
||||
}
|
||||
|
@ -88,12 +88,14 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
apiErr := currServer.Backend.Logout(token)
|
||||
if apiErr != nil {
|
||||
errorResponse(w, *apiErr, http.StatusBadRequest, "") // TODO: check code
|
||||
user := currServer.Backend.GetUserByToken(token)
|
||||
if user == nil {
|
||||
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
|
||||
return
|
||||
}
|
||||
|
||||
user.Logout(token)
|
||||
|
||||
sendJsonResponse(w, http.StatusOK, struct{}{})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user