This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #2220, let sitehelper log you in if the website requests it
Change auth.login() to return the login success Change auth.login() to have options for asking about ownership information, and suppressing register-on-failed-login Change server to do an ownership check on a shot on successful login Add wantsauth script that is used to eagerly talk to the addon and try to initiate login Change shot/controller to use wantsauth
- Loading branch information
Showing
9 changed files
with
187 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* globals catcher, callBackground */ | ||
/** This is a content script added to all Page Shot pages, and allows the site to | ||
communicate with the add-on */ | ||
|
||
window.sitehelper = (function () { | ||
|
||
catcher.registerHandler((errorObj) => { | ||
callBackground("reportError", errorObj); | ||
}); | ||
|
||
|
||
function sendCustomEvent(name, detail) { | ||
if (typeof detail == "object") { | ||
// Note sending an object can lead to security problems, while a string | ||
// is safe to transfer: | ||
detail = JSON.stringify(detail); | ||
} | ||
document.dispatchEvent(new CustomEvent(name, {detail})); | ||
} | ||
|
||
document.addEventListener("delete-everything", catcher.watchFunction((event) => { | ||
// FIXME: implement | ||
alert("Not yet implemented"); | ||
}, false)); | ||
|
||
document.addEventListener("request-login", catcher.watchFunction((event) => { | ||
let shotId = event.detail; | ||
catcher.watchPromise(callBackground("getAuthInfo", shotId || null).then((info) => { | ||
sendCustomEvent("login-successful", {deviceId: info.deviceId, isOwner: info.isOwner}); | ||
})); | ||
})); | ||
|
||
// Depending on the script loading order, the site might get the addon-present event, | ||
// but probably won't - instead the site will ask for that event after it has loaded | ||
document.addEventListener("request-addon-present", catcher.watchFunction(() => { | ||
sendCustomEvent("addon-present"); | ||
}), false); | ||
|
||
sendCustomEvent("addon-present"); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** This allows for early communication with the add-on to ask for authentication information | ||
Including this script on a page indicates that the page would like to be authenticated | ||
(and that it isn't currently authenticated) | ||
A controller might use this like: | ||
if (window.wantsauth) { | ||
if (window.wantsauth.getAuthData()) { | ||
add window.wantsauth.getAuthData() to model | ||
} else { | ||
window.wantsauth.addAuthDataListener((data) => { | ||
add data to model | ||
}); | ||
} | ||
} | ||
*/ | ||
window.wantsauth = (function () { | ||
let exports = {}; | ||
|
||
let savedAuthData = null; | ||
let authDataCallbacks = []; | ||
|
||
// Note that this module is only loosely bound to any controller, but there | ||
// is special logic for view pages where ownership is interesting in addition to | ||
// authentication. As a result we have to parse the URL on our own: | ||
let maybeShotId = location.href.replace(/^https?:\/\/[^/]+\//i, ""); | ||
maybeShotId = maybeShotId.replace(/\?.*/, "").replace(/#.*/, ""); | ||
if (maybeShotId.search(/[a-z0-9]+\/[a-z0-9.]+$/i) === -1) { | ||
// Not a shot ID, which should look like {stuff}/{stuff} | ||
maybeShotId = null; | ||
} | ||
|
||
// These events are used to communicate with sitehelper.js: | ||
document.addEventListener("login-successful", (event) => { | ||
let {deviceId, isOwner} = JSON.parse(event.detail); | ||
savedAuthData = { | ||
deviceId, | ||
isOwner | ||
}; | ||
for (let callback of authDataCallbacks) { | ||
callback(savedAuthData); | ||
} | ||
}, false); | ||
|
||
document.addEventListener("addon-present", () => { | ||
document.dispatchEvent(new CustomEvent("request-login", {detail: maybeShotId})); | ||
}, false); | ||
|
||
document.dispatchEvent(new CustomEvent("request-addon-present")); | ||
|
||
exports.getAuthData = function () { | ||
return savedAuthData; | ||
}; | ||
|
||
exports.addAuthDataListener = function (func) { | ||
authDataCallbacks.push(func); | ||
}; | ||
|
||
return exports; | ||
})(); |