diff --git a/lib/client.js b/lib/client.js index d269e7ea1..94b018ef8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -45,7 +45,7 @@ var utils = require("./utils"); function MatrixClient(opts) { utils.checkObjectHasKeys(opts, ["baseUrl", "request"]); utils.checkObjectHasNoAdditionalKeys(opts, - ["baseUrl", "request", "accessToken", "userId", "store", "scheduler"] + ["baseUrl", "idBaseUrl", "request", "accessToken", "userId", "store", "scheduler"] ); this.store = opts.store || new StubStore(); @@ -61,6 +61,7 @@ function MatrixClient(opts) { var httpOpts = { baseUrl: opts.baseUrl, + idBaseUrl: opts.idBaseUrl, accessToken: opts.accessToken, request: opts.request, prefix: httpApi.PREFIX_V1, @@ -1381,6 +1382,42 @@ function _PojoToMatrixEventMapper(plainOldJsObject) { return new MatrixEvent(plainOldJsObject); } +// Identity Server Operations +// ========================== + +/** + * @param {string} client_secret + * @param {string} email + * @param {string} send_attempt + * @param {module:client.callback} callback Optional. + * @return {module:client.Promise} Resolves: TODO + * @return {module:http-api.MatrixError} Rejects: with an error response. + */ +MatrixClient.prototype.requestEmailToken = function(email, client_secret, send_attempt, callback) { + var params = { + client_secret: client_secret, + email: email, + send_attempt: send_attempt + }; + return this._http.idServerRequest( + callback, "POST", "/validate/email/requestToken", params, httpApi.PREFIX_IDENTITY_V1 + ); +}; + +/** + * Generates a random string suitable for use as a client secret + */ +MatrixClient.prototype.generateClientSecret = function() { + var ret = ""; + var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + for (var i = 0; i < 32; i++) { + ret += chars.charAt(Math.floor(Math.random() * chars.length)); + } + + return ret; +}; + /** */ module.exports.MatrixClient = MatrixClient; diff --git a/lib/http-api.js b/lib/http-api.js index 3aa553157..100577d71 100644 --- a/lib/http-api.js +++ b/lib/http-api.js @@ -23,6 +23,11 @@ module.exports.PREFIX_V1 = "/_matrix/client/api/v1"; */ module.exports.PREFIX_V2_ALPHA = "/_matrix/client/v2_alpha"; +/** + * URI path for the identity API + */ +module.exports.PREFIX_IDENTITY_V1 = "/_matrix/identity/api/v1"; + /** * Construct a MatrixHttpApi. * @constructor @@ -216,6 +221,36 @@ module.exports.MatrixHttpApi.prototype = { return defer.promise; }, + idServerRequest: function(callback, method, path, params, prefix) { + var fullUri = this.opts.idBaseUrl + prefix + path; + + if (callback !== undefined && !utils.isFunction(callback)) { + throw Error( + "Expected callback to be a function but got " + typeof callback + ); + } + + var opts = { + uri: fullUri, + method: method, + withCredentials: false, + json: false, + _matrix_opts: this.opts + }; + if (method == 'GET') { + opts.qs = params; + } else { + opts.form = params; + } + + var defer = q.defer(); + this.opts.request( + opts, + requestCallback(defer, callback, this.opts.onlyData) + ); + return defer.promise; + }, + /** * Perform an authorised request to the homeserver. * @param {Function} callback Optional. The callback to invoke on