From 1dbc5082a90631138e24a00c95076e7731551e0a Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 23 Jul 2019 19:37:02 +0500 Subject: [PATCH] Add WhoAmI handler --- README.md | 1 + internal/backend.go | 1 + internal/backends/memory/backend.go | 15 +++++++++++++++ internal/handlers.go | 15 +++++++++++++++ internal/models/whoami/whoami.go | 6 ++++++ internal/server.go | 1 + 6 files changed, 39 insertions(+) create mode 100644 internal/models/whoami/whoami.go diff --git a/README.md b/README.md index 1acd7cc..ec680f9 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,4 @@ 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 diff --git a/internal/backend.go b/internal/backend.go index e4136df..0e8dbe0 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -11,6 +11,7 @@ type Backend interface { Register(username, password, device string) (user User, token string, err *models.ApiError) Login(username, password, device string) (token string, err *models.ApiError) Logout(token string) *models.ApiError + GetUserByToken(token string) (user User) 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 0a160e9..97a5cb1 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -91,3 +91,18 @@ func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response return nil, nil // TODO: implement } + +func (backend *Backend) GetUserByToken(token string) internal.User { + backend.mutex.Lock() + defer backend.mutex.Unlock() + + for _, user := range backend.data { + for userToken := range user.(*User).Tokens { + if userToken == token { + return user + } + } + } + + return nil +} diff --git a/internal/handlers.go b/internal/handlers.go index 7ed24a4..2872685 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -15,6 +15,7 @@ import ( register "github.com/nxshock/signaller/internal/models/register" mSync "github.com/nxshock/signaller/internal/models/sync" "github.com/nxshock/signaller/internal/models/versions" + "github.com/nxshock/signaller/internal/models/whoami" ) func RootHandler(w http.ResponseWriter, r *http.Request) { @@ -126,6 +127,20 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami +func WhoAmIHandler(w http.ResponseWriter, r *http.Request) { + token := getTokenFromResponse(r) + if token == "" { + errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "") + } + + user := currServer.Backend.GetUserByToken(token) + + response := whoami.Response{UserID: user.ID()} + + sendJsonResponse(w, http.StatusOK, response) +} + // 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/whoami/whoami.go b/internal/models/whoami/whoami.go new file mode 100644 index 0000000..db228c4 --- /dev/null +++ b/internal/models/whoami/whoami.go @@ -0,0 +1,6 @@ +package whoami + +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami +type Response struct { + UserID string `json:"user_id"` // Required. The user id that owns the access token. +} diff --git a/internal/server.go b/internal/server.go index e3e4c22..3f208de 100644 --- a/internal/server.go +++ b/internal/server.go @@ -22,6 +22,7 @@ func New() *Server { router.HandleFunc("/_matrix/client/r0/login", LoginHandler) router.HandleFunc("/_matrix/client/r0/logout", LogoutHandler) router.HandleFunc("/_matrix/client/r0/register", RegisterHandler) + router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler) router.HandleFunc("/_matrix/client/r0/sync", SyncHandler) router.HandleFunc("/", RootHandler)