From d241f5b3eb6f9f9bbd269e9cd5db146f51c3cf4a Mon Sep 17 00:00:00 2001 From: Steven Hammerton Date: Thu, 5 Nov 2015 14:51:23 +0000 Subject: [PATCH 1/4] Add login with token method --- lib/client.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/client.js b/lib/client.js index cf67155c7..5d11df353 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1746,6 +1746,18 @@ MatrixClient.prototype.loginWithCas = function(ticket, service, callback) { }, callback); }; +/** + * @param {string} token Login token previously received from homeserver + * @param {module:client.callback} callback Optional. + * @return {module:client.Promise} Resolves: TODO + * @return {module:http-api.MatrixError} Rejects: with an error response. + */ +MatrixClient.prototype.loginWithToken = function(token, callback) { + return this.login("m.login.token", { + token: token + }, callback); +}; + // Push operations // =============== From c3097979f282e03da512326f9f5dd69ad46f9e7e Mon Sep 17 00:00:00 2001 From: Steven Hammerton Date: Thu, 5 Nov 2015 15:19:04 +0000 Subject: [PATCH 2/4] Change login with CAS to redirect to HS for CAS login --- lib/client.js | 26 +++++--------------------- lib/http-api.js | 9 +++++++++ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/client.js b/lib/client.js index 5d11df353..685e3d8ca 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1722,28 +1722,12 @@ MatrixClient.prototype.loginWithSAML2 = function(relayState, callback) { }; /** - * @param {module:client.callback} callback Optional. - * @return {module:client.Promise} Resolves: TODO - * @return {module:http-api.MatrixError} Rejects: with an error response. + * @param {string} redirectUrl URL to which to be redirect to after + * HS authenticates with CAS and issues login token + * Will redirect to homeserver to complete CAS login */ -MatrixClient.prototype.getCasServer = function(callback) { - return this._http.request( - callback, "GET", "/login/cas", undefined, undefined - ); -}; - -/** - * @param {string} ticket (Received from CAS) - * @param {string} service Service to which the token was granted - * @param {module:client.callback} callback Optional. - * @return {module:client.Promise} Resolves: TODO - * @return {module:http-api.MatrixError} Rejects: with an error response. - */ -MatrixClient.prototype.loginWithCas = function(ticket, service, callback) { - return this.login("m.login.cas", { - ticket: ticket, - service: service - }, callback); +MatrixClient.prototype.loginWithCas = function(redirectUrl) { + this._http.redirect("/login/cas/redirect", {"redirectUrl": redirectUrl}, httpApi.PREFIX_V1); }; /** diff --git a/lib/http-api.js b/lib/http-api.js index 09b3d4d38..03c498691 100644 --- a/lib/http-api.js +++ b/lib/http-api.js @@ -296,6 +296,15 @@ module.exports.MatrixHttpApi.prototype = { return this._request(callback, method, fullUri, queryParams, data); }, + redirect: function(path, queryParams, prefix) { + var queryString = ""; + if (queryParams) { + queryString = "?" + utils.encodeParams(queryParams); + } + var fullUri = this.opts.baseUrl + prefix + path + queryString; + window.location.href = fullUri; + }, + _request: function(callback, method, uri, queryParams, data) { if (callback !== undefined && !utils.isFunction(callback)) { throw Error( From b963f177cc7422fdf7e710dd5ba2c5852ba9f04a Mon Sep 17 00:00:00 2001 From: Steven Hammerton Date: Fri, 6 Nov 2015 12:11:50 +0000 Subject: [PATCH 3/4] Update CAS login to return url rather than update location as the JS SDK may not be run within a browser env --- lib/client.js | 10 ++++++---- lib/http-api.js | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/client.js b/lib/client.js index 685e3d8ca..db2cc57a2 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1722,12 +1722,14 @@ MatrixClient.prototype.loginWithSAML2 = function(relayState, callback) { }; /** - * @param {string} redirectUrl URL to which to be redirect to after + * @param {string} redirectUrl URL to which to be redirected to after * HS authenticates with CAS and issues login token - * Will redirect to homeserver to complete CAS login + * @return {string} CAS login URL */ -MatrixClient.prototype.loginWithCas = function(redirectUrl) { - this._http.redirect("/login/cas/redirect", {"redirectUrl": redirectUrl}, httpApi.PREFIX_V1); +MatrixClient.prototype.getCasLoginUrl = function(redirectUrl) { + return this._http.getUrl("/login/cas/redirect", { + "redirectUrl": redirectUrl + }, httpApi.PREFIX_V1); }; /** diff --git a/lib/http-api.js b/lib/http-api.js index 03c498691..797daa426 100644 --- a/lib/http-api.js +++ b/lib/http-api.js @@ -296,13 +296,23 @@ module.exports.MatrixHttpApi.prototype = { return this._request(callback, method, fullUri, queryParams, data); }, - redirect: function(path, queryParams, prefix) { + /** + * Form and return a homeserver request URL based on the given path + * params and prefix. + * @param {string} path The HTTP path after the supplied prefix e.g. + * "/createRoom". + * @param {Object} queryParams A dict of query params (these will NOT be + * urlencoded). + * @param {string} prefix The full prefix to use e.g. + * "/_matrix/client/v2_alpha". + * @return {string} URL + */ + getUrl: function(path, queryParams, prefix) { var queryString = ""; if (queryParams) { queryString = "?" + utils.encodeParams(queryParams); } - var fullUri = this.opts.baseUrl + prefix + path + queryString; - window.location.href = fullUri; + return this.opts.baseUrl + prefix + path + queryString; }, _request: function(callback, method, uri, queryParams, data) { From e71a87c62ccf32af1860156fe1757533d6c3e18e Mon Sep 17 00:00:00 2001 From: Steven Hammerton Date: Fri, 6 Nov 2015 12:14:24 +0000 Subject: [PATCH 4/4] Update javadoc --- lib/client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index db2cc57a2..98fdfc04e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1722,9 +1722,9 @@ MatrixClient.prototype.loginWithSAML2 = function(relayState, callback) { }; /** - * @param {string} redirectUrl URL to which to be redirected to after - * HS authenticates with CAS and issues login token - * @return {string} CAS login URL + * @param {string} redirectUrl The URL to redirect to after the HS + * authenticates with CAS. + * @return {string} The HS URL to hit to begin the CAS login process. */ MatrixClient.prototype.getCasLoginUrl = function(redirectUrl) { return this._http.getUrl("/login/cas/redirect", {