2017-09-05 13:24:16 -07:00
|
|
|
import GoTrue from "gotrue-js";
|
2017-01-26 19:23:42 -02:00
|
|
|
import jwtDecode from 'jwt-decode';
|
2017-09-05 13:24:16 -07:00
|
|
|
import {List} from 'immutable';
|
2017-01-26 19:23:42 -02:00
|
|
|
import { get, pick, intersection } from "lodash";
|
2016-12-23 16:59:48 -02:00
|
|
|
import GitHubBackend from "../github/implementation";
|
|
|
|
import API from "./API";
|
|
|
|
import AuthenticationPage from "./AuthenticationPage";
|
|
|
|
|
2017-09-05 13:24:16 -07:00
|
|
|
const localHosts = {
|
|
|
|
localhost: true,
|
|
|
|
'127.0.0.1': true,
|
|
|
|
'0.0.0.0': true
|
|
|
|
}
|
2017-09-05 19:30:03 -07:00
|
|
|
const defaults = {
|
|
|
|
identity: '/.netlify/identity',
|
|
|
|
gateway: '/.netlify/git/github'
|
|
|
|
}
|
2017-09-05 13:24:16 -07:00
|
|
|
|
|
|
|
function getEndpoint(endpoint, netlifySiteURL) {
|
2017-09-05 19:30:03 -07:00
|
|
|
if (localHosts[document.location.host.split(":").shift()] && netlifySiteURL && endpoint.match(/^\/\.netlify\//)) {
|
|
|
|
const parts = [];
|
|
|
|
if (netlifySiteURL) {
|
|
|
|
parts.push(netlifySiteURL);
|
|
|
|
if (!netlifySiteURL.match(/\/$/)) { parts.push("/"); }
|
|
|
|
}
|
|
|
|
parts.push(endpoint.replace(/^\//, ''));
|
2017-09-05 13:24:16 -07:00
|
|
|
return parts.join("");
|
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
2017-09-05 19:30:03 -07:00
|
|
|
export default class GitGateway extends GitHubBackend {
|
2016-12-23 16:59:48 -02:00
|
|
|
constructor(config) {
|
|
|
|
super(config, true);
|
|
|
|
|
2017-09-06 18:28:20 -07:00
|
|
|
this.accept_roles = (config.getIn(["backend", "accept_roles"]) || List()).toArray();
|
2017-01-26 19:23:42 -02:00
|
|
|
|
2017-09-05 13:24:16 -07:00
|
|
|
const netlifySiteURL = localStorage.getItem("netlifySiteURL");
|
2017-09-05 19:30:03 -07:00
|
|
|
const APIUrl = getEndpoint(config.getIn(["backend", "identity_url"], defaults.identity), netlifySiteURL);
|
|
|
|
this.github_proxy_url = getEndpoint(config.getIn(["backend", "gateway_url"], defaults.gateway), netlifySiteURL);
|
|
|
|
this.authClient = window.netlifyIdentity ? window.netlifyIdentity.gotrue : new GoTrue({APIUrl});
|
2016-12-23 16:59:48 -02:00
|
|
|
|
|
|
|
AuthenticationPage.authClient = this.authClient;
|
|
|
|
}
|
|
|
|
|
2017-08-29 13:45:05 -06:00
|
|
|
restoreUser() {
|
2017-09-05 19:30:03 -07:00
|
|
|
const user = this.authClient && this.authClient.currentUser();
|
2016-12-23 16:59:48 -02:00
|
|
|
if (!user) return Promise.reject();
|
|
|
|
return this.authenticate(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticate(user) {
|
2017-01-10 22:23:22 -02:00
|
|
|
this.tokenPromise = user.jwt.bind(user);
|
2017-01-26 19:23:42 -02:00
|
|
|
return this.tokenPromise()
|
|
|
|
.then((token) => {
|
2017-09-05 16:48:51 -07:00
|
|
|
let validRole = true;
|
|
|
|
if (this.accept_roles && this.accept_roles.length > 0) {
|
|
|
|
validRole = intersection(userRoles, this.accept_roles).length > 0;
|
|
|
|
}
|
2017-01-26 19:23:42 -02:00
|
|
|
const userRoles = get(jwtDecode(token), 'app_metadata.roles', []);
|
2017-09-05 16:48:51 -07:00
|
|
|
if (validRole) {
|
2017-01-26 19:23:42 -02:00
|
|
|
const userData = {
|
2017-09-06 18:14:59 -07:00
|
|
|
name: user.user_metadata.name || user.email.split('@').shift(),
|
2017-01-26 19:23:42 -02:00
|
|
|
email: user.email,
|
2017-09-08 09:02:48 -06:00
|
|
|
avatar_url: user.user_metadata.avatar_url,
|
2017-01-26 19:23:42 -02:00
|
|
|
metadata: user.user_metadata,
|
|
|
|
};
|
|
|
|
this.api = new API({
|
|
|
|
api_root: this.github_proxy_url,
|
2017-09-18 17:56:49 -04:00
|
|
|
branch: this.branch,
|
2017-01-26 19:23:42 -02:00
|
|
|
tokenPromise: this.tokenPromise,
|
|
|
|
commitAuthor: pick(userData, ["name", "email"]),
|
|
|
|
});
|
|
|
|
return userData;
|
|
|
|
} else {
|
2017-09-05 16:48:51 -07:00
|
|
|
throw new Error("You don't have sufficient permissions to access Netlify CMS");
|
2017-01-26 19:23:42 -02:00
|
|
|
}
|
2017-09-05 13:24:16 -07:00
|
|
|
});
|
2017-01-10 22:23:22 -02:00
|
|
|
}
|
|
|
|
|
2017-09-05 19:30:03 -07:00
|
|
|
logout() {
|
|
|
|
if (window.netlifyIdentity) {
|
|
|
|
return window.netlifyIdentity.logout();
|
|
|
|
}
|
|
|
|
const user = this.authClient.currentUser();
|
|
|
|
return user && user.logout();
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
getToken() {
|
|
|
|
return this.tokenPromise();
|
2016-12-23 16:59:48 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
authComponent() {
|
|
|
|
return AuthenticationPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|