You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-29 16:43:09 +03:00
Merge pull request #151 from matrix-org/rav/device_id_in_login
/login, /register: Add device_id and initial_device_display_name
This commit is contained in:
@@ -80,8 +80,12 @@ var DeviceVerification = {
|
|||||||
* @param {Object=} opts.store The data store to use. If not specified,
|
* @param {Object=} opts.store The data store to use. If not specified,
|
||||||
* this client will not store any HTTP responses.
|
* this client will not store any HTTP responses.
|
||||||
*
|
*
|
||||||
* @param {string=} opts.deviceId A unique identifier for this device, for use
|
* @param {string=} opts.deviceId A unique identifier for this device; used for
|
||||||
* in end-to-end crypto. If not specified, end-to-end crypto will be disabled.
|
* 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
|
* @param {Object=} opts.sessionStore A store to be used for end-to-end crypto
|
||||||
* session data. This should be a {@link
|
* session data. This should be a {@link
|
||||||
@@ -115,9 +119,16 @@ function MatrixClient(opts) {
|
|||||||
this.sessionStore = opts.sessionStore || null;
|
this.sessionStore = opts.sessionStore || null;
|
||||||
|
|
||||||
this.deviceId = opts.deviceId;
|
this.deviceId = opts.deviceId;
|
||||||
|
this.defaultDeviceDisplayName = opts.defaultDeviceDisplayName || "js-sdk device";
|
||||||
|
|
||||||
|
var userId = (opts.userId || null);
|
||||||
|
this.credentials = {
|
||||||
|
userId: userId,
|
||||||
|
};
|
||||||
|
|
||||||
this._olmDevice = null;
|
this._olmDevice = null;
|
||||||
|
|
||||||
if (CRYPTO_ENABLED && this.sessionStore !== null) {
|
if (CRYPTO_ENABLED && this.sessionStore !== null && userId !== null) {
|
||||||
this._olmDevice = new OlmDevice(opts.sessionStore);
|
this._olmDevice = new OlmDevice(opts.sessionStore);
|
||||||
|
|
||||||
var json = '{"algorithms":["' + OLM_ALGORITHM + '"]';
|
var json = '{"algorithms":["' + OLM_ALGORITHM + '"]';
|
||||||
@@ -128,13 +139,13 @@ function MatrixClient(opts) {
|
|||||||
json += ',"curve25519:' + this.deviceId + '":';
|
json += ',"curve25519:' + this.deviceId + '":';
|
||||||
json += JSON.stringify(this._olmDevice.deviceCurve25519Key);
|
json += JSON.stringify(this._olmDevice.deviceCurve25519Key);
|
||||||
json += '}';
|
json += '}';
|
||||||
json += ',"user_id":' + JSON.stringify(opts.userId);
|
json += ',"user_id":' + JSON.stringify(userId);
|
||||||
json += '}';
|
json += '}';
|
||||||
var signature = this._olmDevice.sign(json);
|
var signature = this._olmDevice.sign(json);
|
||||||
this.deviceKeys = JSON.parse(json);
|
this.deviceKeys = JSON.parse(json);
|
||||||
var signatures = {};
|
var signatures = {};
|
||||||
signatures[opts.userId] = {};
|
signatures[userId] = {};
|
||||||
signatures[opts.userId]["ed25519:" + this.deviceId] = signature;
|
signatures[userId]["ed25519:" + this.deviceId] = signature;
|
||||||
this.deviceKeys.signatures = signatures;
|
this.deviceKeys.signatures = signatures;
|
||||||
|
|
||||||
var deviceInfo = {
|
var deviceInfo = {
|
||||||
@@ -143,11 +154,11 @@ function MatrixClient(opts) {
|
|||||||
verified: DeviceVerification.VERIFIED,
|
verified: DeviceVerification.VERIFIED,
|
||||||
};
|
};
|
||||||
var myDevices = this.sessionStore.getEndToEndDevicesForUser(
|
var myDevices = this.sessionStore.getEndToEndDevicesForUser(
|
||||||
opts.userId
|
userId
|
||||||
) || {};
|
) || {};
|
||||||
myDevices[opts.deviceId] = deviceInfo;
|
myDevices[opts.deviceId] = deviceInfo;
|
||||||
this.sessionStore.storeEndToEndDevicesForUser(
|
this.sessionStore.storeEndToEndDevicesForUser(
|
||||||
opts.userId, myDevices
|
userId, myDevices
|
||||||
);
|
);
|
||||||
setupCryptoEventHandler(this);
|
setupCryptoEventHandler(this);
|
||||||
}
|
}
|
||||||
@@ -174,9 +185,6 @@ function MatrixClient(opts) {
|
|||||||
onlyData: true,
|
onlyData: true,
|
||||||
extraParams: opts.queryParams
|
extraParams: opts.queryParams
|
||||||
};
|
};
|
||||||
this.credentials = {
|
|
||||||
userId: (opts.userId || null)
|
|
||||||
};
|
|
||||||
this._http = new httpApi.MatrixHttpApi(this, httpOpts);
|
this._http = new httpApi.MatrixHttpApi(this, httpOpts);
|
||||||
this.callList = {
|
this.callList = {
|
||||||
// callId: MatrixCall
|
// callId: MatrixCall
|
||||||
@@ -2689,9 +2697,19 @@ MatrixClient.prototype.paginateEventTimeline = function(eventTimeline, opts) {
|
|||||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||||
*/
|
*/
|
||||||
MatrixClient.prototype.login = function(loginType, data, callback) {
|
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(
|
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) {
|
MatrixClient.prototype.registerGuest = function(opts, callback) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
opts.body = opts.body || {};
|
opts.body = opts.body || {};
|
||||||
|
return this.registerRequest(opts.body, "guest", callback);
|
||||||
return this._http.request(
|
|
||||||
callback, "POST", "/register", {
|
|
||||||
kind: "guest"
|
|
||||||
},
|
|
||||||
opts.body
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2793,9 +2805,33 @@ MatrixClient.prototype.register = function(username, password,
|
|||||||
if (bindEmail !== undefined) { params.bind_email = bindEmail; }
|
if (bindEmail !== undefined) { params.bind_email = bindEmail; }
|
||||||
if (guestAccessToken !== undefined) { params.guest_access_token = guestAccessToken; }
|
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(
|
return this._http.request(
|
||||||
callback, "POST", "/register", undefined,
|
callback, "POST", "/register", params, request_data
|
||||||
params
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user