Add WhoAmI handler

This commit is contained in:
nxshock 2019-07-23 19:37:02 +05:00
parent a1717ec784
commit 1dbc5082a9
6 changed files with 39 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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