2018-07-03 15:47:15 -04:00
|
|
|
import trim from 'lodash/trim';
|
|
|
|
import trimEnd from 'lodash/trim';
|
2018-04-24 21:35:56 -04:00
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
const NETLIFY_API = 'https://api.netlify.com';
|
2018-04-24 21:35:56 -04:00
|
|
|
const AUTH_ENDPOINT = 'auth';
|
2016-06-05 01:52:18 -07:00
|
|
|
|
|
|
|
class NetlifyError {
|
|
|
|
constructor(err) {
|
|
|
|
this.err = err;
|
|
|
|
}
|
|
|
|
toString() {
|
|
|
|
return this.err && this.err.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const PROVIDERS = {
|
|
|
|
github: {
|
|
|
|
width: 960,
|
|
|
|
height: 600
|
|
|
|
},
|
|
|
|
gitlab: {
|
|
|
|
width: 960,
|
|
|
|
height: 600
|
|
|
|
},
|
|
|
|
bitbucket: {
|
|
|
|
width: 960,
|
|
|
|
height: 500
|
|
|
|
},
|
|
|
|
email: {
|
|
|
|
width: 500,
|
|
|
|
height: 400
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Authenticator {
|
2016-07-20 13:46:52 -03:00
|
|
|
constructor(config = {}) {
|
|
|
|
this.site_id = config.site_id || null;
|
2018-04-24 21:35:56 -04:00
|
|
|
this.base_url = trimEnd(config.base_url, '/') || NETLIFY_API;
|
|
|
|
this.auth_endpoint = trim(config.auth_endpoint, '/') || AUTH_ENDPOINT;
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handshakeCallback(options, cb) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizeCallback(options, cb) {
|
|
|
|
const fn = (e) => {
|
|
|
|
if (e.origin !== this.base_url) { return; }
|
2018-07-19 18:11:23 -07:00
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
if (e.data.indexOf('authorization:' + options.provider + ':success:') === 0) {
|
2018-07-19 18:11:23 -07:00
|
|
|
const data = JSON.parse(e.data.match(new RegExp('^authorization:' + options.provider + ':success:(.+)$'))[1]);
|
2016-06-05 01:52:18 -07:00
|
|
|
window.removeEventListener('message', fn, false);
|
|
|
|
this.authWindow.close();
|
|
|
|
cb(null, data);
|
|
|
|
}
|
|
|
|
if (e.data.indexOf('authorization:' + options.provider + ':error:') === 0) {
|
2018-07-19 18:11:23 -07:00
|
|
|
const err = JSON.parse(e.data.match(new RegExp('^authorization:' + options.provider + ':error:(.+)$'))[1]);
|
2016-06-05 01:52:18 -07:00
|
|
|
window.removeEventListener('message', fn, false);
|
|
|
|
this.authWindow.close();
|
|
|
|
cb(new NetlifyError(err));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSiteID() {
|
|
|
|
if (this.site_id) {
|
|
|
|
return this.site_id;
|
|
|
|
}
|
|
|
|
const host = document.location.host.split(':')[0];
|
2018-07-19 18:11:23 -07:00
|
|
|
return host === 'localhost' ? 'cms.netlify.com' : host;
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
authenticate(options, cb) {
|
2018-07-19 18:11:23 -07:00
|
|
|
const { provider } = options;
|
|
|
|
const siteID = this.getSiteID();
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
if (!provider) {
|
|
|
|
return cb(new NetlifyError({
|
2018-07-19 18:11:23 -07:00
|
|
|
message: 'You must specify a provider when calling netlify.authenticate',
|
2016-06-05 01:52:18 -07:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (!siteID) {
|
|
|
|
return cb(new NetlifyError({
|
2018-07-19 18:11:23 -07:00
|
|
|
message: 'You must set a site_id with netlify.configure({site_id: \'your-site-id\'}) to make authentication work from localhost',
|
2016-06-05 01:52:18 -07:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
const conf = PROVIDERS[provider] || PROVIDERS.github;
|
2018-07-19 18:11:23 -07:00
|
|
|
const left = (screen.width / 2) - (conf.width / 2);
|
|
|
|
const top = (screen.height / 2) - (conf.height / 2);
|
2016-06-05 01:52:18 -07:00
|
|
|
window.addEventListener('message', this.handshakeCallback(options, cb), false);
|
2018-07-19 18:11:23 -07:00
|
|
|
let url = `${ this.base_url }/${ this.auth_endpoint }?provider=${ options.provider }&site_id=${ siteID }`;
|
2016-06-05 01:52:18 -07:00
|
|
|
if (options.scope) {
|
|
|
|
url += '&scope=' + options.scope;
|
|
|
|
}
|
|
|
|
if (options.login === true) {
|
|
|
|
url += '&login=true';
|
|
|
|
}
|
|
|
|
if (options.beta_invite) {
|
|
|
|
url += '&beta_invite=' + options.beta_invite;
|
|
|
|
}
|
|
|
|
if (options.invite_code) {
|
|
|
|
url += '&invite_code=' + options.invite_code;
|
|
|
|
}
|
|
|
|
this.authWindow = window.open(
|
|
|
|
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 + ');')
|
|
|
|
);
|
|
|
|
this.authWindow.focus();
|
|
|
|
}
|
2018-07-19 18:11:23 -07:00
|
|
|
|
|
|
|
refresh(options, cb) {
|
|
|
|
const { provider, refresh_token } = options;
|
|
|
|
const siteID = this.getSiteID();
|
|
|
|
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',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
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',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
return refreshPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, use the provided callback.
|
|
|
|
refreshPromise.then(data => cb(null, data)).catch(cb);
|
|
|
|
}
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Authenticator;
|