diff --git a/lib/client.js b/lib/client.js index c257edffb..232b2f19b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -80,8 +80,12 @@ var DeviceVerification = { * @param {Object=} opts.store The data store to use. If not specified, * this client will not store any HTTP responses. * - * @param {string=} opts.deviceId A unique identifier for this device, for use - * in end-to-end crypto. If not specified, end-to-end crypto will be disabled. + * @param {string=} opts.deviceId A unique identifier for this device; used for + * tracking things like crypto keys and access tokens. If not specified, + * end-to-end crypto will be disabled. + * + * @param {string=} opts.defaultDeviceDisplayName Initial display name to set + * on new devices * * @param {Object=} opts.sessionStore A store to be used for end-to-end crypto * session data. This should be a {@link @@ -115,9 +119,16 @@ function MatrixClient(opts) { this.sessionStore = opts.sessionStore || null; this.deviceId = opts.deviceId; + this.defaultDeviceDisplayName = opts.defaultDeviceDisplayName || "js-sdk device"; + + var userId = (opts.userId || null); + this.credentials = { + userId: userId, + }; + this._olmDevice = null; - if (CRYPTO_ENABLED && this.sessionStore !== null) { + if (CRYPTO_ENABLED && this.sessionStore !== null && userId !== null) { this._olmDevice = new OlmDevice(opts.sessionStore); var json = '{"algorithms":["' + OLM_ALGORITHM + '"]'; @@ -128,13 +139,13 @@ function MatrixClient(opts) { json += ',"curve25519:' + this.deviceId + '":'; json += JSON.stringify(this._olmDevice.deviceCurve25519Key); json += '}'; - json += ',"user_id":' + JSON.stringify(opts.userId); + json += ',"user_id":' + JSON.stringify(userId); json += '}'; var signature = this._olmDevice.sign(json); this.deviceKeys = JSON.parse(json); var signatures = {}; - signatures[opts.userId] = {}; - signatures[opts.userId]["ed25519:" + this.deviceId] = signature; + signatures[userId] = {}; + signatures[userId]["ed25519:" + this.deviceId] = signature; this.deviceKeys.signatures = signatures; var deviceInfo = { @@ -143,11 +154,11 @@ function MatrixClient(opts) { verified: DeviceVerification.VERIFIED, }; var myDevices = this.sessionStore.getEndToEndDevicesForUser( - opts.userId + userId ) || {}; myDevices[opts.deviceId] = deviceInfo; this.sessionStore.storeEndToEndDevicesForUser( - opts.userId, myDevices + userId, myDevices ); setupCryptoEventHandler(this); } @@ -174,9 +185,6 @@ function MatrixClient(opts) { onlyData: true, extraParams: opts.queryParams }; - this.credentials = { - userId: (opts.userId || null) - }; this._http = new httpApi.MatrixHttpApi(this, httpOpts); this.callList = { // callId: MatrixCall @@ -2689,9 +2697,19 @@ MatrixClient.prototype.paginateEventTimeline = function(eventTimeline, opts) { * @return {module:http-api.MatrixError} Rejects: with an error response. */ MatrixClient.prototype.login = function(loginType, data, callback) { - data.type = loginType; + var login_data = { + type: loginType, + device_id: this.deviceId, + initial_device_display_name: this.defaultDeviceDisplayName, + }; + + // merge data into login_data + for (var k in data) { + if (data.hasOwnProperty(k)) { login_data[k] = data[k]; } + } + return this._http.authedRequest( - callback, "POST", "/login", undefined, data + callback, "POST", "/login", undefined, login_data ); }; @@ -2706,13 +2724,7 @@ MatrixClient.prototype.login = function(loginType, data, callback) { MatrixClient.prototype.registerGuest = function(opts, callback) { opts = opts || {}; opts.body = opts.body || {}; - - return this._http.request( - callback, "POST", "/register", { - kind: "guest" - }, - opts.body - ); + return this.registerRequest(opts.body, "guest", callback); }; /** @@ -2793,9 +2805,33 @@ MatrixClient.prototype.register = function(username, password, if (bindEmail !== undefined) { params.bind_email = bindEmail; } if (guestAccessToken !== undefined) { params.guest_access_token = guestAccessToken; } + return this.registerRequest(params, undefined, callback); +}; + + +/** + * @param {Object} data parameters for registration request + * @param {string=} kind type of user to register. may be "guest" + * @param {module:client.callback=} callback + * @return {module:client.Promise} Resolves: to the /register response + * @return {module:http-api.MatrixError} Rejects: with an error response. + */ +MatrixClient.prototype.registerRequest = function(data, kind, callback) { + var request_data = { + device_id: this.deviceId, + initial_device_display_name: this.defaultDeviceDisplayName, + }; + + // merge data into request_data + for (var k in data) { + if (data.hasOwnProperty(k)) { request_data[k] = data[k]; } + } + + var params = {}; + if (kind) { params.kind = kind; } + return this._http.request( - callback, "POST", "/register", undefined, - params + callback, "POST", "/register", params, request_data ); };