From fc61b9aca7e371a7ec66f684f823dc8906ac2bf4 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 17 Nov 2012 12:32:51 +0000 Subject: [PATCH] Added login and logout macros for TiddlyWeb --- editions/tw5tiddlyweb/tiddlers/HelloThere.tid | 12 +- plugins/tiddlywiki/tiddlyweb/loginmacro.js | 41 +++++ plugins/tiddlywiki/tiddlyweb/logoutmacro.js | 41 +++++ plugins/tiddlywiki/tiddlyweb/tiddlyweb.js | 149 ++++++++++++------ 4 files changed, 192 insertions(+), 51 deletions(-) create mode 100644 plugins/tiddlywiki/tiddlyweb/loginmacro.js create mode 100644 plugins/tiddlywiki/tiddlyweb/logoutmacro.js diff --git a/editions/tw5tiddlyweb/tiddlers/HelloThere.tid b/editions/tw5tiddlyweb/tiddlers/HelloThere.tid index 9d10e61b3..f20326c8b 100644 --- a/editions/tw5tiddlyweb/tiddlers/HelloThere.tid +++ b/editions/tw5tiddlyweb/tiddlers/HelloThere.tid @@ -2,6 +2,14 @@ title: HelloThere Experimenting with TiddlyWeb integration for TiddlyWiki5. -TiddlyWeb login status: (($:/plugins/tiddlyweb/IsLoggedIn)) +<< +Log in to TiddlyWeb: <> +>> +<< +Log out of TiddlyWeb: <> +>> + +TiddlyWeb [[login status|$:/plugins/tiddlyweb/IsLoggedIn]]: (($:/plugins/tiddlyweb/IsLoggedIn)) + +TiddlyWeb [[username|$:/plugins/tiddlyweb/UserName]]: (($:/plugins/tiddlyweb/UserName)) -TiddlyWeb username: (($:/plugins/tiddlyweb/UserName)) diff --git a/plugins/tiddlywiki/tiddlyweb/loginmacro.js b/plugins/tiddlywiki/tiddlyweb/loginmacro.js new file mode 100644 index 000000000..31513100f --- /dev/null +++ b/plugins/tiddlywiki/tiddlyweb/loginmacro.js @@ -0,0 +1,41 @@ +/*\ +title: $:/plugins/tiddlywiki/tiddlyweb/loginmacro.js +type: application/javascript +module-type: macro + +TiddlyWeb login plugin + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "tiddlyweb.login", + params: {} +}; + +exports.handleEvent = function (event) { + if(event.type === "click") { + $tw.plugins.tiddlyweb.promptLogin(); + } +}; + +exports.executeMacro = function() { + // Create the link + var child = $tw.Tree.Element( + "a", + null, + this.content, + { + events: ["click"], + eventHandler: this + } + ); + child.execute(this.parents,this.tiddlerTitle); + return child; +}; + +})(); diff --git a/plugins/tiddlywiki/tiddlyweb/logoutmacro.js b/plugins/tiddlywiki/tiddlyweb/logoutmacro.js new file mode 100644 index 000000000..4c9571cfe --- /dev/null +++ b/plugins/tiddlywiki/tiddlyweb/logoutmacro.js @@ -0,0 +1,41 @@ +/*\ +title: $:/plugins/tiddlywiki/tiddlyweb/logoutmacro.js +type: application/javascript +module-type: macro + +TiddlyWeb logout plugin + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "tiddlyweb.logout", + params: {} +}; + +exports.handleEvent = function (event) { + if(event.type === "click") { + $tw.plugins.tiddlyweb.logout(); + } +}; + +exports.executeMacro = function() { + // Create the link + var child = $tw.Tree.Element( + "a", + null, + this.content, + { + events: ["click"], + eventHandler: this + } + ); + child.execute(this.parents,this.tiddlerTitle); + return child; +}; + +})(); diff --git a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js index beed08db0..366d78d50 100644 --- a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js +++ b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js @@ -30,49 +30,7 @@ exports.startup = function() { text: "no" }); // Get the login status - $tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) { - if(!isLoggedIn) { - $tw.passwordPrompt.createPrompt({ - serviceName: "Login to TiddlySpace", - callback: function(data) { - $tw.plugins.tiddlyweb.login(data.username,data.password); - return true; // Get rid of the password prompt - } - }); - } - }); -}; - -$tw.plugins.tiddlyweb.getStatus = function(callback) { - // Get status - $tw.plugins.tiddlyweb.httpRequest({ - url: "http://tw5tiddlyweb.tiddlyspace.com/status", - callback: function(err,data) { - var json = null; - try { - json = JSON.parse(data); - } catch (e) { - } - if(json) { - var isLoggedIn = json.username !== "GUEST"; - $tw.wiki.addTiddler({ - title: $tw.plugins.tiddlyweb.titleIsLoggedIn, - text: isLoggedIn ? "yes" : "no" - }); - if(isLoggedIn) { - $tw.wiki.addTiddler({ - title: $tw.plugins.tiddlyweb.titleUserName, - text: json.username - }); - } else { - $tw.wiki.deleteTiddler($tw.plugins.tiddlyweb.titleUserName); - } - } - if(callback) { - callback(isLoggedIn,json); - } - } - }); + $tw.plugins.tiddlyweb.getStatus(); }; /* @@ -92,11 +50,76 @@ $tw.plugins.tiddlyweb.invokeTiddlyWebStartupModules = function(loggedIn) { }); }; +$tw.plugins.tiddlyweb.getCsrfToken = function() { + var regex = /^(?:.*; )?csrf_token=([^(;|$)]*)(?:;|$)/, + match = regex.exec(document.cookie), + csrf = null; + if (match && (match.length === 2)) { + csrf = match[1]; + } + return csrf; +}; + +$tw.plugins.tiddlyweb.getStatus = function(callback) { + // Get status + $tw.plugins.tiddlyweb.httpRequest({ + url: "http://tw5tiddlyweb.tiddlyspace.com/status", + callback: function(err,data) { + // Decode the status JSON + var json = null; + try { + json = JSON.parse(data); + } catch (e) { + } + if(json) { + // Check if we're logged in + var isLoggedIn = json.username !== "GUEST"; + // Set the various status tiddlers + $tw.wiki.addTiddler({ + title: $tw.plugins.tiddlyweb.titleIsLoggedIn, + text: isLoggedIn ? "yes" : "no" + }); + if(isLoggedIn) { + $tw.wiki.addTiddler({ + title: $tw.plugins.tiddlyweb.titleUserName, + text: json.username + }); + } else { + $tw.wiki.deleteTiddler($tw.plugins.tiddlyweb.titleUserName); + } + } + // Invoke the callback if present + if(callback) { + callback(isLoggedIn,json); + } + } + }); +}; + /* -Attempt to login to TiddlyWeb +Dispay a password prompt and allow the user to login */ -$tw.plugins.tiddlyweb.login = function(username,password,options) { - options = options || {}; +$tw.plugins.tiddlyweb.promptLogin = function() { + $tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) { + if(!isLoggedIn) { + $tw.passwordPrompt.createPrompt({ + serviceName: "Login to TiddlySpace", + callback: function(data) { + $tw.plugins.tiddlyweb.login(data.username,data.password); + return true; // Get rid of the password prompt + } + }); + } + }); +}; + +/* +Attempt to login to TiddlyWeb. + username: username + password: password + callback: invoked with arguments (err,isLoggedIn) +*/ +$tw.plugins.tiddlyweb.login = function(username,password,callback) { var httpRequest = $tw.plugins.tiddlyweb.httpRequest({ url: "http://tw5tiddlyweb.tiddlyspace.com/challenge/tiddlywebplugins.tiddlyspace.cookie_form", type: "POST", @@ -107,12 +130,40 @@ $tw.plugins.tiddlyweb.login = function(username,password,options) { }, callback: function(err,data) { if(err) { - console.log("login error",err); + if(callback) { + callback(err); + } } else { $tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) { - console.log("isLoggedIn",isLoggedIn); + if(callback) { + callback(null,isLoggedIn); + } }); - console.log("Result of login",data,httpRequest); + } + } + }); +}; + +/* +Attempt to log out of TiddlyWeb +*/ +$tw.plugins.tiddlyweb.logout = function(options) { + options = options || {}; + var httpRequest = $tw.plugins.tiddlyweb.httpRequest({ + url: "http://tw5tiddlyweb.tiddlyspace.com/logout", + type: "POST", + data: { + csrf_token: $tw.plugins.tiddlyweb.getCsrfToken(), + tiddlyweb_redirect: "/status" // workaround to marginalize automatic subsequent GET + }, + callback: function(err,data) { + if(err) { + console.log("logout error",err); + } else { + $tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) { + console.log("after logout, isLoggedIn",isLoggedIn); + }); + console.log("Result of logout",data,httpRequest); } } });