Massage client API a bit

This commit is contained in:
Neil Alexander 2020-06-12 09:22:24 +01:00
parent b6a591127e
commit 7729a9715d
4 changed files with 70 additions and 33 deletions

View file

@ -28,7 +28,9 @@ import (
type deviceJSON struct { type deviceJSON struct {
DeviceID string `json:"device_id"` DeviceID string `json:"device_id"`
UserID string `json:"user_id"` DisplayName string `json:"display_name"`
LastSeenIP string `json:"last_seen_ip"`
LastSeenTS uint64 `json:"last_seen_ts"`
} }
type devicesJSON struct { type devicesJSON struct {
@ -70,7 +72,6 @@ func GetDeviceByID(
Code: http.StatusOK, Code: http.StatusOK,
JSON: deviceJSON{ JSON: deviceJSON{
DeviceID: dev.ID, DeviceID: dev.ID,
UserID: dev.UserID,
}, },
} }
} }
@ -98,7 +99,6 @@ func GetDevicesByLocalpart(
for _, dev := range deviceList { for _, dev := range deviceList {
res.Devices = append(res.Devices, deviceJSON{ res.Devices = append(res.Devices, deviceJSON{
DeviceID: dev.ID, DeviceID: dev.ID,
UserID: dev.UserID,
}) })
} }

View file

@ -15,6 +15,8 @@
package routing package routing
import ( import (
"encoding/json"
"fmt"
"net/http" "net/http"
"context" "context"
@ -45,15 +47,22 @@ type loginIdentifier struct {
User string `json:"user"` User string `json:"user"`
} }
type passwordRequest struct { type loginWithPasswordRequest struct {
Identifier loginIdentifier `json:"identifier"` Identifier loginIdentifier `json:"identifier"`
Password string `json:"password"` Medium string `json:"medium"` // third-party only
Password string `json:"password"` // m.login.password only
Token string `json:"token"` // m.login.token only
// Both DeviceID and InitialDisplayName can be omitted, or empty strings ("") // Both DeviceID and InitialDisplayName can be omitted, or empty strings ("")
// Thus a pointer is needed to differentiate between the two // Thus a pointer is needed to differentiate between the two
InitialDisplayName *string `json:"initial_device_display_name"` InitialDisplayName *string `json:"initial_device_display_name"`
DeviceID *string `json:"device_id"` DeviceID *string `json:"device_id"`
} }
type loginRequest struct {
Type string `json:"type"`
loginWithPasswordRequest
}
type loginResponse struct { type loginResponse struct {
UserID string `json:"user_id"` UserID string `json:"user_id"`
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
@ -79,12 +88,23 @@ func Login(
JSON: passwordLogin(), JSON: passwordLogin(),
} }
} else if req.Method == http.MethodPost { } else if req.Method == http.MethodPost {
var r passwordRequest var temp interface{}
var acc *authtypes.Account var acc *authtypes.Account
resErr := httputil.UnmarshalJSONRequest(req, &r) resErr := httputil.UnmarshalJSONRequest(req, &temp)
if resErr != nil { if resErr != nil {
return *resErr return *resErr
} }
j, _ := json.MarshalIndent(temp, "", " ")
fmt.Println(string(j))
var r loginRequest
json.Unmarshal(j, &r)
switch r.Type {
case "m.login.password":
j, _ := json.MarshalIndent(r, "", " ")
fmt.Printf("LOGIN REQUEST: %+v\n", string(j))
switch r.Identifier.Type { switch r.Identifier.Type {
case "m.id.user": case "m.id.user":
if r.Identifier.User == "" { if r.Identifier.User == "" {
@ -113,10 +133,23 @@ func Login(
JSON: jsonerror.Forbidden("username or password was incorrect, or the account does not exist"), JSON: jsonerror.Forbidden("username or password was incorrect, or the account does not exist"),
} }
} }
case "m.login.token":
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Unknown("Token login is not supported"),
}
default: default:
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("login identifier '" + r.Identifier.Type + "' not supported"), JSON: jsonerror.Unknown("login identifier '" + r.Identifier.Type + "' not supported"),
}
}
default:
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Unknown(fmt.Sprintf("Login type %q not supported", r.Type)),
} }
} }
@ -126,7 +159,7 @@ func Login(
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
dev, err := getDevice(req.Context(), r, deviceDB, acc, token) dev, err := getDevice(req.Context(), r.loginWithPasswordRequest, deviceDB, acc, token)
if err != nil { if err != nil {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusInternalServerError, Code: http.StatusInternalServerError,
@ -153,7 +186,7 @@ func Login(
// getDevice returns a new or existing device // getDevice returns a new or existing device
func getDevice( func getDevice(
ctx context.Context, ctx context.Context,
r passwordRequest, r loginWithPasswordRequest,
deviceDB devices.Database, deviceDB devices.Database,
acc *authtypes.Account, acc *authtypes.Account,
token string, token string,

View file

@ -506,7 +506,11 @@ func Register(
"session_id": r.Auth.Session, "session_id": r.Auth.Session,
}).Info("Processing registration request") }).Info("Processing registration request")
return handleRegistrationFlow(req, r, sessionID, cfg, accountDB, deviceDB) resp := handleRegistrationFlow(req, r, sessionID, cfg, accountDB, deviceDB)
j, _ := json.MarshalIndent(resp, "", " ")
fmt.Println("ERROR!")
fmt.Println(string(j))
return resp
} }
func handleGuestRegistration( func handleGuestRegistration(

View file

@ -157,7 +157,7 @@ func main() {
logrus.Fatal(httpServer.Serve(ygg)) logrus.Fatal(httpServer.Serve(ygg))
}() }()
go func() { go func() {
httpBindAddr := fmt.Sprintf("localhost:%d", *instancePort) httpBindAddr := fmt.Sprintf(":%d", *instancePort)
logrus.Info("Listening on ", httpBindAddr) logrus.Info("Listening on ", httpBindAddr)
logrus.Fatal(http.ListenAndServe(httpBindAddr, nil)) logrus.Fatal(http.ListenAndServe(httpBindAddr, nil))
}() }()