From 8bc553aff4de13599aed45db89e4d63acde634fa Mon Sep 17 00:00:00 2001 From: nxshock Date: Thu, 25 Jul 2019 21:56:31 +0500 Subject: [PATCH] Add user change password method --- README.md | 1 + internal/backend.go | 1 + internal/backends/memory/user.go | 10 ++++++++++ internal/backends/memory/user_test.go | 12 ++++++++++++ internal/handlers.go | 27 +++++++++++++++++++++++++++ internal/models/password/password.go | 12 ++++++++++++ internal/server.go | 1 + 7 files changed, 64 insertions(+) create mode 100644 internal/models/password/password.go diff --git a/README.md b/README.md index 84a90c7..6d0c8bd 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [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.4 POST /_matrix/client/r0/logout/all](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all) +- [x] [5.5.4 POST /_matrix/client/r0/account/password](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-account-password) - [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] [6.1 GET /_matrix/client/r0/capabilities](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities) - [x] [10.4.1 GET /_matrix/client/r0/joined_rooms](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms) diff --git a/internal/backend.go b/internal/backend.go index 5dbd64f..49393ea 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -33,6 +33,7 @@ type User interface { SetTopic(room Room, topic string) *models.ApiError SendMessage(room Room, text string) *models.ApiError JoinedRooms() []Room + ChangePassword(newPassword string) Logout(token string) LogoutAll() } diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index b220d8d..a8938c7 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -1,6 +1,7 @@ package memory import ( + "sync" "time" "github.com/nxshock/signaller/internal" @@ -15,6 +16,8 @@ type User struct { Tokens map[string]Token backend *Backend + + mutex sync.RWMutex } func (user *User) ID() string { @@ -173,6 +176,13 @@ func (user *User) JoinedRooms() []internal.Room { return result } +func (user *User) ChangePassword(newPassword string) { + user.mutex.Lock() + defer user.mutex.Unlock() + + user.password = newPassword +} + func (user *User) Logout(token string) { delete(user.Tokens, token) } diff --git a/internal/backends/memory/user_test.go b/internal/backends/memory/user_test.go index 820269e..a25fb62 100644 --- a/internal/backends/memory/user_test.go +++ b/internal/backends/memory/user_test.go @@ -118,3 +118,15 @@ func TestJoinedRooms(t *testing.T) { rooms := user.JoinedRooms() assert.Equal(t, []internal.Room{room}, rooms) } + +func TestNewPassword(t *testing.T) { + backend := NewBackend("localhost") + + var newPassword = "new password" + + user, _, err := backend.Register("user1", "old password", "") + assert.Nil(t, err) + + user.ChangePassword(newPassword) + assert.Equal(t, newPassword, user.Password()) +} diff --git a/internal/handlers.go b/internal/handlers.go index d0a487f..855bc16 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -14,6 +14,7 @@ import ( "github.com/nxshock/signaller/internal/models/capabilities" "github.com/nxshock/signaller/internal/models/joinedrooms" login "github.com/nxshock/signaller/internal/models/login" + "github.com/nxshock/signaller/internal/models/password" register "github.com/nxshock/signaller/internal/models/register" mSync "github.com/nxshock/signaller/internal/models/sync" "github.com/nxshock/signaller/internal/models/versions" @@ -198,6 +199,32 @@ func JoinedRoomsHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } +// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-account-password +func PasswordHandler(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_FORBIDDEN, http.StatusForbidden, "") + } + + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + var request password.Request + getRequest(r, &request) // TODO: handle error + + user.ChangePassword(request.NewPassword) + + sendJsonResponse(w, http.StatusOK, struct{}{}) +} + // https://models.org/docs/spec/client_server/latest#get-models-client-r0-sync func SyncHandler(w http.ResponseWriter, r *http.Request) { var request mSync.SyncRequest diff --git a/internal/models/password/password.go b/internal/models/password/password.go new file mode 100644 index 0000000..f76b3c2 --- /dev/null +++ b/internal/models/password/password.go @@ -0,0 +1,12 @@ +package password + +// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-account-password +type Request struct { + NewPassword string `json:"new_password"` // Required. The new password for the account. + Auth AuthenticationData `json:"auth,omitempty"` // Additional authentication information for the user-interactive authentication API. +} + +type AuthenticationData struct { + Type string `json:"type"` // Required. The login type that the client is attempting to complete. + Session string `json:"session"` //The value of the session key given by the homeserver. +} diff --git a/internal/server.go b/internal/server.go index a5d2379..1a9551d 100644 --- a/internal/server.go +++ b/internal/server.go @@ -29,6 +29,7 @@ func New() *Server { router.HandleFunc("/_matrix/client/r0/register", RegisterHandler) router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler) router.HandleFunc("/_matrix/client/r0/joined_rooms", JoinedRoomsHandler) + router.HandleFunc("/_matrix/client/r0/account/password", PasswordHandler) router.HandleFunc("/_matrix/client/r0/sync", SyncHandler) router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler) router.HandleFunc("/", RootHandler)