Add user.LogoutAll() method

This commit is contained in:
nxshock 2019-07-24 19:15:07 +05:00
parent b7cb0db6c5
commit 92f2b43f56
5 changed files with 33 additions and 1 deletions

View File

@ -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.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.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.4.3 POST /_matrix/client/r0/logout](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout)
- [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) - [x] [5.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami)

View File

@ -33,4 +33,5 @@ type User interface {
LeaveRoom(room Room) *models.ApiError LeaveRoom(room Room) *models.ApiError
SetTopic(room Room, topic string) *models.ApiError SetTopic(room Room, topic string) *models.ApiError
SendMessage(room Room, text string) *models.ApiError SendMessage(room Room, text string) *models.ApiError
LogoutAll()
} }

View File

@ -154,3 +154,7 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError
return nil return nil
} }
func (user *User) LogoutAll() {
user.Tokens = make(map[string]Token)
}

View File

@ -97,6 +97,31 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
sendJsonResponse(w, http.StatusOK, struct{}{}) 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 // https://models.org/docs/spec/client_server/latest#post-models-client-r0-register
func RegisterHandler(w http.ResponseWriter, r *http.Request) { func RegisterHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {

View File

@ -21,6 +21,7 @@ func New() *Server {
router.HandleFunc("/_matrix/client/versions", VersionHandler) router.HandleFunc("/_matrix/client/versions", VersionHandler)
router.HandleFunc("/_matrix/client/r0/login", LoginHandler) router.HandleFunc("/_matrix/client/r0/login", LoginHandler)
router.HandleFunc("/_matrix/client/r0/logout", LogoutHandler) 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/register", RegisterHandler)
router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler) router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler)
router.HandleFunc("/_matrix/client/r0/sync", SyncHandler) router.HandleFunc("/_matrix/client/r0/sync", SyncHandler)