/* * Federated Wiki : Social Security Plugin * * Licensed under the MIT license. * https://github.com/fedwiki/wiki-security-social/blob/master/LICENSE.txt */ /* 1. Display login button - if there is no authenticated user 2. Display logout button - if the user is authenticated 3. When user authenticated, claim site if unclaimed - and repaint footer. */ (function() { var WinChan, claim_wiki, settings, setup, update_footer; WinChan = require('./winchan.js'); settings = {}; claim_wiki = function() { var myInit; if (!isClaimed) { myInit = { method: 'GET', cache: 'no-cache', mode: 'same-origin', credentials: 'include' }; return fetch('/auth/claim-wiki', myInit).then(function(response) { if (response.ok) { return response.json().then(function(json) { var ownerName; if (wiki.lineup.bestTitle() === 'Login Required') { return location.reload(); } else { ownerName = json.ownerName; window.isClaimed = true; window.isOwner = true; return update_footer(ownerName, true); } }); } else { return console.log('Attempt to claim site failed', response); } }); } }; update_footer = function(ownerName, isAuthenticated) { var logoutIconClass, logoutTitle, signonTitle; if (ownerName) { $('footer > #site-owner').html("Site Owned by: " + ownerName + ""); } $('footer > #security').empty(); if (isAuthenticated) { if (isOwner) { logoutTitle = "Sign-out"; logoutIconClass = 'fa fa-unlock fa-lg fa-fw'; } else { logoutTitle = "Not Owner : Sign-out"; logoutIconClass = 'fa fa-lock fa-lg fa-fw notOwner'; } $('footer > #security').append(""); $('footer > #security > #logout').on('click', function(e) { var myInit; e.preventDefault(); myInit = { method: 'GET', cache: 'no-cache', mode: 'same-origin', credentials: 'include' }; return fetch('/logout', myInit).then(function(response) { var user; if (response.ok) { window.isAuthenticated = false; user = ''; document.cookie = "state=loggedOut" + ";domain=." + settings.cookieDomain + "; path=/; max-age=60; sameSite=Strict;"; return update_footer(ownerName, isAuthenticated); } else { return console.log('logout failed: ', response); } }); }); if (!isClaimed) { $('footer > #security').append(""); return $('footer > #security > #claim').on('click', function(e) { e.preventDefault(); return claim_wiki(); }); } } else { if (!isClaimed) { signonTitle = 'Claim this Wiki'; } else { signonTitle = 'Wiki Owner Sign-on'; } $('footer > #security').append(""); return $('footer > #security > #show-security-dialog').on('click', function(e) { var w; e.preventDefault(); document.cookie = ("wikiName=" + window.location.host) + (";domain=." + settings.cookieDomain + "; path=/; max-age=300; sameSite=Strict;"); return w = WinChan.open({ url: settings.dialogURL, relay_url: settings.relayURL, window_features: "menubar=0, location=0, resizable=0, scrollbars=1, status=0, dialog=1, width=700, height=375", params: {} }, function(err, r) { if (err) { return console.log(err); } else { window.isAuthenticated = true; if (!isClaimed) { return claim_wiki(); } else { if (wiki.lineup.bestTitle() === 'Login Required') { return location.reload(); } else { return update_footer(ownerName, true); } } } }); }); } }; setup = function(user) { var lastCookie, myInit; if (!$("link[href='https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css']").length) { $('').appendTo("head"); } lastCookie = document.cookie; window.setInterval(function() { var currentCookie, myInit; currentCookie = document.cookie; if (currentCookie !== lastCookie) { console.log("Cookie changed"); if (document.cookie.match('(?:^|;)\\s?state=(.*?)(?:;|$)') !== null) { try { switch (document.cookie.match('(?:^|;)\\s?state=(.*?)(?:;|$)')[1]) { case 'loggedIn': window.isAuthenticated = true; break; case 'loggedOut': window.isAuthenticated = false; } myInit = { method: 'GET', cache: 'no-cache', mode: 'same-origin' }; fetch('/auth/client-settings.json', myInit).then(function(response) { return response.json().then(function(json) { window.isOwner = json.isOwner; return update_footer(ownerName, isAuthenticated); }); }); } catch (error) {} } return lastCookie = currentCookie; } }, 100); if (!$("link[href='/security/style.css']").length) { $('').appendTo("head"); } myInit = { method: 'GET', cache: 'no-cache', mode: 'same-origin' }; return fetch('/auth/client-settings.json', myInit).then(function(response) { if (response.ok) { return response.json().then(function(json) { var dialogHost, dialogProtocol; window.isOwner = json.isOwner; settings = json; if (settings.wikiHost) { dialogHost = settings.wikiHost; } else { dialogHost = window.location.hostname; } settings.cookieDomain = dialogHost; if (settings.useHttps) { dialogProtocol = 'https:'; } else { dialogProtocol = window.location.protocol; if (window.location.port) { dialogHost = dialogHost + ':' + window.location.port; } } settings.dialogURL = dialogProtocol + '//' + dialogHost + '/auth/loginDialog'; settings.relayURL = dialogProtocol + '//' + dialogHost + '/auth/relay.html'; settings.dialogAddAltURL = dialogProtocol + '//' + dialogHost + '/auth/addAuthDialog'; return update_footer(ownerName, isAuthenticated); }); } else { return console.log('Unable to fetch client settings: ', response); } }); }; window.plugins.security = { setup: setup, claim_wiki: claim_wiki, update_footer: update_footer }; }).call(this);