Skip to content

Instantly share code, notes, and snippets.

@tobias-edwards
Last active March 17, 2023 22:23
Show Gist options
  • Save tobias-edwards/f10ec6d4b439faf933418196af405ef7 to your computer and use it in GitHub Desktop.
Save tobias-edwards/f10ec6d4b439faf933418196af405ef7 to your computer and use it in GitHub Desktop.
Contentstack log in via SSO

Compatible in Chrome and Firefox.

Chrome optimisation

This script matches against every Contentstack page because Firefox cannot match @match against the path of a URL.

For Chrome, you can replace the existing @match with:

// @match        https://app.contentstack.com/#!/login*

This will match the script against the login page only.

// ==UserScript==
// @name Contentstack log in via SSO
// @description Automatically logs in to Contentstack via SSO
// @author tobias-edwards
// @match https://app.contentstack.com/
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
// @grant none
// @run-at document-end
// @version 0.1
// ==/UserScript==
const SSO_NAME = "SSO NAME GOES HERE";
const setReactInput = (input, value) => {
// React overrides native <input> value setter, so call directly
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value"
).set;
nativeInputValueSetter.call(input, value);
const inputEvent = new Event("input", { bubbles: true });
input.dispatchEvent(inputEvent);
};
const sso = {
load: function () {
window.location.href = "https://app.contentstack.com/#!/login/sso";
return this;
},
logIn: function (name) {
VM.observe(document.body, () => {
const ssoInput = document.querySelector("#sso");
if (ssoInput) {
const ssoSubmit = ssoInput.form.querySelector("button[type='submit']");
setReactInput(ssoInput, name);
ssoSubmit.click();
}
});
return this;
},
};
const { hash, search } = window.location;
if (hash.startsWith("#!/login") && search === "") {
sso.load().logIn(SSO_NAME);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment