Add user change password method

This commit is contained in:
nxshock 2019-07-25 21:56:31 +05:00
parent 33bb853438
commit 8bc553aff4
7 changed files with 64 additions and 0 deletions

View File

@ -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)

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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())
}

View File

@ -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

View File

@ -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.
}

View File

@ -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)