2019-07-19 07:39:53 +00:00
|
|
|
package internal
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-19 16:29:20 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/common"
|
|
|
|
|
2019-07-19 07:39:53 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models"
|
2019-07-25 15:14:37 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/capabilities"
|
2019-07-24 15:04:45 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/joinedrooms"
|
2019-07-19 15:52:03 +00:00
|
|
|
login "github.com/nxshock/signaller/internal/models/login"
|
2019-07-25 16:56:31 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/password"
|
2019-07-19 15:52:03 +00:00
|
|
|
register "github.com/nxshock/signaller/internal/models/register"
|
|
|
|
mSync "github.com/nxshock/signaller/internal/models/sync"
|
2019-07-20 17:10:43 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/versions"
|
2019-07-23 14:37:02 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/whoami"
|
2019-07-18 14:10:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.RequestURI)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
// 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 {
|
2019-07-18 15:15:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
// 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 {
|
2019-07-18 15:15:19 +00:00
|
|
|
// https://models.org/docs/spec/client_server/latest#get-models-client-r0-login
|
2019-07-18 14:10:43 +00:00
|
|
|
case "GET":
|
|
|
|
{
|
2019-07-20 06:30:16 +00:00
|
|
|
response := login.GetReply{
|
2019-07-19 16:29:20 +00:00
|
|
|
Flows: []login.Flow{
|
|
|
|
login.Flow{Type: common.AuthenticationTypePassword},
|
|
|
|
},
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendJsonResponse(w, http.StatusOK, response)
|
|
|
|
}
|
2019-07-18 15:15:19 +00:00
|
|
|
// 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-19 07:39:53 +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, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-20 06:30:16 +00:00
|
|
|
response := login.PostReply{
|
2019-07-18 14:10:43 +00:00
|
|
|
UserID: request.Identifier.User,
|
2019-07-19 16:29:20 +00:00
|
|
|
AccessToken: token,
|
|
|
|
}
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
sendJsonResponse(w, http.StatusOK, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
// 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 {
|
2019-07-18 15:15:19 +00:00
|
|
|
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 == "" {
|
2019-07-18 15:15:19 +00:00
|
|
|
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{}{})
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
// 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 {
|
2019-07-18 15:15:19 +00:00
|
|
|
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" {
|
2019-07-18 15:15:19 +00:00
|
|
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind)
|
2019-07-18 14:10:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-19 15:52:03 +00:00
|
|
|
var request register.RegisterRequest
|
2019-07-18 14:10:43 +00:00
|
|
|
getRequest(r, &request) // TODO: handle error
|
|
|
|
|
2019-07-21 09:43:14 +00:00
|
|
|
_, 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, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-19 15:52:03 +00:00
|
|
|
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, "")
|
2019-07-30 11:45:47 +00:00
|
|
|
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, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
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, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}{})
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
// 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) {
|
2019-07-19 15:52:03 +00:00
|
|
|
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-07-18 15:15:19 +00:00
|
|
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes")
|
2019-07-18 14:10:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
request.Timeout = timeout
|
|
|
|
//log.Printf("%+v", request)
|
|
|
|
|
|
|
|
token := getTokenFromResponse(r)
|
|
|
|
if token == "" {
|
2019-07-18 15:15:19 +00:00
|
|
|
errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
2019-07-18 14:10:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:39:53 +00:00
|
|
|
response, _ := currServer.Backend.Sync(token, request) // TODO: handle error
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
response.NextBatch = "123"
|
2019-07-19 15:52:03 +00:00
|
|
|
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, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-29 14:32:38 +00:00
|
|
|
// 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-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)
|
|
|
|
}
|