From 11097f763c43a7fefccae575622047a357226714 Mon Sep 17 00:00:00 2001 From: nxshock Date: Fri, 2 Aug 2019 18:17:18 +0500 Subject: [PATCH] Add register.available method --- STATUS.md | 1 + internal/backend.go | 1 + internal/backends/memory/backend.go | 11 +++++++++ internal/backends/memory/backend_test.go | 23 +++++++++++++++++++ internal/handlers.go | 22 ++++++++++++++++++ .../registeravailable/registeravailable.go | 9 ++++++++ internal/server.go | 1 + 7 files changed, 68 insertions(+) create mode 100644 internal/models/registeravailable/registeravailable.go diff --git a/STATUS.md b/STATUS.md index a084648..457bfd8 100644 --- a/STATUS.md +++ b/STATUS.md @@ -8,6 +8,7 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [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.1 POST /_matrix/client/r0/register](https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-register) - [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.5.8 GET /_matrix/client/r0/register/available](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-register-available) - [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 d32c59a..a54651a 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -12,6 +12,7 @@ type Backend interface { Register(username, password, device string) (user User, token string, err *models.ApiError) Login(username, password, device string) (user User, token string, err *models.ApiError) GetUserByToken(token string) (user User) + GetUserByName(userName string) User GetRoomByID(id string) Room Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError) } diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index 2da8351..727800b 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -100,3 +100,14 @@ func (backend *Backend) GetRoomByID(id string) internal.Room { return nil } + +func (backend *Backend) GetUserByName(userName string) internal.User { + backend.mutex.Lock() + defer backend.mutex.Unlock() + + if user, exists := backend.data[userName]; exists { + return user + } + + return nil +} diff --git a/internal/backends/memory/backend_test.go b/internal/backends/memory/backend_test.go index 8851508..31d08f4 100644 --- a/internal/backends/memory/backend_test.go +++ b/internal/backends/memory/backend_test.go @@ -113,3 +113,26 @@ func TestGetRoomByID(t *testing.T) { room = backend.GetRoomByID("worng id") assert.Nil(t, room) } + +func TestGetUserByName(t *testing.T) { + backend := NewBackend("localhost") + + var ( + userName = "user" + ) + + user, token, err := backend.Register(userName, "", "") + assert.Nil(t, err) + assert.NotNil(t, user) + assert.NotEmpty(t, token) + + t.Run("Test picking user with username", func(_ *testing.T) { + user2 := backend.GetUserByName(userName) + assert.Equal(t, user, user2) + }) + + t.Run("Test picking user with wrong username", func(_ *testing.T) { + user2 := backend.GetUserByName("wrong username") + assert.Nil(t, user2) + }) +} diff --git a/internal/handlers.go b/internal/handlers.go index 98e0508..44bfc10 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" + "github.com/nxshock/signaller/internal/models/registeravailable" + "github.com/nxshock/signaller/internal/models/devices" "github.com/gorilla/mux" @@ -348,6 +350,26 @@ func DevicesHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-register-available +func registerAvailableHandler(w http.ResponseWriter, r *http.Request) { + var request registeravailable.Request + + err := getRequest(r, &request) + if err != nil { + errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error()) + return + } + + user := currServer.Backend.GetUserByName(request.Username) // TODO: add validation of username + if user != nil { + errorResponse(w, models.M_USER_IN_USE, http.StatusBadRequest, "Desired user ID is already taken.") + return + } + + response := registeravailable.Response{Available: false} + sendJsonResponse(w, http.StatusOK, response) +} + // https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-directory-list-room-roomid // https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-directory-list-room-roomid func listRoomHandler(w http.ResponseWriter, r *http.Request) { diff --git a/internal/models/registeravailable/registeravailable.go b/internal/models/registeravailable/registeravailable.go new file mode 100644 index 0000000..701b743 --- /dev/null +++ b/internal/models/registeravailable/registeravailable.go @@ -0,0 +1,9 @@ +package registeravailable + +type Request struct { + Username string `json:"username"` // The username to check the availability of. +} + +type Response struct { + Available bool `json:"available"` // A flag to indicate that the username is available. This should always be true when the server replies with 200 OK. +} diff --git a/internal/server.go b/internal/server.go index b947fcb..28cdffd 100644 --- a/internal/server.go +++ b/internal/server.go @@ -38,6 +38,7 @@ func NewServer(port int) (*Server, error) { router.HandleFunc("/_matrix/client/r0/devices", DevicesHandler) router.HandleFunc("/_matrix/client/r0/directory/list/room/{roomID}", listRoomHandler) router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler) + router.HandleFunc("/_matrix/client/r0/register/available", registerAvailableHandler) router.HandleFunc("/", RootHandler) if port <= 0 || port > 65535 {