chore: add code formatting and linting (#952)
This commit is contained in:
@ -5,34 +5,34 @@ import uuid from 'uuid/v4';
|
||||
|
||||
function createNonce() {
|
||||
const nonce = uuid();
|
||||
window.sessionStorage.setItem("netlify-cms-auth", JSON.stringify({ nonce }));
|
||||
window.sessionStorage.setItem('netlify-cms-auth', JSON.stringify({ nonce }));
|
||||
return nonce;
|
||||
}
|
||||
|
||||
function validateNonce(check) {
|
||||
const auth = window.sessionStorage.getItem("netlify-cms-auth");
|
||||
const auth = window.sessionStorage.getItem('netlify-cms-auth');
|
||||
const valid = auth && JSON.parse(auth).nonce;
|
||||
window.localStorage.removeItem("netlify-cms-auth");
|
||||
return (check === valid);
|
||||
window.localStorage.removeItem('netlify-cms-auth');
|
||||
return check === valid;
|
||||
}
|
||||
|
||||
export default class ImplicitAuthenticator {
|
||||
constructor(config = {}) {
|
||||
const baseURL = trimEnd(config.base_url, '/');
|
||||
const authEndpoint = trim(config.auth_endpoint, '/');
|
||||
this.auth_url = `${ baseURL }/${ authEndpoint }`;
|
||||
this.auth_url = `${baseURL}/${authEndpoint}`;
|
||||
this.appID = config.app_id;
|
||||
this.clearHash = config.clearHash;
|
||||
}
|
||||
|
||||
authenticate(options, cb) {
|
||||
if (
|
||||
document.location.protocol !== "https:"
|
||||
// TODO: Is insecure localhost a bad idea as well? I don't think it is, since you are not actually
|
||||
// sending the token over the internet in this case, assuming the auth URL is secure.
|
||||
&& (document.location.hostname !== "localhost" && document.location.hostname !== "127.0.0.1")
|
||||
) {
|
||||
return cb(new Error("Cannot authenticate over insecure protocol!"));
|
||||
document.location.protocol !== 'https:' &&
|
||||
// TODO: Is insecure localhost a bad idea as well? I don't think it is, since you are not actually
|
||||
// sending the token over the internet in this case, assuming the auth URL is secure.
|
||||
(document.location.hostname !== 'localhost' && document.location.hostname !== '127.0.0.1')
|
||||
) {
|
||||
return cb(new Error('Cannot authenticate over insecure protocol!'));
|
||||
}
|
||||
|
||||
const authURL = new URL(this.auth_url);
|
||||
@ -50,7 +50,7 @@ export default class ImplicitAuthenticator {
|
||||
*/
|
||||
completeAuth(cb) {
|
||||
const hashParams = new URLSearchParams(document.location.hash.replace(/^#?\/?/, ''));
|
||||
if (!hashParams.has("access_token") && !hashParams.has("error")) {
|
||||
if (!hashParams.has('access_token') && !hashParams.has('error')) {
|
||||
return;
|
||||
}
|
||||
// Remove tokens from hash so that token does not remain in browser history.
|
||||
@ -60,11 +60,11 @@ export default class ImplicitAuthenticator {
|
||||
|
||||
const validNonce = validateNonce(params.get('state'));
|
||||
if (!validNonce) {
|
||||
return cb(new Error("Invalid nonce"));
|
||||
return cb(new Error('Invalid nonce'));
|
||||
}
|
||||
|
||||
if (params.has('error')) {
|
||||
return cb(new Error(`${ params.get('error') }: ${ params.get('error_description') }`));
|
||||
return cb(new Error(`${params.get('error')}: ${params.get('error_description')}`));
|
||||
}
|
||||
|
||||
if (params.has('access_token')) {
|
||||
|
@ -16,20 +16,20 @@ class NetlifyError {
|
||||
const PROVIDERS = {
|
||||
github: {
|
||||
width: 960,
|
||||
height: 600
|
||||
height: 600,
|
||||
},
|
||||
gitlab: {
|
||||
width: 960,
|
||||
height: 600
|
||||
height: 600,
|
||||
},
|
||||
bitbucket: {
|
||||
width: 960,
|
||||
height: 500
|
||||
height: 500,
|
||||
},
|
||||
email: {
|
||||
width: 500,
|
||||
height: 400
|
||||
}
|
||||
height: 400,
|
||||
},
|
||||
};
|
||||
|
||||
class Authenticator {
|
||||
@ -40,8 +40,8 @@ class Authenticator {
|
||||
}
|
||||
|
||||
handshakeCallback(options, cb) {
|
||||
const fn = (e) => {
|
||||
if (e.data === ('authorizing:' + options.provider) && e.origin === this.base_url) {
|
||||
const fn = e => {
|
||||
if (e.data === 'authorizing:' + options.provider && e.origin === this.base_url) {
|
||||
window.removeEventListener('message', fn, false);
|
||||
window.addEventListener('message', this.authorizeCallback(options, cb), false);
|
||||
return this.authWindow.postMessage(e.data, e.origin);
|
||||
@ -51,17 +51,23 @@ class Authenticator {
|
||||
}
|
||||
|
||||
authorizeCallback(options, cb) {
|
||||
const fn = (e) => {
|
||||
if (e.origin !== this.base_url) { return; }
|
||||
const fn = e => {
|
||||
if (e.origin !== this.base_url) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.data.indexOf('authorization:' + options.provider + ':success:') === 0) {
|
||||
const data = JSON.parse(e.data.match(new RegExp('^authorization:' + options.provider + ':success:(.+)$'))[1]);
|
||||
const data = JSON.parse(
|
||||
e.data.match(new RegExp('^authorization:' + options.provider + ':success:(.+)$'))[1],
|
||||
);
|
||||
window.removeEventListener('message', fn, false);
|
||||
this.authWindow.close();
|
||||
cb(null, data);
|
||||
}
|
||||
if (e.data.indexOf('authorization:' + options.provider + ':error:') === 0) {
|
||||
const err = JSON.parse(e.data.match(new RegExp('^authorization:' + options.provider + ':error:(.+)$'))[1]);
|
||||
const err = JSON.parse(
|
||||
e.data.match(new RegExp('^authorization:' + options.provider + ':error:(.+)$'))[1],
|
||||
);
|
||||
window.removeEventListener('message', fn, false);
|
||||
this.authWindow.close();
|
||||
cb(new NetlifyError(err));
|
||||
@ -83,21 +89,28 @@ class Authenticator {
|
||||
const siteID = this.getSiteID();
|
||||
|
||||
if (!provider) {
|
||||
return cb(new NetlifyError({
|
||||
message: 'You must specify a provider when calling netlify.authenticate',
|
||||
}));
|
||||
return cb(
|
||||
new NetlifyError({
|
||||
message: 'You must specify a provider when calling netlify.authenticate',
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (!siteID) {
|
||||
return cb(new NetlifyError({
|
||||
message: 'You must set a site_id with netlify.configure({site_id: \'your-site-id\'}) to make authentication work from localhost',
|
||||
}));
|
||||
return cb(
|
||||
new NetlifyError({
|
||||
message:
|
||||
"You must set a site_id with netlify.configure({site_id: 'your-site-id'}) to make authentication work from localhost",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const conf = PROVIDERS[provider] || PROVIDERS.github;
|
||||
const left = (screen.width / 2) - (conf.width / 2);
|
||||
const top = (screen.height / 2) - (conf.height / 2);
|
||||
const left = screen.width / 2 - conf.width / 2;
|
||||
const top = screen.height / 2 - conf.height / 2;
|
||||
window.addEventListener('message', this.handshakeCallback(options, cb), false);
|
||||
let url = `${ this.base_url }/${ this.auth_endpoint }?provider=${ options.provider }&site_id=${ siteID }`;
|
||||
let url = `${this.base_url}/${this.auth_endpoint}?provider=${
|
||||
options.provider
|
||||
}&site_id=${siteID}`;
|
||||
if (options.scope) {
|
||||
url += '&scope=' + options.scope;
|
||||
}
|
||||
@ -114,7 +127,15 @@ class Authenticator {
|
||||
url,
|
||||
'Netlify Authorization',
|
||||
'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, ' +
|
||||
('width=' + conf.width + ', height=' + conf.height + ', top=' + top + ', left=' + left + ');')
|
||||
('width=' +
|
||||
conf.width +
|
||||
', height=' +
|
||||
conf.height +
|
||||
', top=' +
|
||||
top +
|
||||
', left=' +
|
||||
left +
|
||||
');'),
|
||||
);
|
||||
this.authWindow.focus();
|
||||
}
|
||||
@ -125,17 +146,24 @@ class Authenticator {
|
||||
const onError = cb || Promise.reject.bind(Promise);
|
||||
|
||||
if (!provider || !refresh_token) {
|
||||
return onError(new NetlifyError({
|
||||
message: 'You must specify a provider and refresh token when calling netlify.refresh',
|
||||
}));
|
||||
return onError(
|
||||
new NetlifyError({
|
||||
message: 'You must specify a provider and refresh token when calling netlify.refresh',
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (!siteID) {
|
||||
return onError(new NetlifyError({
|
||||
message: 'You must set a site_id with netlify.configure({site_id: \'your-site-id\'}) to make token refresh work from localhost',
|
||||
}));
|
||||
return onError(
|
||||
new NetlifyError({
|
||||
message:
|
||||
"You must set a site_id with netlify.configure({site_id: 'your-site-id'}) to make token refresh work from localhost",
|
||||
}),
|
||||
);
|
||||
}
|
||||
const url = `${ this.base_url }/${ this.auth_endpoint }/refresh?provider=${ provider }&site_id=${ siteID }&refresh_token=${ refresh_token }`;
|
||||
const refreshPromise = fetch(url, { method: "POST", body: "" }).then(res => res.json());
|
||||
const url = `${this.base_url}/${
|
||||
this.auth_endpoint
|
||||
}/refresh?provider=${provider}&site_id=${siteID}&refresh_token=${refresh_token}`;
|
||||
const refreshPromise = fetch(url, { method: 'POST', body: '' }).then(res => res.json());
|
||||
|
||||
// Return a promise if a callback wasn't provided
|
||||
if (!cb) {
|
||||
|
Reference in New Issue
Block a user