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 {
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 {
@ -70,7 +72,6 @@ func GetDeviceByID(
Code: http.StatusOK,
JSON: deviceJSON{
DeviceID: dev.ID,
UserID: dev.UserID,
},
}
}
@ -98,7 +99,6 @@ func GetDevicesByLocalpart(
for _, dev := range deviceList {
res.Devices = append(res.Devices, deviceJSON{
DeviceID: dev.ID,
UserID: dev.UserID,
})
}

View file

@ -15,6 +15,8 @@
package routing
import (
"encoding/json"
"fmt"
"net/http"
"context"
@ -45,15 +47,22 @@ type loginIdentifier struct {
User string `json:"user"`
}
type passwordRequest struct {
type loginWithPasswordRequest struct {
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 ("")
// Thus a pointer is needed to differentiate between the two
InitialDisplayName *string `json:"initial_device_display_name"`
DeviceID *string `json:"device_id"`
}
type loginRequest struct {
Type string `json:"type"`
loginWithPasswordRequest
}
type loginResponse struct {
UserID string `json:"user_id"`
AccessToken string `json:"access_token"`
@ -79,12 +88,23 @@ func Login(
JSON: passwordLogin(),
}
} else if req.Method == http.MethodPost {
var r passwordRequest
var temp interface{}
var acc *authtypes.Account
resErr := httputil.UnmarshalJSONRequest(req, &r)
resErr := httputil.UnmarshalJSONRequest(req, &temp)
if resErr != nil {
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 {
case "m.id.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"),
}
}
case "m.login.token":
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Unknown("Token login is not supported"),
}
default:
return util.JSONResponse{
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()
}
dev, err := getDevice(req.Context(), r, deviceDB, acc, token)
dev, err := getDevice(req.Context(), r.loginWithPasswordRequest, deviceDB, acc, token)
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
@ -153,7 +186,7 @@ func Login(
// getDevice returns a new or existing device
func getDevice(
ctx context.Context,
r passwordRequest,
r loginWithPasswordRequest,
deviceDB devices.Database,
acc *authtypes.Account,
token string,

View file

@ -506,7 +506,11 @@ func Register(
"session_id": r.Auth.Session,
}).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(

View file

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