signaller/internal/handlers.go

638 lines
18 KiB
Go
Raw Normal View History

package internal
2019-07-18 14:10:43 +00:00
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
2019-07-31 15:01:20 +00:00
"github.com/gorilla/mux"
2019-08-03 14:19:18 +00:00
"github.com/signaller-matrix/signaller/internal/models"
"github.com/signaller-matrix/signaller/internal/models/capabilities"
"github.com/signaller-matrix/signaller/internal/models/common"
"github.com/signaller-matrix/signaller/internal/models/devices"
2019-08-05 14:39:28 +00:00
"github.com/signaller-matrix/signaller/internal/models/filter"
2019-08-03 14:19:18 +00:00
"github.com/signaller-matrix/signaller/internal/models/joinedrooms"
"github.com/signaller-matrix/signaller/internal/models/listroom"
"github.com/signaller-matrix/signaller/internal/models/login"
"github.com/signaller-matrix/signaller/internal/models/password"
"github.com/signaller-matrix/signaller/internal/models/publicrooms"
"github.com/signaller-matrix/signaller/internal/models/register"
"github.com/signaller-matrix/signaller/internal/models/registeravailable"
"github.com/signaller-matrix/signaller/internal/models/roomalias"
2019-08-03 14:19:18 +00:00
mSync "github.com/signaller-matrix/signaller/internal/models/sync"
"github.com/signaller-matrix/signaller/internal/models/versions"
"github.com/signaller-matrix/signaller/internal/models/whoami"
2019-07-18 14:10:43 +00:00
)
func RootHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI)
}
// https://models.org/docs/spec/client_server/latest#get-models-client-versions
2019-07-18 14:10:43 +00:00
func VersionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
2019-07-18 14:10:43 +00:00
return
}
2019-07-20 17:10:43 +00:00
response := versions.Reply{Versions: []string{Version}}
2019-07-18 14:10:43 +00:00
sendJsonResponse(w, http.StatusOK, response)
}
// https://models.org/docs/spec/client_server/latest#login
2019-07-18 14:10:43 +00:00
func LoginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
// https://models.org/docs/spec/client_server/latest#get-models-client-r0-login
2019-07-18 14:10:43 +00:00
case "GET":
{
response := login.GetReply{
Flows: []login.Flow{
login.Flow{Type: common.AuthenticationTypePassword},
},
2019-07-18 14:10:43 +00:00
}
sendJsonResponse(w, http.StatusOK, response)
}
// https://models.org/docs/spec/client_server/latest#post-models-client-r0-login
2019-07-18 14:10:43 +00:00
case "POST":
{
2019-07-20 06:35:01 +00:00
var request login.PostRequest
2019-07-18 14:10:43 +00:00
getRequest(r, &request) // TODO: handle error
// delete start "@" if presents
if strings.HasPrefix(request.Identifier.User, "@") {
request.Identifier.User = strings.TrimPrefix(request.Identifier.User, "@")
}
2019-07-31 15:53:36 +00:00
_, token, apiErr := currServer.Backend.Login(request.Identifier.User, request.Password, request.DeviceID)
2019-07-18 14:10:43 +00:00
if apiErr != nil {
errorResponse(w, apiErr, http.StatusForbidden, "")
2019-07-18 14:10:43 +00:00
return
}
response := login.PostReply{
2019-07-18 14:10:43 +00:00
UserID: request.Identifier.User,
AccessToken: token,
}
2019-07-18 14:10:43 +00:00
sendJsonResponse(w, http.StatusOK, response)
}
}
}
2019-08-01 14:29:43 +00:00
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-leave
func leaveRoomHandler(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_MISSING_TOKEN, http.StatusBadRequest, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
room := currServer.Backend.GetRoomByID(mux.Vars(r)["roomID"]) // TODO: can ["roomID"] throw panic?
if room == nil {
errorResponse(w, models.M_NOT_FOUND, http.StatusBadRequest, "room not found")
return
}
err := user.LeaveRoom(room)
if err != nil {
errorResponse(w, err, http.StatusBadRequest, "")
2019-08-01 14:29:43 +00:00
return
}
sendJsonResponse(w, http.StatusOK, struct{}{})
}
// https://models.org/docs/spec/client_server/latest#post-models-client-r0-logout
2019-07-18 14:10:43 +00:00
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
2019-07-18 14:10:43 +00:00
return
}
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "")
2019-07-18 14:10:43 +00:00
return
}
2019-07-24 14:31:07 +00:00
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
2019-07-18 14:10:43 +00:00
return
}
2019-07-24 14:31:07 +00:00
user.Logout(token)
2019-07-18 14:10:43 +00:00
sendJsonResponse(w, http.StatusOK, struct{}{})
}
2019-07-24 14:15:07 +00:00
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all
func LogoutAllHandler(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_MISSING_TOKEN, http.StatusBadRequest, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
user.LogoutAll()
sendJsonResponse(w, http.StatusOK, struct{}{})
}
// https://models.org/docs/spec/client_server/latest#post-models-client-r0-register
2019-07-18 14:10:43 +00:00
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
2019-07-18 14:10:43 +00:00
return
}
kind := r.FormValue("kind")
if kind != "user" {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind)
2019-07-18 14:10:43 +00:00
return
}
var request register.RegisterRequest
2019-07-18 14:10:43 +00:00
getRequest(r, &request) // TODO: handle error
_, token, apiErr := currServer.Backend.Register(request.Username, request.Password, request.DeviceID)
2019-07-18 14:10:43 +00:00
if apiErr != nil {
errorResponse(w, apiErr, http.StatusBadRequest, "")
2019-07-18 14:10:43 +00:00
return
}
var response register.RegisterResponse
2019-07-18 14:10:43 +00:00
response.UserID = "@" + request.Username
response.DeviceID = request.DeviceID
response.AccessToken = token
sendJsonResponse(w, http.StatusOK, response)
}
2019-07-23 14:37:02 +00:00
// 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 == "" {
2019-07-29 14:36:58 +00:00
errorResponse(w, models.M_MISSING_TOKEN, http.StatusForbidden, "")
return
2019-07-23 14:37:02 +00:00
}
user := currServer.Backend.GetUserByToken(token)
2019-07-29 14:36:58 +00:00
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusForbidden, "")
return
2019-07-29 14:36:58 +00:00
}
2019-07-23 14:37:02 +00:00
response := whoami.Response{UserID: user.ID()}
sendJsonResponse(w, http.StatusOK, response)
}
2019-07-24 15:04:45 +00:00
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms
func JoinedRoomsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
2019-07-31 15:03:12 +00:00
return
2019-07-24 15:04:45 +00:00
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
rooms := user.JoinedRooms()
var response joinedrooms.Response
for _, room := range rooms {
response.JoinedRooms = append(response.JoinedRooms, room.ID())
}
sendJsonResponse(w, http.StatusOK, response)
}
2019-07-25 16:56:31 +00:00
// 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, "")
2019-07-31 15:03:12 +00:00
return
2019-07-25 16:56:31 +00:00
}
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
2019-07-18 14:10:43 +00:00
func SyncHandler(w http.ResponseWriter, r *http.Request) {
var request mSync.SyncRequest
2019-07-18 14:10:43 +00:00
request.Filter = r.FormValue("filter")
timeout, err := strconv.Atoi(r.FormValue("timeout"))
if err != nil {
2019-08-11 13:53:18 +00:00
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failed")
2019-07-18 14:10:43 +00:00
return
}
request.Timeout = timeout
token := getTokenFromResponse(r)
if token == "" {
2019-08-11 13:53:18 +00:00
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
2019-07-18 14:10:43 +00:00
return
}
2019-08-11 13:53:18 +00:00
response, _ := user.Sync(token, request) // TODO: handle error
2019-07-18 14:10:43 +00:00
response.NextBatch = "123"
response.Rooms = mSync.RoomsSyncReply{}
2019-07-18 14:10:43 +00:00
sendJsonResponse(w, http.StatusOK, response)
}
2019-07-25 15:14:37 +00:00
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities
func CapabilitiesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
2019-07-31 15:03:12 +00:00
return
2019-07-25 15:14:37 +00:00
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
var response capabilities.Response
response.Capabilities = currServer.Capabilities
sendJsonResponse(w, http.StatusOK, response)
}
2019-08-05 14:39:28 +00:00
//
2019-08-04 16:46:31 +00:00
func AddFilterHandler(w http.ResponseWriter, r *http.Request) {
2019-08-05 10:12:48 +00:00
if r.Method != http.MethodPost {
2019-08-04 16:46:31 +00:00
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
2019-08-05 14:39:28 +00:00
var request filter.Request
err := getRequest(r, &request)
if err != nil {
errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error())
return
}
2019-08-04 16:46:31 +00:00
filterID := RandomString(12)
user.AddFilter(filterID, common.Filter(request))
2019-08-04 16:46:31 +00:00
2019-08-05 14:39:28 +00:00
response := filter.Response{FilterID: filterID}
sendJsonResponse(w, http.StatusOK, response)
2019-08-04 16:46:31 +00:00
}
func GetFilterHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
filter := user.GetFilterByID(mux.Vars(r)["filterID"])
if filter == nil {
errorResponse(w, models.M_INVALID_PARAM, http.StatusNotFound, "")
2019-08-05 14:39:28 +00:00
return
2019-08-04 16:46:31 +00:00
}
sendJsonResponse(w, http.StatusOK, filter)
}
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-devices
func DevicesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
2019-07-31 15:05:28 +00:00
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
var response devices.Response
response.Devices = user.Devices()
sendJsonResponse(w, http.StatusOK, response)
}
2019-08-02 13:17:18 +00:00
// 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)
}
2019-07-31 15:01:20 +00:00
// 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) {
room := currServer.Backend.GetRoomByID(mux.Vars(r)["roomID"]) // TODO: can ["roomID"] throw panic?
if room == nil {
errorResponse(w, models.M_NOT_FOUND, http.StatusBadRequest, "room not found")
return
}
switch r.Method {
case http.MethodGet:
response := listroom.Response{
Visibility: room.Visibility()}
sendJsonResponse(w, http.StatusOK, response)
case http.MethodPut:
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
var request listroom.Request
err := getRequest(r, &request)
if err != nil {
errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error())
return
}
user.SetRoomVisibility(room, request.Visibility)
sendJsonResponse(w, http.StatusOK, struct{}{})
default:
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
}
}
// https://matrix.org/docs/spec/client_server/latest#id335
func publicRoomsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodPost {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
var request publicrooms.Request
err := getRequest(r, &request)
if err != nil {
errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error())
return
}
if request.Limit <= 0 {
request.Limit = 50 // TODO: move to const
}
if r.Method == http.MethodPost {
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
}
publicRooms := currServer.Backend.PublicRooms(request.Filter.GenericSearchTerm) // TODO: make Post request as User method
chunks := roomsToPublicRoomsChunks(publicRooms)
var response publicrooms.Response
// TODO: test and rewrite this code
if request.Since == "" {
if request.Limit >= len(chunks) {
response.Chunk = chunks
// TODO: should fill response.TotalRoomCountEstimate?
} else {
response.Chunk = chunks[:request.Limit]
response.NextBatch = strconv.Itoa(request.Limit + 1)
response.TotalRoomCountEstimate = len(chunks)
}
} else {
response.PrevBatch = "0"
from, err := strconv.Atoi(request.Since)
if err != nil {
errorResponse(w, models.M_INVALID_PARAM, http.StatusBadRequest, "Wrong Since field specified") // TODO: check code
return
}
if len(chunks) >= from {
if request.Limit >= len(chunks) {
response.Chunk = chunks[from : from+request.Limit]
} else {
response.Chunk = chunks[from:]
}
}
}
sendJsonResponse(w, http.StatusOK, response)
}
// https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-directory-room-roomalias
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-directory-room-roomalias
// https://matrix.org/docs/spec/client_server/latest#delete-matrix-client-r0-directory-room-roomalias
func roomAliasHandler(w http.ResponseWriter, r *http.Request) {
roomAlias := mux.Vars(r)["roomAlias"] // TODO: add validation of room alias
var user User
if r.Method == http.MethodPut || r.Method == http.MethodDelete {
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
return
}
user = currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
}
var request roomalias.Request
err := getRequest(r, &request)
if err != nil {
errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error())
return
}
var response interface{}
switch r.Method {
case http.MethodGet:
2019-08-08 14:38:30 +00:00
room := currServer.Backend.GetRoomByAlias(StripAlias(currServer.Address, roomAlias))
if room == nil {
errorResponse(w, models.M_NOT_FOUND, http.StatusNotFound, "room not found")
return
}
response = roomalias.ResponseGet{
RoomID: room.ID(),
Servers: []string{currServer.Address}}
case http.MethodPut:
room := currServer.Backend.GetRoomByID(request.RoomID)
if room == nil {
errorResponse(w, models.M_NOT_FOUND, http.StatusNotFound, "room not found")
return
}
err := user.AddRoomAlias(room, roomAlias)
if err != nil {
errorResponse(w, err, http.StatusConflict, "") // TODO: check http code
return
}
response = struct{}{}
case http.MethodDelete:
2019-08-08 14:38:30 +00:00
err := user.DeleteRoomAlias(StripAlias(currServer.Address, roomAlias))
if err != nil {
errorResponse(w, err, http.StatusConflict, "") // TODO: check http code
return
}
response = struct{}{}
}
sendJsonResponse(w, http.StatusOK, response)
}
2019-07-18 14:10:43 +00:00
func sendJsonResponse(w http.ResponseWriter, httpStatus int, data interface{}) error {
b, err := json.Marshal(data)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)
_, err = w.Write(b) // TODO: handle n
return err
}
func getRequest(r *http.Request, request interface{}) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(b, request)
}