Merge pull request #410 from matrix-org/rav/use_server_device_id

Use server-generated deviceId
This commit is contained in:
David Baker 2016-08-12 11:40:55 +01:00 committed by GitHub
commit 33e9abe421
4 changed files with 22 additions and 14 deletions

View file

@ -134,6 +134,7 @@ function _loginWithToken(queryParams, defaultDeviceDisplayName) {
console.log("Logged in with token"); console.log("Logged in with token");
setLoggedIn({ setLoggedIn({
userId: data.user_id, userId: data.user_id,
deviceId: data.device_id,
accessToken: data.access_token, accessToken: data.access_token,
homeserverUrl: queryParams.homeserver, homeserverUrl: queryParams.homeserver,
identityServerUrl: queryParams.identityServer, identityServerUrl: queryParams.identityServer,
@ -164,6 +165,7 @@ function _registerAsGuest(hsUrl, isUrl, defaultDeviceDisplayName) {
console.log("Registered as guest: %s", creds.user_id); console.log("Registered as guest: %s", creds.user_id);
setLoggedIn({ setLoggedIn({
userId: creds.user_id, userId: creds.user_id,
deviceId: creds.device_id,
accessToken: creds.access_token, accessToken: creds.access_token,
homeserverUrl: hsUrl, homeserverUrl: hsUrl,
identityServerUrl: isUrl, identityServerUrl: isUrl,
@ -183,6 +185,7 @@ function _restoreFromLocalStorage() {
const is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org'; const is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org';
const access_token = localStorage.getItem("mx_access_token"); const access_token = localStorage.getItem("mx_access_token");
const user_id = localStorage.getItem("mx_user_id"); const user_id = localStorage.getItem("mx_user_id");
const device_id = localStorage.getItem("mx_device_id");
let is_guest; let is_guest;
if (localStorage.getItem("mx_is_guest") !== null) { if (localStorage.getItem("mx_is_guest") !== null) {
@ -196,6 +199,7 @@ function _restoreFromLocalStorage() {
console.log("Restoring session for %s", user_id); console.log("Restoring session for %s", user_id);
setLoggedIn({ setLoggedIn({
userId: user_id, userId: user_id,
deviceId: device_id,
accessToken: access_token, accessToken: access_token,
homeserverUrl: hs_url, homeserverUrl: hs_url,
identityServerUrl: is_url, identityServerUrl: is_url,
@ -223,10 +227,19 @@ export function setLoggedIn(credentials) {
try { try {
localStorage.setItem("mx_hs_url", credentials.homeserverUrl); localStorage.setItem("mx_hs_url", credentials.homeserverUrl);
localStorage.setItem("mx_is_url", credentials.identityServerUrl); localStorage.setItem("mx_is_url", credentials.identityServerUrl);
localStorage.setItem("mx_user_id", credentials.userId); localStorage.setItem("mx_user_id", credentials.userId);
localStorage.setItem("mx_access_token", credentials.accessToken); localStorage.setItem("mx_access_token", credentials.accessToken);
localStorage.setItem("mx_is_guest", JSON.stringify(credentials.guest)); localStorage.setItem("mx_is_guest", JSON.stringify(credentials.guest));
// if we didn't get a deviceId from the login, leave mx_device_id unset,
// rather than setting it to "undefined".
//
// (in this case MatrixClient doesn't bother with the crypto stuff
// - that's fine for us).
if (credentials.deviceId) {
localStorage.setItem("mx_device_id", credentials.deviceId);
}
console.log("Session persisted for %s", credentials.userId); console.log("Session persisted for %s", credentials.userId);
} catch (e) { } catch (e) {
console.warn("Error using local storage: can't persist session!", e); console.warn("Error using local storage: can't persist session!", e);

View file

@ -21,21 +21,11 @@ import utils from 'matrix-js-sdk/lib/utils';
const localStorage = window.localStorage; const localStorage = window.localStorage;
function deviceId() {
// XXX: is Math.random()'s deterministicity a problem here?
var id = Math.floor(Math.random()*16777215).toString(16);
id = "W" + "000000".substring(id.length) + id;
if (localStorage) {
id = localStorage.getItem("mx_device_id") || id;
localStorage.setItem("mx_device_id", id);
}
return id;
}
interface MatrixClientCreds { interface MatrixClientCreds {
homeserverUrl: string, homeserverUrl: string,
identityServerUrl: string, identityServerUrl: string,
userId: string, userId: string,
deviceId: string,
accessToken: string, accessToken: string,
guest: boolean, guest: boolean,
} }
@ -87,6 +77,7 @@ class MatrixClientPeg {
homeserverUrl: this.matrixClient.baseUrl, homeserverUrl: this.matrixClient.baseUrl,
identityServerUrl: this.matrixClient.idBaseUrl, identityServerUrl: this.matrixClient.idBaseUrl,
userId: this.matrixClient.credentials.userId, userId: this.matrixClient.credentials.userId,
deviceId: this.matrixClient.getDeviceId(),
accessToken: this.matrixClient.getAccessToken(), accessToken: this.matrixClient.getAccessToken(),
guest: this.matrixClient.isGuest(), guest: this.matrixClient.isGuest(),
}; };
@ -98,12 +89,12 @@ class MatrixClientPeg {
idBaseUrl: creds.identityServerUrl, idBaseUrl: creds.identityServerUrl,
accessToken: creds.accessToken, accessToken: creds.accessToken,
userId: creds.userId, userId: creds.userId,
deviceId: creds.deviceId,
timelineSupport: true, timelineSupport: true,
}; };
if (localStorage) { if (localStorage) {
opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage); opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage);
opts.deviceId = deviceId();
} }
this.matrixClient = Matrix.createClient(opts); this.matrixClient = Matrix.createClient(opts);

View file

@ -336,6 +336,7 @@ class Login extends Signup {
}).then((creds) => { }).then((creds) => {
return { return {
userId: creds.user_id, userId: creds.user_id,
deviceId: creds.device_id,
accessToken: creds.access_token, accessToken: creds.access_token,
homeserverUrl: this._hsUrl, homeserverUrl: this._hsUrl,
identityServerUrl: this._isUrl, identityServerUrl: this._isUrl,
@ -371,6 +372,7 @@ class Login extends Signup {
homeserverUrl: self._hsUrl, homeserverUrl: self._hsUrl,
identityServerUrl: self._isUrl, identityServerUrl: self._isUrl,
userId: data.user_id, userId: data.user_id,
deviceId: data.device_id,
accessToken: data.access_token accessToken: data.access_token
}); });
}, function(error) { }, function(error) {
@ -394,6 +396,7 @@ class Login extends Signup {
homeserverUrl: self._fallbackHsUrl, homeserverUrl: self._fallbackHsUrl,
identityServerUrl: self._isUrl, identityServerUrl: self._isUrl,
userId: data.user_id, userId: data.user_id,
deviceId: data.device_id,
accessToken: data.access_token accessToken: data.access_token
}); });
}, function(fallback_error) { }, function(fallback_error) {

View file

@ -159,6 +159,7 @@ module.exports = React.createClass({
} }
self.props.onLoggedIn({ self.props.onLoggedIn({
userId: response.user_id, userId: response.user_id,
deviceId: response.device_id,
homeserverUrl: self.registerLogic.getHomeserverUrl(), homeserverUrl: self.registerLogic.getHomeserverUrl(),
identityServerUrl: self.registerLogic.getIdentityServerUrl(), identityServerUrl: self.registerLogic.getIdentityServerUrl(),
accessToken: response.access_token accessToken: response.access_token