2015-11-18 18:13:43 +01:00
|
|
|
"use strict";
|
|
|
|
var q = require("q");
|
|
|
|
|
2015-11-19 17:07:58 +01:00
|
|
|
/**
|
|
|
|
* An interface class which login types should abide by.
|
|
|
|
*/
|
2015-11-18 18:13:43 +01: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 18:43:38 +01:00
|
|
|
|
|
|
|
onReceiveData() {
|
|
|
|
// NOP
|
|
|
|
}
|
2015-11-18 18:13:43 +01:00
|
|
|
}
|
|
|
|
Stage.TYPE = "NOT IMPLEMENTED";
|
|
|
|
|
|
|
|
|
2015-11-19 17:07:58 +01:00
|
|
|
/**
|
|
|
|
* This stage requires no auth.
|
|
|
|
*/
|
2015-11-18 18:13:43 +01: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 17:07:58 +01:00
|
|
|
/**
|
|
|
|
* This stage uses Google's Recaptcha to do auth.
|
|
|
|
*/
|
2015-11-18 18:13:43 +01:00
|
|
|
class RecaptchaStage extends Stage {
|
|
|
|
constructor(matrixClient, signupInstance) {
|
|
|
|
super(RecaptchaStage.TYPE, matrixClient, signupInstance);
|
2015-11-19 17:07:58 +01:00
|
|
|
this.defer = q.defer(); // resolved with the captcha response
|
2015-11-18 18:43:38 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 19:00:47 +02:00
|
|
|
// called when the recaptcha has been completed.
|
2015-11-18 18:43:38 +01:00
|
|
|
onReceiveData(data) {
|
2016-10-11 19:00:47 +02:00
|
|
|
if (!data || !data.response) {
|
2015-11-18 18:43:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-10-11 19:00:47 +02:00
|
|
|
this.defer.resolve({
|
|
|
|
auth: {
|
|
|
|
type: 'm.login.recaptcha',
|
|
|
|
response: data.response,
|
|
|
|
}
|
|
|
|
});
|
2015-11-18 18:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
complete() {
|
2015-11-18 18:43:38 +01:00
|
|
|
return this.defer.promise;
|
2015-11-18 18:13:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RecaptchaStage.TYPE = "m.login.recaptcha";
|
|
|
|
|
|
|
|
|
2015-11-19 17:07:58 +01:00
|
|
|
/**
|
|
|
|
* This state uses the IS to verify email addresses.
|
|
|
|
*/
|
2015-11-18 18:13:43 +01:00
|
|
|
class EmailIdentityStage extends Stage {
|
|
|
|
constructor(matrixClient, signupInstance) {
|
|
|
|
super(EmailIdentityStage.TYPE, matrixClient, signupInstance);
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:16:49 +01:00
|
|
|
_completeVerify() {
|
2015-11-19 17:07:58 +01:00
|
|
|
// pull out the host of the IS URL by creating an anchor element
|
2015-11-19 15:16:49 +01:00
|
|
|
var isLocation = document.createElement('a');
|
|
|
|
isLocation.href = this.signupInstance.getIdentityServerUrl();
|
|
|
|
|
2016-03-16 20:42:52 +01:00
|
|
|
var clientSecret = this.clientSecret || this.signupInstance.params.clientSecret;
|
|
|
|
var sid = this.sid || this.signupInstance.params.idSid;
|
|
|
|
|
2015-11-19 15:16:49 +01:00
|
|
|
return q({
|
|
|
|
auth: {
|
|
|
|
type: 'm.login.email.identity',
|
|
|
|
threepid_creds: {
|
2016-03-16 20:42:52 +01:00
|
|
|
sid: sid,
|
|
|
|
client_secret: clientSecret,
|
2015-11-19 15:16:49 +01:00
|
|
|
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 18:13:43 +01:00
|
|
|
complete() {
|
2015-11-19 17:07:58 +01:00
|
|
|
// TODO: The Registration class shouldn't really know this info.
|
2015-11-19 15:16:49 +01:00
|
|
|
if (this.signupInstance.params.hasEmailInfo) {
|
|
|
|
return this._completeVerify();
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:08:18 +02:00
|
|
|
this.clientSecret = this.signupInstance.params.clientSecret;
|
|
|
|
if (!this.clientSecret) {
|
|
|
|
return q.reject(new Error("No client secret specified by Signup class!"));
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:13:43 +01:00
|
|
|
var nextLink = this.signupInstance.params.registrationUrl +
|
|
|
|
'?client_secret=' +
|
2016-03-16 20:42:52 +01:00
|
|
|
encodeURIComponent(this.clientSecret) +
|
2015-11-18 18:13:43 +01:00
|
|
|
"&hs_url=" +
|
|
|
|
encodeURIComponent(this.signupInstance.getHomeserverUrl()) +
|
|
|
|
"&is_url=" +
|
|
|
|
encodeURIComponent(this.signupInstance.getIdentityServerUrl()) +
|
|
|
|
"&session_id=" +
|
2015-11-19 14:58:34 +01:00
|
|
|
encodeURIComponent(this.signupInstance.getServerData().session);
|
2015-11-18 18:13:43 +01:00
|
|
|
|
2016-03-16 20:42:52 +01:00
|
|
|
var self = this;
|
2016-07-06 16:22:06 +02:00
|
|
|
return this.client.requestRegisterEmailToken(
|
2015-11-18 18:13:43 +01:00
|
|
|
this.signupInstance.email,
|
2016-03-16 20:42:52 +01:00
|
|
|
this.clientSecret,
|
2015-11-19 17:07:58 +01:00
|
|
|
1, // TODO: Multiple send attempts?
|
2015-11-18 18:13:43 +01:00
|
|
|
nextLink
|
|
|
|
).then(function(response) {
|
2016-03-16 20:42:52 +01:00
|
|
|
self.sid = response.sid;
|
|
|
|
return self._completeVerify();
|
|
|
|
}).then(function(request) {
|
|
|
|
request.poll_for_success = true;
|
|
|
|
return request;
|
2015-11-18 18:13:43 +01:00
|
|
|
}, function(error) {
|
|
|
|
console.error(error);
|
|
|
|
var e = {
|
|
|
|
isFatal: true
|
|
|
|
};
|
2016-07-06 16:22:06 +02:00
|
|
|
if (error.errcode == 'M_THREEPID_IN_USE') {
|
|
|
|
e.message = "This email address is already registered";
|
2015-11-18 18:13:43 +01:00
|
|
|
} else {
|
|
|
|
e.message = 'Unable to contact the given identity server';
|
|
|
|
}
|
2015-11-20 11:37:46 +01:00
|
|
|
throw e;
|
2015-11-18 18:13:43 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EmailIdentityStage.TYPE = "m.login.email.identity";
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
[DummyStage.TYPE]: DummyStage,
|
|
|
|
[RecaptchaStage.TYPE]: RecaptchaStage,
|
|
|
|
[EmailIdentityStage.TYPE]: EmailIdentityStage
|
2016-03-16 20:42:52 +01:00
|
|
|
};
|