2015-11-18 17:13:43 +00:00
|
|
|
"use strict";
|
|
|
|
var q = require("q");
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
/**
|
|
|
|
* An interface class which login types should abide by.
|
|
|
|
*/
|
2015-11-18 17:13:43 +00:00
|
|
|
class Stage {
|
|
|
|
constructor(type, matrixClient, signupInstance) {
|
|
|
|
this.type = type;
|
|
|
|
this.client = matrixClient;
|
|
|
|
this.signupInstance = signupInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
complete() {
|
|
|
|
// Return a promise which is:
|
|
|
|
// RESOLVED => With an Object which has an 'auth' key which is the auth dict
|
|
|
|
// to submit.
|
|
|
|
// REJECTED => With an Error if there was a problem with this stage.
|
|
|
|
// Has a "message" string and an "isFatal" flag.
|
|
|
|
return q.reject("NOT IMPLEMENTED");
|
|
|
|
}
|
2015-11-18 17:43:38 +00:00
|
|
|
|
|
|
|
onReceiveData() {
|
|
|
|
// NOP
|
|
|
|
}
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
Stage.TYPE = "NOT IMPLEMENTED";
|
|
|
|
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
/**
|
|
|
|
* This stage requires no auth.
|
|
|
|
*/
|
2015-11-18 17:13:43 +00:00
|
|
|
class DummyStage extends Stage {
|
|
|
|
constructor(matrixClient, signupInstance) {
|
|
|
|
super(DummyStage.TYPE, matrixClient, signupInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
complete() {
|
|
|
|
return q({
|
|
|
|
auth: {
|
|
|
|
type: DummyStage.TYPE
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DummyStage.TYPE = "m.login.dummy";
|
|
|
|
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
/**
|
|
|
|
* This stage uses Google's Recaptcha to do auth.
|
|
|
|
*/
|
2015-11-18 17:13:43 +00:00
|
|
|
class RecaptchaStage extends Stage {
|
|
|
|
constructor(matrixClient, signupInstance) {
|
|
|
|
super(RecaptchaStage.TYPE, matrixClient, signupInstance);
|
2015-11-19 16:07:58 +00:00
|
|
|
this.defer = q.defer(); // resolved with the captcha response
|
|
|
|
this.publicKey = null; // from the HS
|
|
|
|
this.divId = null; // from the UI component
|
2015-11-18 17:43:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
// called when the UI component has loaded the recaptcha <div> so we can
|
|
|
|
// render to it.
|
2015-11-18 17:43:38 +00:00
|
|
|
onReceiveData(data) {
|
2015-11-19 16:07:58 +00:00
|
|
|
if (!data || !data.divId) {
|
2015-11-18 17:43:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-19 16:07:58 +00:00
|
|
|
this.divId = data.divId;
|
2015-11-18 17:43:38 +00:00
|
|
|
this._attemptRender();
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete() {
|
|
|
|
var publicKey;
|
2015-11-18 17:43:38 +00:00
|
|
|
var serverParams = this.signupInstance.getServerData().params;
|
|
|
|
if (serverParams && serverParams["m.login.recaptcha"]) {
|
|
|
|
publicKey = serverParams["m.login.recaptcha"].public_key;
|
2015-11-18 17:13:43 +00:00
|
|
|
}
|
|
|
|
if (!publicKey) {
|
|
|
|
return q.reject({
|
|
|
|
message: "This server has not supplied enough information for Recaptcha " +
|
|
|
|
"authentication",
|
|
|
|
isFatal: true
|
|
|
|
});
|
|
|
|
}
|
2015-11-18 17:43:38 +00:00
|
|
|
this.publicKey = publicKey;
|
|
|
|
this._attemptRender();
|
|
|
|
return this.defer.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
_attemptRender() {
|
|
|
|
if (!global.grecaptcha) {
|
|
|
|
console.error("grecaptcha not loaded!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.publicKey) {
|
|
|
|
console.error("No public key for recaptcha!");
|
|
|
|
return;
|
|
|
|
}
|
2015-11-19 16:07:58 +00:00
|
|
|
if (!this.divId) {
|
|
|
|
console.error("No div ID specified!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Rendering to %s", this.divId);
|
2015-11-18 17:43:38 +00:00
|
|
|
var self = this;
|
2015-11-19 16:07:58 +00:00
|
|
|
global.grecaptcha.render(this.divId, {
|
2015-11-18 17:43:38 +00:00
|
|
|
sitekey: this.publicKey,
|
2015-11-18 17:13:43 +00:00
|
|
|
callback: function(response) {
|
2015-11-19 15:19:30 +00:00
|
|
|
console.log("Received captcha response");
|
|
|
|
self.defer.resolve({
|
2015-11-18 17:13:43 +00:00
|
|
|
auth: {
|
|
|
|
type: 'm.login.recaptcha',
|
|
|
|
response: response
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RecaptchaStage.TYPE = "m.login.recaptcha";
|
|
|
|
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
/**
|
|
|
|
* This state uses the IS to verify email addresses.
|
|
|
|
*/
|
2015-11-18 17:13:43 +00:00
|
|
|
class EmailIdentityStage extends Stage {
|
|
|
|
constructor(matrixClient, signupInstance) {
|
|
|
|
super(EmailIdentityStage.TYPE, matrixClient, signupInstance);
|
|
|
|
}
|
|
|
|
|
2015-11-19 14:16:49 +00:00
|
|
|
_completeVerify() {
|
2015-11-19 16:07:58 +00:00
|
|
|
// pull out the host of the IS URL by creating an anchor element
|
2015-11-19 14:16:49 +00:00
|
|
|
var isLocation = document.createElement('a');
|
|
|
|
isLocation.href = this.signupInstance.getIdentityServerUrl();
|
|
|
|
|
|
|
|
return q({
|
|
|
|
auth: {
|
|
|
|
type: 'm.login.email.identity',
|
|
|
|
threepid_creds: {
|
|
|
|
sid: this.signupInstance.params.idSid,
|
|
|
|
client_secret: this.signupInstance.params.clientSecret,
|
|
|
|
id_server: isLocation.host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Complete the email stage.
|
|
|
|
*
|
|
|
|
* This is called twice under different circumstances:
|
|
|
|
* 1) When requesting an email token from the IS
|
|
|
|
* 2) When validating query parameters received from the link in the email
|
|
|
|
*/
|
2015-11-18 17:13:43 +00:00
|
|
|
complete() {
|
2015-11-19 16:07:58 +00:00
|
|
|
// TODO: The Registration class shouldn't really know this info.
|
2015-11-19 14:16:49 +00:00
|
|
|
if (this.signupInstance.params.hasEmailInfo) {
|
|
|
|
return this._completeVerify();
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:07:58 +00:00
|
|
|
var clientSecret = this.client.generateClientSecret();
|
2015-11-18 17:13:43 +00:00
|
|
|
var nextLink = this.signupInstance.params.registrationUrl +
|
|
|
|
'?client_secret=' +
|
2015-11-19 16:07:58 +00:00
|
|
|
encodeURIComponent(clientSecret) +
|
2015-11-18 17:13:43 +00:00
|
|
|
"&hs_url=" +
|
|
|
|
encodeURIComponent(this.signupInstance.getHomeserverUrl()) +
|
|
|
|
"&is_url=" +
|
|
|
|
encodeURIComponent(this.signupInstance.getIdentityServerUrl()) +
|
|
|
|
"&session_id=" +
|
2015-11-19 13:58:34 +00:00
|
|
|
encodeURIComponent(this.signupInstance.getServerData().session);
|
2015-11-18 17:13:43 +00:00
|
|
|
|
|
|
|
return this.client.requestEmailToken(
|
|
|
|
this.signupInstance.email,
|
2015-11-19 16:07:58 +00:00
|
|
|
clientSecret,
|
|
|
|
1, // TODO: Multiple send attempts?
|
2015-11-18 17:13:43 +00:00
|
|
|
nextLink
|
|
|
|
).then(function(response) {
|
|
|
|
return {}; // don't want to make a request
|
|
|
|
}, function(error) {
|
|
|
|
console.error(error);
|
|
|
|
var e = {
|
|
|
|
isFatal: true
|
|
|
|
};
|
|
|
|
if (error.errcode == 'THREEPID_IN_USE') {
|
|
|
|
e.message = "Email in use";
|
|
|
|
} else {
|
|
|
|
e.message = 'Unable to contact the given identity server';
|
|
|
|
}
|
2015-11-20 10:37:46 +00:00
|
|
|
throw e;
|
2015-11-18 17:13:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EmailIdentityStage.TYPE = "m.login.email.identity";
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
[DummyStage.TYPE]: DummyStage,
|
|
|
|
[RecaptchaStage.TYPE]: RecaptchaStage,
|
|
|
|
[EmailIdentityStage.TYPE]: EmailIdentityStage
|
|
|
|
};
|