2015-11-12 11:54:35 +00:00
|
|
|
"use strict";
|
2016-08-11 12:50:38 +00:00
|
|
|
|
|
|
|
import Matrix from "matrix-js-sdk";
|
|
|
|
|
2015-11-12 11:54:35 +00:00
|
|
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
2015-11-18 17:13:43 +00:00
|
|
|
var SignupStages = require("./SignupStages");
|
2015-11-12 11:54:35 +00:00
|
|
|
var dis = require("./dispatcher");
|
2015-11-12 15:15:00 +00:00
|
|
|
var q = require("q");
|
2015-11-12 11:54:35 +00:00
|
|
|
|
2015-11-18 17:13:43 +00:00
|
|
|
const EMAIL_STAGE_TYPE = "m.login.email.identity";
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
/**
|
|
|
|
* A base class for common functionality between Registration and Login e.g.
|
|
|
|
* storage of HS/IS URLs.
|
|
|
|
*/
|
2015-11-17 17:25:14 +00:00
|
|
|
class Signup {
|
|
|
|
constructor(hsUrl, isUrl) {
|
|
|
|
this._hsUrl = hsUrl;
|
|
|
|
this._isUrl = isUrl;
|
|
|
|
}
|
2015-11-12 11:54:35 +00:00
|
|
|
|
2015-11-17 17:25:14 +00:00
|
|
|
getHomeserverUrl() {
|
|
|
|
return this._hsUrl;
|
|
|
|
}
|
2015-11-12 11:54:35 +00:00
|
|
|
|
2015-11-17 17:25:14 +00:00
|
|
|
getIdentityServerUrl() {
|
|
|
|
return this._isUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHomeserverUrl(hsUrl) {
|
2015-11-12 11:54:35 +00:00
|
|
|
this._hsUrl = hsUrl;
|
2015-11-17 17:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setIdentityServerUrl(isUrl) {
|
2015-11-12 11:54:35 +00:00
|
|
|
this._isUrl = isUrl;
|
2015-11-17 17:25:14 +00:00
|
|
|
}
|
2016-08-11 12:50:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a temporary MatrixClient, which can be used for login or register
|
|
|
|
* requests.
|
|
|
|
*/
|
|
|
|
_createTemporaryClient() {
|
|
|
|
return Matrix.createClient({
|
|
|
|
baseUrl: this._hsUrl,
|
|
|
|
idBaseUrl: this._isUrl,
|
|
|
|
});
|
|
|
|
}
|
2015-11-17 17:25:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
/**
|
|
|
|
* Registration logic class
|
|
|
|
*/
|
2015-11-17 17:25:14 +00:00
|
|
|
class Register extends Signup {
|
|
|
|
constructor(hsUrl, isUrl) {
|
|
|
|
super(hsUrl, isUrl);
|
2015-11-18 17:13:43 +00:00
|
|
|
this.setStep("START");
|
|
|
|
this.data = null; // from the server
|
2015-11-19 16:07:58 +00:00
|
|
|
// random other stuff (e.g. query params, NOT params from the server)
|
|
|
|
this.params = {};
|
2015-11-18 17:13:43 +00:00
|
|
|
this.credentials = null;
|
2015-11-18 17:43:38 +00:00
|
|
|
this.activeStage = null;
|
2015-11-19 15:19:30 +00:00
|
|
|
this.registrationPromise = null;
|
|
|
|
// These values MUST be undefined else we'll send "username: null" which
|
|
|
|
// will error on Synapse rather than having the key absent.
|
|
|
|
this.username = undefined; // desired
|
|
|
|
this.email = undefined; // desired
|
|
|
|
this.password = undefined; // desired
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setClientSecret(secret) {
|
|
|
|
this.params.clientSecret = secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSessionId(sessionId) {
|
|
|
|
this.params.sessionId = sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
setRegistrationUrl(regUrl) {
|
|
|
|
this.params.registrationUrl = regUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIdSid(idSid) {
|
|
|
|
this.params.idSid = idSid;
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:23:32 +00:00
|
|
|
setGuestAccessToken(token) {
|
|
|
|
this.guestAccessToken = token;
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:13:43 +00:00
|
|
|
getStep() {
|
|
|
|
return this._step;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCredentials() {
|
|
|
|
return this.credentials;
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:43:38 +00:00
|
|
|
getServerData() {
|
|
|
|
return this.data || {};
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:19:30 +00:00
|
|
|
getPromise() {
|
|
|
|
return this.registrationPromise;
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:13:43 +00:00
|
|
|
setStep(step) {
|
|
|
|
this._step = 'Register.' + step;
|
|
|
|
// TODO:
|
|
|
|
// It's a shame this is going to the global dispatcher, we only really
|
|
|
|
// want things which have an instance of this class to be able to add
|
|
|
|
// listeners...
|
|
|
|
console.log("Dispatching 'registration_step_update' for step %s", this._step);
|
|
|
|
dis.dispatch({
|
|
|
|
action: "registration_step_update"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
register(formVals) {
|
|
|
|
var {username, password, email} = formVals;
|
|
|
|
this.email = email;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
2016-08-11 12:50:38 +00:00
|
|
|
const client = this._createTemporaryClient();
|
|
|
|
return this._tryRegister(client);
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:50:38 +00:00
|
|
|
_tryRegister(client, authDict, poll_for_success) {
|
2015-11-18 17:13:43 +00:00
|
|
|
var self = this;
|
2015-12-17 14:34:45 +00:00
|
|
|
|
|
|
|
var bindEmail;
|
|
|
|
|
|
|
|
if (this.username && this.password) {
|
|
|
|
// only need to bind_email when sending u/p - sending it at other
|
|
|
|
// times clobbers the u/p resulting in M_MISSING_PARAM (password)
|
|
|
|
bindEmail = true;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:50:38 +00:00
|
|
|
return client.register(
|
2016-01-07 17:23:32 +00:00
|
|
|
this.username, this.password, this.params.sessionId, authDict, bindEmail,
|
2016-06-02 12:14:52 +00:00
|
|
|
this.guestAccessToken
|
2015-11-18 17:13:43 +00:00
|
|
|
).then(function(result) {
|
|
|
|
self.credentials = result;
|
|
|
|
self.setStep("COMPLETE");
|
|
|
|
return result; // contains the credentials
|
|
|
|
}, function(error) {
|
2016-03-16 19:42:52 +00:00
|
|
|
if (error.httpStatus === 401) {
|
|
|
|
if (error.data && error.data.flows) {
|
2016-03-17 09:42:52 +00:00
|
|
|
// Remember the session ID from the server:
|
|
|
|
// Either this is our first 401 in which case we need to store the
|
|
|
|
// session ID for future calls, or it isn't in which case this
|
|
|
|
// is just a no-op since it ought to be the same (or if it isn't,
|
|
|
|
// we should use the latest one from the server in any case).
|
2016-03-16 19:42:52 +00:00
|
|
|
self.params.sessionId = error.data.session;
|
|
|
|
self.data = error.data || {};
|
|
|
|
var flow = self.chooseFlow(error.data.flows);
|
|
|
|
|
|
|
|
if (flow) {
|
|
|
|
console.log("Active flow => %s", JSON.stringify(flow));
|
|
|
|
var flowStage = self.firstUncompletedStage(flow);
|
|
|
|
if (flowStage != self.activeStage) {
|
2016-08-11 12:50:38 +00:00
|
|
|
return self._startStage(client, flowStage).catch(function(err) {
|
2016-07-06 14:22:06 +00:00
|
|
|
self.setStep('START');
|
|
|
|
throw err;
|
|
|
|
});
|
2016-03-16 19:42:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
2016-03-16 19:42:52 +00:00
|
|
|
if (poll_for_success) {
|
|
|
|
return q.delay(5000).then(function() {
|
2016-08-11 12:50:38 +00:00
|
|
|
return self._tryRegister(client, authDict, poll_for_success);
|
2016-03-16 19:42:52 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error("Authorisation failed!");
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (error.errcode === 'M_USER_IN_USE') {
|
|
|
|
throw new Error("Username in use");
|
2016-01-15 10:08:16 +00:00
|
|
|
} else if (error.errcode == 'M_INVALID_USERNAME') {
|
2016-01-14 17:28:53 +00:00
|
|
|
throw new Error("User names may only contain alphanumeric characters, underscores or dots!");
|
2015-11-18 17:13:43 +00:00
|
|
|
} else if (error.httpStatus >= 400 && error.httpStatus < 500) {
|
|
|
|
throw new Error(`Registration failed! (${error.httpStatus})`);
|
|
|
|
} else if (error.httpStatus >= 500 && error.httpStatus < 600) {
|
|
|
|
throw new Error(
|
|
|
|
`Server error during registration! (${error.httpStatus})`
|
|
|
|
);
|
|
|
|
} else if (error.name == "M_MISSING_PARAM") {
|
|
|
|
// The HS hasn't remembered the login params from
|
|
|
|
// the first try when the login email was sent.
|
|
|
|
throw new Error(
|
|
|
|
"This home server does not support resuming registration."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:19:30 +00:00
|
|
|
firstUncompletedStage(flow) {
|
2015-11-18 17:13:43 +00:00
|
|
|
for (var i = 0; i < flow.stages.length; ++i) {
|
2015-11-19 15:19:30 +00:00
|
|
|
if (!this.hasCompletedStage(flow.stages[i])) {
|
|
|
|
return flow.stages[i];
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
hasCompletedStage(stageType) {
|
|
|
|
var completed = (this.data || {}).completed || [];
|
|
|
|
return completed.indexOf(stageType) !== -1;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:50:38 +00:00
|
|
|
_startStage(client, stageName) {
|
2015-11-18 17:13:43 +00:00
|
|
|
var self = this;
|
|
|
|
this.setStep(`STEP_${stageName}`);
|
|
|
|
var StageClass = SignupStages[stageName];
|
|
|
|
if (!StageClass) {
|
|
|
|
// no idea how to handle this!
|
|
|
|
throw new Error("Unknown stage: " + stageName);
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:50:38 +00:00
|
|
|
var stage = new StageClass(client, this);
|
2015-11-18 17:43:38 +00:00
|
|
|
this.activeStage = stage;
|
2015-11-18 17:13:43 +00:00
|
|
|
return stage.complete().then(function(request) {
|
|
|
|
if (request.auth) {
|
2015-11-19 16:07:58 +00:00
|
|
|
console.log("Stage %s is returning an auth dict", stageName);
|
2016-08-11 12:50:38 +00:00
|
|
|
return self._tryRegister(client, request.auth, request.poll_for_success);
|
2015-11-19 11:41:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// never resolve the promise chain. This is for things like email auth
|
|
|
|
// which display a "check your email" message and relies on the
|
|
|
|
// link in the email to actually register you.
|
2015-11-19 15:19:30 +00:00
|
|
|
console.log("Waiting for external action.");
|
2015-11-19 11:41:32 +00:00
|
|
|
return q.defer().promise;
|
|
|
|
}
|
2015-11-18 17:13:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseFlow(flows) {
|
|
|
|
// If the user gave us an email then we want to pick an email
|
|
|
|
// flow we can do, else any other flow.
|
|
|
|
var emailFlow = null;
|
|
|
|
var otherFlow = null;
|
|
|
|
flows.forEach(function(flow) {
|
|
|
|
var flowHasEmail = false;
|
|
|
|
for (var stageI = 0; stageI < flow.stages.length; ++stageI) {
|
|
|
|
var stage = flow.stages[stageI];
|
|
|
|
|
|
|
|
if (!SignupStages[stage]) {
|
|
|
|
// we can't do this flow, don't have a Stage impl.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stage === EMAIL_STAGE_TYPE) {
|
|
|
|
flowHasEmail = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flowHasEmail) {
|
|
|
|
emailFlow = flow;
|
|
|
|
} else {
|
|
|
|
otherFlow = flow;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.email || this.hasCompletedStage(EMAIL_STAGE_TYPE)) {
|
|
|
|
// we've been given an email or we've already done an email part
|
|
|
|
return emailFlow;
|
|
|
|
} else {
|
|
|
|
return otherFlow;
|
|
|
|
}
|
2015-11-17 17:25:14 +00:00
|
|
|
}
|
2015-11-18 17:43:38 +00:00
|
|
|
|
2015-11-19 14:16:49 +00:00
|
|
|
recheckState() {
|
|
|
|
// We've been given a bunch of data from a previous register step,
|
|
|
|
// this only happens for email auth currently. It's kinda ming we need
|
|
|
|
// to know this though. A better solution would be to ask the stages if
|
|
|
|
// they are ready to do something rather than accepting that we know about
|
|
|
|
// email auth and its internals.
|
|
|
|
this.params.hasEmailInfo = (
|
|
|
|
this.params.clientSecret && this.params.sessionId && this.params.idSid
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.params.hasEmailInfo) {
|
2016-08-11 12:50:38 +00:00
|
|
|
const client = this._createTemporaryClient();
|
|
|
|
this.registrationPromise = this._startStage(client, EMAIL_STAGE_TYPE);
|
2015-11-19 14:16:49 +00:00
|
|
|
}
|
2015-11-19 15:19:30 +00:00
|
|
|
return this.registrationPromise;
|
2015-11-19 14:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:43:38 +00:00
|
|
|
tellStage(stageName, data) {
|
|
|
|
if (this.activeStage && this.activeStage.type === stageName) {
|
|
|
|
console.log("Telling stage %s about something..", stageName);
|
|
|
|
this.activeStage.onReceiveData(data);
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 17:25:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:13:43 +00:00
|
|
|
|
2015-11-17 17:25:14 +00:00
|
|
|
class Login extends Signup {
|
2016-05-27 13:57:43 +00:00
|
|
|
constructor(hsUrl, isUrl, fallbackHsUrl) {
|
2015-11-17 17:25:14 +00:00
|
|
|
super(hsUrl, isUrl);
|
2016-05-27 13:57:43 +00:00
|
|
|
this._fallbackHsUrl = fallbackHsUrl;
|
2015-11-12 11:54:35 +00:00
|
|
|
this._currentFlowIndex = 0;
|
|
|
|
this._flows = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getFlows() {
|
|
|
|
var self = this;
|
2016-08-11 12:50:38 +00:00
|
|
|
var client = this._createTemporaryClient();
|
|
|
|
return client.loginFlows().then(function(result) {
|
2015-11-12 11:54:35 +00:00
|
|
|
self._flows = result.flows;
|
|
|
|
self._currentFlowIndex = 0;
|
|
|
|
// technically the UI should display options for all flows for the
|
|
|
|
// user to then choose one, so return all the flows here.
|
|
|
|
return self._flows;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseFlow(flowIndex) {
|
|
|
|
this._currentFlowIndex = flowIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentFlowStep() {
|
|
|
|
// technically the flow can have multiple steps, but no one does this
|
|
|
|
// for login so we can ignore it.
|
|
|
|
var flowStep = this._flows[this._currentFlowIndex];
|
|
|
|
return flowStep ? flowStep.type : null;
|
|
|
|
}
|
2015-11-12 15:15:00 +00:00
|
|
|
|
2016-08-11 12:05:16 +00:00
|
|
|
loginAsGuest() {
|
2016-08-11 12:50:38 +00:00
|
|
|
var client = this._createTemporaryClient();
|
|
|
|
return client.registerGuest().then((creds) => {
|
2016-08-11 12:05:16 +00:00
|
|
|
return {
|
|
|
|
userId: creds.user_id,
|
|
|
|
accessToken: creds.access_token,
|
|
|
|
homeserverUrl: this._hsUrl,
|
|
|
|
identityServerUrl: this._isUrl,
|
|
|
|
guest: true
|
|
|
|
};
|
|
|
|
}, (error) => {
|
|
|
|
if (error.httpStatus === 403) {
|
|
|
|
error.friendlyText = "Guest access is disabled on this Home Server.";
|
|
|
|
} else {
|
|
|
|
error.friendlyText = "Failed to register as guest: " + error.data;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-12 15:15:00 +00:00
|
|
|
loginViaPassword(username, pass) {
|
|
|
|
var self = this;
|
|
|
|
var isEmail = username.indexOf("@") > 0;
|
|
|
|
var loginParams = {
|
|
|
|
password: pass
|
|
|
|
};
|
|
|
|
if (isEmail) {
|
|
|
|
loginParams.medium = 'email';
|
|
|
|
loginParams.address = username;
|
|
|
|
} else {
|
|
|
|
loginParams.user = username;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:50:38 +00:00
|
|
|
var client = this._createTemporaryClient();
|
|
|
|
return client.login('m.login.password', loginParams).then(function(data) {
|
2015-11-12 15:15:00 +00:00
|
|
|
return q({
|
|
|
|
homeserverUrl: self._hsUrl,
|
|
|
|
identityServerUrl: self._isUrl,
|
|
|
|
userId: data.user_id,
|
|
|
|
accessToken: data.access_token
|
|
|
|
});
|
|
|
|
}, function(error) {
|
|
|
|
if (error.httpStatus == 400 && loginParams.medium) {
|
|
|
|
error.friendlyText = (
|
|
|
|
'This Home Server does not support login using email address.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (error.httpStatus === 403) {
|
|
|
|
error.friendlyText = (
|
|
|
|
'Incorrect username and/or password.'
|
|
|
|
);
|
2016-05-27 13:57:43 +00:00
|
|
|
if (self._fallbackHsUrl) {
|
2016-08-11 12:50:38 +00:00
|
|
|
var fbClient = Matrix.createClient({
|
|
|
|
baseUrl: self._fallbackHsUrl,
|
|
|
|
idBaseUrl: this._isUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
return fbClient.login('m.login.password', loginParams).then(function(data) {
|
2016-05-27 13:57:43 +00:00
|
|
|
return q({
|
|
|
|
homeserverUrl: self._fallbackHsUrl,
|
|
|
|
identityServerUrl: self._isUrl,
|
|
|
|
userId: data.user_id,
|
|
|
|
accessToken: data.access_token
|
|
|
|
});
|
|
|
|
}, function(fallback_error) {
|
|
|
|
// throw the original error
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
2015-11-12 15:15:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
error.friendlyText = (
|
|
|
|
'There was a problem logging in. (HTTP ' + error.httpStatus + ")"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
2015-11-12 11:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.Register = Register;
|
|
|
|
module.exports.Login = Login;
|