Minor refactoring; remove debug logging; add comments

This commit is contained in:
Kegan Dougal 2015-11-19 16:07:58 +00:00
parent cc74676718
commit b12f0f1df7
2 changed files with 48 additions and 27 deletions

View file

@ -6,6 +6,10 @@ var q = require("q");
const EMAIL_STAGE_TYPE = "m.login.email.identity"; const EMAIL_STAGE_TYPE = "m.login.email.identity";
/**
* A base class for common functionality between Registration and Login e.g.
* storage of HS/IS URLs.
*/
class Signup { class Signup {
constructor(hsUrl, isUrl) { constructor(hsUrl, isUrl) {
this._hsUrl = hsUrl; this._hsUrl = hsUrl;
@ -29,13 +33,16 @@ class Signup {
} }
} }
/**
* Registration logic class
*/
class Register extends Signup { class Register extends Signup {
constructor(hsUrl, isUrl) { constructor(hsUrl, isUrl) {
super(hsUrl, isUrl); super(hsUrl, isUrl);
this.setStep("START"); this.setStep("START");
this.data = null; // from the server this.data = null; // from the server
this.params = {}; // random other stuff (e.g. query params, NOT params from the server) // random other stuff (e.g. query params, NOT params from the server)
this.params = {};
this.credentials = null; this.credentials = null;
this.activeStage = null; this.activeStage = null;
this.registrationPromise = null; this.registrationPromise = null;
@ -104,12 +111,12 @@ class Register extends Signup {
this._hsUrl, this._hsUrl,
this._isUrl this._isUrl
); );
console.log("register(formVals)"); console.log("Starting registration process (form submission)");
return this._tryRegister(); return this._tryRegister();
} }
_tryRegister(authDict) { _tryRegister(authDict) {
console.log("_tryRegister %s", JSON.stringify(authDict)); console.log("Trying to register with auth dict: %s", JSON.stringify(authDict));
var self = this; var self = this;
return MatrixClientPeg.get().register( return MatrixClientPeg.get().register(
this.username, this.password, this.params.sessionId, authDict this.username, this.password, this.params.sessionId, authDict
@ -163,6 +170,11 @@ class Register extends Signup {
} }
} }
hasCompletedStage(stageType) {
var completed = (this.data || {}).completed || [];
return completed.indexOf(stageType) !== -1;
}
startStage(stageName) { startStage(stageName) {
var self = this; var self = this;
this.setStep(`STEP_${stageName}`); this.setStep(`STEP_${stageName}`);
@ -175,8 +187,8 @@ class Register extends Signup {
var stage = new StageClass(MatrixClientPeg.get(), this); var stage = new StageClass(MatrixClientPeg.get(), this);
this.activeStage = stage; this.activeStage = stage;
return stage.complete().then(function(request) { return stage.complete().then(function(request) {
console.log("Stage %s completed with %s", stageName, JSON.stringify(request));
if (request.auth) { if (request.auth) {
console.log("Stage %s is returning an auth dict", stageName);
return self._tryRegister(request.auth); return self._tryRegister(request.auth);
} }
else { else {
@ -189,11 +201,6 @@ class Register extends Signup {
}); });
} }
hasCompletedStage(stageType) {
var completed = (this.data || {}).completed || [];
return completed.indexOf(stageType) !== -1;
}
chooseFlow(flows) { chooseFlow(flows) {
// If the user gave us an email then we want to pick an email // If the user gave us an email then we want to pick an email
// flow we can do, else any other flow. // flow we can do, else any other flow.
@ -248,7 +255,6 @@ class Register extends Signup {
); );
if (this.params.hasEmailInfo) { if (this.params.hasEmailInfo) {
console.log("recheckState has email info.. starting email info..");
this.registrationPromise = this.startStage(EMAIL_STAGE_TYPE); this.registrationPromise = this.startStage(EMAIL_STAGE_TYPE);
} }
return this.registrationPromise; return this.registrationPromise;

View file

@ -1,6 +1,9 @@
"use strict"; "use strict";
var q = require("q"); var q = require("q");
/**
* An interface class which login types should abide by.
*/
class Stage { class Stage {
constructor(type, matrixClient, signupInstance) { constructor(type, matrixClient, signupInstance) {
this.type = type; this.type = type;
@ -24,6 +27,9 @@ class Stage {
Stage.TYPE = "NOT IMPLEMENTED"; Stage.TYPE = "NOT IMPLEMENTED";
/**
* This stage requires no auth.
*/
class DummyStage extends Stage { class DummyStage extends Stage {
constructor(matrixClient, signupInstance) { constructor(matrixClient, signupInstance) {
super(DummyStage.TYPE, matrixClient, signupInstance); super(DummyStage.TYPE, matrixClient, signupInstance);
@ -40,17 +46,24 @@ class DummyStage extends Stage {
DummyStage.TYPE = "m.login.dummy"; DummyStage.TYPE = "m.login.dummy";
/**
* This stage uses Google's Recaptcha to do auth.
*/
class RecaptchaStage extends Stage { class RecaptchaStage extends Stage {
constructor(matrixClient, signupInstance) { constructor(matrixClient, signupInstance) {
super(RecaptchaStage.TYPE, matrixClient, signupInstance); super(RecaptchaStage.TYPE, matrixClient, signupInstance);
this.defer = q.defer(); this.defer = q.defer(); // resolved with the captcha response
this.publicKey = null; this.publicKey = null; // from the HS
this.divId = null; // from the UI component
} }
// called when the UI component has loaded the recaptcha <div> so we can
// render to it.
onReceiveData(data) { onReceiveData(data) {
if (data !== "loaded") { if (!data || !data.divId) {
return; return;
} }
this.divId = data.divId;
this._attemptRender(); this._attemptRender();
} }
@ -81,9 +94,13 @@ class RecaptchaStage extends Stage {
console.error("No public key for recaptcha!"); console.error("No public key for recaptcha!");
return; return;
} }
if (!this.divId) {
console.error("No div ID specified!");
return;
}
console.log("Rendering to %s", this.divId);
var self = this; var self = this;
// FIXME: Tight coupling here and in CaptchaForm.js global.grecaptcha.render(this.divId, {
global.grecaptcha.render('mx_recaptcha', {
sitekey: this.publicKey, sitekey: this.publicKey,
callback: function(response) { callback: function(response) {
console.log("Received captcha response"); console.log("Received captcha response");
@ -100,13 +117,16 @@ class RecaptchaStage extends Stage {
RecaptchaStage.TYPE = "m.login.recaptcha"; RecaptchaStage.TYPE = "m.login.recaptcha";
/**
* This state uses the IS to verify email addresses.
*/
class EmailIdentityStage extends Stage { class EmailIdentityStage extends Stage {
constructor(matrixClient, signupInstance) { constructor(matrixClient, signupInstance) {
super(EmailIdentityStage.TYPE, matrixClient, signupInstance); super(EmailIdentityStage.TYPE, matrixClient, signupInstance);
} }
_completeVerify() { _completeVerify() {
console.log("_completeVerify"); // pull out the host of the IS URL by creating an anchor element
var isLocation = document.createElement('a'); var isLocation = document.createElement('a');
isLocation.href = this.signupInstance.getIdentityServerUrl(); isLocation.href = this.signupInstance.getIdentityServerUrl();
@ -130,20 +150,15 @@ class EmailIdentityStage extends Stage {
* 2) When validating query parameters received from the link in the email * 2) When validating query parameters received from the link in the email
*/ */
complete() { complete() {
console.log("Email complete()"); // TODO: The Registration class shouldn't really know this info.
if (this.signupInstance.params.hasEmailInfo) { if (this.signupInstance.params.hasEmailInfo) {
return this._completeVerify(); return this._completeVerify();
} }
var config = { var clientSecret = this.client.generateClientSecret();
clientSecret: this.client.generateClientSecret(),
sendAttempt: 1
};
this.signupInstance.params[EmailIdentityStage.TYPE] = config;
var nextLink = this.signupInstance.params.registrationUrl + var nextLink = this.signupInstance.params.registrationUrl +
'?client_secret=' + '?client_secret=' +
encodeURIComponent(config.clientSecret) + encodeURIComponent(clientSecret) +
"&hs_url=" + "&hs_url=" +
encodeURIComponent(this.signupInstance.getHomeserverUrl()) + encodeURIComponent(this.signupInstance.getHomeserverUrl()) +
"&is_url=" + "&is_url=" +
@ -153,8 +168,8 @@ class EmailIdentityStage extends Stage {
return this.client.requestEmailToken( return this.client.requestEmailToken(
this.signupInstance.email, this.signupInstance.email,
config.clientSecret, clientSecret,
config.sendAttempt, 1, // TODO: Multiple send attempts?
nextLink nextLink
).then(function(response) { ).then(function(response) {
return {}; // don't want to make a request return {}; // don't want to make a request