From 92f2b43f56f07f2ac340d560b362113fe671d12c Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 24 Jul 2019 19:15:07 +0500 Subject: [PATCH] Add user.LogoutAll() method --- README.md | 3 ++- internal/backend.go | 1 + internal/backends/memory/user.go | 4 ++++ internal/handlers.go | 25 +++++++++++++++++++++++++ internal/server.go | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ec680f9..de5a7ab 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,5 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [x] [5.4.1 GET /_matrix/client/r0/login](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-login) - [x] [5.4.2 POST /_matrix/client/r0/login](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login) - [x] [5.4.3 POST /_matrix/client/r0/logout](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout) -- [x] [5.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami) \ No newline at end of file +- [x] [5.4.4 POST /_matrix/client/r0/logout/all](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all) +- [x] [5.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami) diff --git a/internal/backend.go b/internal/backend.go index 0e8dbe0..57652be 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -33,4 +33,5 @@ type User interface { LeaveRoom(room Room) *models.ApiError SetTopic(room Room, topic string) *models.ApiError SendMessage(room Room, text string) *models.ApiError + LogoutAll() } diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 25dfe79..aa1bc66 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -154,3 +154,7 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError return nil } + +func (user *User) LogoutAll() { + user.Tokens = make(map[string]Token) +} diff --git a/internal/handlers.go b/internal/handlers.go index 2872685..ab7ce00 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -97,6 +97,31 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, struct{}{}) } +// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all +func LogoutAllHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + return + } + + token := getTokenFromResponse(r) + + if token == "" { + errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "") + return + } + + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + user.LogoutAll() + + sendJsonResponse(w, http.StatusOK, struct{}{}) +} + // https://models.org/docs/spec/client_server/latest#post-models-client-r0-register func RegisterHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { diff --git a/internal/server.go b/internal/server.go index 3f208de..1b02c67 100644 --- a/internal/server.go +++ b/internal/server.go @@ -21,6 +21,7 @@ func New() *Server { router.HandleFunc("/_matrix/client/versions", VersionHandler) router.HandleFunc("/_matrix/client/r0/login", LoginHandler) router.HandleFunc("/_matrix/client/r0/logout", LogoutHandler) + router.HandleFunc("/_matrix/client/r0/logout/all", LogoutAllHandler) router.HandleFunc("/_matrix/client/r0/register", RegisterHandler) router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler) router.HandleFunc("/_matrix/client/r0/sync", SyncHandler)