Create blank device keys when logging in on a new device

This commit is contained in:
Kegan Dougal 2020-07-31 10:54:37 +01:00
parent b672915ba0
commit d23d031565
4 changed files with 26 additions and 15 deletions

View file

@ -23,8 +23,8 @@ import (
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/clientapi/userutil"
"github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/config"
userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/dendrite/userapi/storage/accounts" "github.com/matrix-org/dendrite/userapi/storage/accounts"
"github.com/matrix-org/dendrite/userapi/storage/devices"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
@ -57,7 +57,7 @@ func passwordLogin() flows {
// Login implements GET and POST /login // Login implements GET and POST /login
func Login( func Login(
req *http.Request, accountDB accounts.Database, deviceDB devices.Database, req *http.Request, accountDB accounts.Database, userAPI userapi.UserInternalAPI,
cfg *config.Dendrite, cfg *config.Dendrite,
) util.JSONResponse { ) util.JSONResponse {
if req.Method == http.MethodGet { if req.Method == http.MethodGet {
@ -81,7 +81,7 @@ func Login(
return *authErr return *authErr
} }
// make a device/access token // make a device/access token
return completeAuth(req.Context(), cfg.Matrix.ServerName, deviceDB, login) return completeAuth(req.Context(), cfg.Matrix.ServerName, userAPI, login)
} }
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusMethodNotAllowed, Code: http.StatusMethodNotAllowed,
@ -90,7 +90,7 @@ func Login(
} }
func completeAuth( func completeAuth(
ctx context.Context, serverName gomatrixserverlib.ServerName, deviceDB devices.Database, login *auth.Login, ctx context.Context, serverName gomatrixserverlib.ServerName, userAPI userapi.UserInternalAPI, login *auth.Login,
) util.JSONResponse { ) util.JSONResponse {
token, err := auth.GenerateAccessToken() token, err := auth.GenerateAccessToken()
if err != nil { if err != nil {
@ -104,9 +104,13 @@ func completeAuth(
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
dev, err := deviceDB.CreateDevice( var performRes userapi.PerformDeviceCreationResponse
ctx, localpart, login.DeviceID, token, login.InitialDisplayName, err = userAPI.PerformDeviceCreation(ctx, &userapi.PerformDeviceCreationRequest{
) DeviceDisplayName: login.InitialDisplayName,
DeviceID: login.DeviceID,
AccessToken: token,
Localpart: localpart,
}, &performRes)
if err != nil { if err != nil {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusInternalServerError, Code: http.StatusInternalServerError,
@ -117,10 +121,10 @@ func completeAuth(
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: loginResponse{ JSON: loginResponse{
UserID: dev.UserID, UserID: performRes.Device.UserID,
AccessToken: dev.AccessToken, AccessToken: performRes.Device.AccessToken,
HomeServer: serverName, HomeServer: serverName,
DeviceID: dev.ID, DeviceID: performRes.Device.ID,
}, },
} }
} }

View file

@ -387,7 +387,7 @@ func Setup(
r0mux.Handle("/login", r0mux.Handle("/login",
httputil.MakeExternalAPI("login", func(req *http.Request) util.JSONResponse { httputil.MakeExternalAPI("login", func(req *http.Request) util.JSONResponse {
return Login(req, accountDB, deviceDB, cfg) return Login(req, accountDB, userAPI, cfg)
}), }),
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)

View file

@ -131,6 +131,8 @@ Local device key changes appear in v2 /sync
Local device key changes appear in /keys/changes Local device key changes appear in /keys/changes
New users appear in /keys/changes New users appear in /keys/changes
Local delete device changes appear in v2 /sync Local delete device changes appear in v2 /sync
Local new device changes appear in v2 /sync
Users receive device_list updates for their own devices
Get left notifs for other users in sync and /keys/changes when user leaves Get left notifs for other users in sync and /keys/changes when user leaves
Can add account data Can add account data
Can add account data to room Can add account data to room

View file

@ -104,7 +104,8 @@ func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.Pe
} }
res.DeviceCreated = true res.DeviceCreated = true
res.Device = dev res.Device = dev
return nil // create empty device keys and upload them to trigger device list changes
return a.deviceListUpdate(dev.UserID, []string{dev.ID})
} }
func (a *UserInternalAPI) PerformDeviceDeletion(ctx context.Context, req *api.PerformDeviceDeletionRequest, res *api.PerformDeviceDeletionResponse) error { func (a *UserInternalAPI) PerformDeviceDeletion(ctx context.Context, req *api.PerformDeviceDeletionRequest, res *api.PerformDeviceDeletionResponse) error {
@ -121,10 +122,14 @@ func (a *UserInternalAPI) PerformDeviceDeletion(ctx context.Context, req *api.Pe
return err return err
} }
// create empty device keys and upload them to delete what was once there and trigger device list changes // create empty device keys and upload them to delete what was once there and trigger device list changes
deviceKeys := make([]keyapi.DeviceKeys, len(req.DeviceIDs)) return a.deviceListUpdate(req.UserID, req.DeviceIDs)
for i, did := range req.DeviceIDs { }
func (a *UserInternalAPI) deviceListUpdate(userID string, deviceIDs []string) error {
deviceKeys := make([]keyapi.DeviceKeys, len(deviceIDs))
for i, did := range deviceIDs {
deviceKeys[i] = keyapi.DeviceKeys{ deviceKeys[i] = keyapi.DeviceKeys{
UserID: req.UserID, UserID: userID,
DeviceID: did, DeviceID: did,
KeyJSON: nil, KeyJSON: nil,
} }