migrate git gateway backend
This commit is contained in:
@ -0,0 +1,179 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import React from 'react';
|
||||
import styled from 'react-emotion';
|
||||
import { partial } from 'lodash';
|
||||
import {
|
||||
Icon,
|
||||
AuthenticationPage,
|
||||
buttons,
|
||||
shadows,
|
||||
colors,
|
||||
colorsRaw,
|
||||
lengths
|
||||
} from 'netlify-cms-ui-default';
|
||||
|
||||
const LoginButton = styled.button`
|
||||
${buttons.button};
|
||||
${shadows.dropDeep};
|
||||
${buttons.default};
|
||||
${buttons.gray};
|
||||
|
||||
padding: 0 30px;
|
||||
display: block;
|
||||
margin-top: 20px;
|
||||
margin-left: auto;
|
||||
`
|
||||
|
||||
const AuthForm = styled.form`
|
||||
width: 350px;
|
||||
margin-top: -80px;
|
||||
`
|
||||
|
||||
const AuthInput = styled.input`
|
||||
background-color: ${colorsRaw.white};
|
||||
border-radius: ${lengths.borderRadius};
|
||||
|
||||
font-size: 14px;
|
||||
padding: 10px 10px;
|
||||
margin-bottom: 15px;
|
||||
margin-top: 6px;
|
||||
width: 100%;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
box-shadow: inset 0 0 0 2px ${colors.active};
|
||||
}
|
||||
`
|
||||
|
||||
const ErrorMessage = styled.p`
|
||||
color: ${colors.errorText};
|
||||
`
|
||||
|
||||
let component = null;
|
||||
|
||||
console.log(window.netlifyIdentity);
|
||||
if (window.netlifyIdentity) {
|
||||
window.netlifyIdentity.on('login', (user) => {
|
||||
component && component.handleIdentityLogin(user);
|
||||
});
|
||||
window.netlifyIdentity.on('logout', () => {
|
||||
component && component.handleIdentityLogout();
|
||||
});
|
||||
}
|
||||
|
||||
export default class GitGatewayAuthenticationPage extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
component = this;
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (!this.loggedIn && window.netlifyIdentity && window.netlifyIdentity.currentUser()) {
|
||||
this.props.onLogin(window.netlifyIdentity.currentUser());
|
||||
window.netlifyIdentity.close();
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
component = null;
|
||||
}
|
||||
|
||||
handleIdentityLogin = (user) => {
|
||||
this.props.onLogin(user);
|
||||
window.netlifyIdentity.close();
|
||||
}
|
||||
|
||||
handleIdentityLogout = () => {
|
||||
window.netlifyIdentity.open();
|
||||
}
|
||||
|
||||
handleIdentity = () => {
|
||||
const user = window.netlifyIdentity.currentUser();
|
||||
if (user) {
|
||||
this.props.onLogin(user);
|
||||
} else {
|
||||
window.netlifyIdentity.open();
|
||||
}
|
||||
}
|
||||
|
||||
static propTypes = {
|
||||
onLogin: PropTypes.func.isRequired,
|
||||
inProgress: PropTypes.bool.isRequired,
|
||||
};
|
||||
|
||||
state = { email: "", password: "", errors: {} };
|
||||
|
||||
handleChange = (name, e) => {
|
||||
this.setState({ ...this.state, [name]: e.target.value });
|
||||
};
|
||||
|
||||
handleLogin = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const { email, password } = this.state;
|
||||
const errors = {};
|
||||
if (!email) {
|
||||
errors.email = 'Make sure to enter your email.';
|
||||
}
|
||||
if (!password) {
|
||||
errors.password = 'Please enter your password.';
|
||||
}
|
||||
|
||||
if (Object.keys(errors).length > 0) {
|
||||
this.setState({ errors });
|
||||
return;
|
||||
}
|
||||
|
||||
AuthenticationPage.authClient.login(this.state.email, this.state.password, true)
|
||||
.then((user) => {
|
||||
this.props.onLogin(user);
|
||||
})
|
||||
.catch((error) => {
|
||||
this.setState({ errors: { server: error.description || error.msg || error }, loggingIn: false });
|
||||
});
|
||||
};
|
||||
|
||||
render() {
|
||||
const { errors } = this.state;
|
||||
const { error, inProgress } = this.props;
|
||||
|
||||
if (window.netlifyIdentity) {
|
||||
return (
|
||||
<AuthenticationPage
|
||||
onLogin={this.handleIdentity}
|
||||
renderButtonContent={() => 'Login with Netlify Identity'}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
console.log('returning');
|
||||
|
||||
return (
|
||||
<AuthenticationPage renderPageContent={() => (
|
||||
<AuthForm onSubmit={this.handleLogin}>
|
||||
{!error ? null : <ErrorMessage>{error}</ErrorMessage>}
|
||||
{!errors.server ? null : <ErrorMessage>{errors.server}</ErrorMessage>}
|
||||
<ErrorMessage>{errors.email || null}</ErrorMessage>
|
||||
<AuthInput
|
||||
type="text"
|
||||
name="email"
|
||||
placeholder="Email"
|
||||
value={this.state.email}
|
||||
onChange={partial(this.handleChange, 'email')}
|
||||
/>
|
||||
<ErrorMessage>{errors.password || null}</ErrorMessage>
|
||||
<AuthInput
|
||||
type="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
value={this.state.password}
|
||||
onChange={partial(this.handleChange, 'password')}
|
||||
/>
|
||||
<LoginButton disabled={inProgress}>{inProgress ? 'Logging in...' : 'Login'}</LoginButton>
|
||||
</AuthForm>
|
||||
)}/>
|
||||
);
|
||||
}
|
||||
}
|
106
packages/netlify-cms-backend-git-gateway/src/GitHubAPI.js
Normal file
106
packages/netlify-cms-backend-git-gateway/src/GitHubAPI.js
Normal file
@ -0,0 +1,106 @@
|
||||
import { API as GithubAPI } from "netlify-cms-backend-github";
|
||||
import { APIError } from "netlify-cms-lib-util";
|
||||
|
||||
export default class API extends GithubAPI {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
this.api_root = config.api_root;
|
||||
this.tokenPromise = config.tokenPromise;
|
||||
this.commitAuthor = config.commitAuthor;
|
||||
this.repoURL = "";
|
||||
}
|
||||
|
||||
hasWriteAccess() {
|
||||
return this.getBranch()
|
||||
.then(() => true)
|
||||
.catch(error => {
|
||||
if (error.status === 401) {
|
||||
if (error.message === "Bad credentials") {
|
||||
throw new APIError("Git Gateway Error: Please ask your site administrator to reissue the Git Gateway token.", error.status, 'Git Gateway');
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (error.status === 404 && (error.message === undefined || error.message === "Unable to locate site configuration")) {
|
||||
throw new APIError(`Git Gateway Error: Please make sure Git Gateway is enabled on your site.`, error.status, 'Git Gateway');
|
||||
} else {
|
||||
console.error("Problem fetching repo data from Git Gateway");
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getRequestHeaders(headers = {}) {
|
||||
return this.tokenPromise()
|
||||
.then((jwtToken) => {
|
||||
const baseHeader = {
|
||||
"Authorization": `Bearer ${ jwtToken }`,
|
||||
"Content-Type": "application/json",
|
||||
...headers,
|
||||
};
|
||||
|
||||
return baseHeader;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
urlFor(path, options) {
|
||||
const cacheBuster = new Date().getTime();
|
||||
const params = [`ts=${ cacheBuster }`];
|
||||
if (options.params) {
|
||||
for (const key in options.params) {
|
||||
params.push(`${ key }=${ encodeURIComponent(options.params[key]) }`);
|
||||
}
|
||||
}
|
||||
if (params.length) {
|
||||
path += `?${ params.join("&") }`;
|
||||
}
|
||||
return this.api_root + path;
|
||||
}
|
||||
|
||||
user() {
|
||||
return Promise.resolve(this.commitAuthor);
|
||||
}
|
||||
|
||||
request(path, options = {}) {
|
||||
const url = this.urlFor(path, options);
|
||||
let responseStatus;
|
||||
return this.getRequestHeaders(options.headers || {})
|
||||
.then(headers => fetch(url, { ...options, headers }))
|
||||
.then((response) => {
|
||||
responseStatus = response.status;
|
||||
const contentType = response.headers.get("Content-Type");
|
||||
if (contentType && contentType.match(/json/)) {
|
||||
return this.parseJsonResponse(response);
|
||||
}
|
||||
const text = response.text();
|
||||
if (!response.ok) {
|
||||
return Promise.reject(text);
|
||||
}
|
||||
return text;
|
||||
})
|
||||
.catch(error => {
|
||||
throw new APIError((error.message || error.msg), responseStatus, 'Git Gateway');
|
||||
});
|
||||
}
|
||||
|
||||
commit(message, changeTree) {
|
||||
const commitParams = {
|
||||
message,
|
||||
tree: changeTree.sha,
|
||||
parents: changeTree.parentSha ? [changeTree.parentSha] : [],
|
||||
};
|
||||
|
||||
if (this.commitAuthor) {
|
||||
commitParams.author = {
|
||||
...this.commitAuthor,
|
||||
date: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
return this.request("/git/commits", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(commitParams),
|
||||
});
|
||||
}
|
||||
|
||||
}
|
24
packages/netlify-cms-backend-git-gateway/src/GitLabAPI.js
Normal file
24
packages/netlify-cms-backend-git-gateway/src/GitLabAPI.js
Normal file
@ -0,0 +1,24 @@
|
||||
import { flow } from "lodash";
|
||||
import { API as GitlabAPI } from "netlify-cms-backend-gitlab";
|
||||
import { unsentRequest, then } from "netlify-cms-lib-util";
|
||||
|
||||
export default class API extends GitlabAPI {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
this.tokenPromise = config.tokenPromise;
|
||||
this.commitAuthor = config.commitAuthor;
|
||||
this.repoURL = "";
|
||||
}
|
||||
|
||||
authenticateRequest = async req => unsentRequest.withHeaders({
|
||||
Authorization: `Bearer ${ await this.tokenPromise() }`,
|
||||
}, req);
|
||||
|
||||
request = async req => flow([
|
||||
this.buildRequest,
|
||||
this.authenticateRequest,
|
||||
then(unsentRequest.performRequest),
|
||||
])(req);
|
||||
|
||||
hasWriteAccess = () => Promise.resolve(true)
|
||||
}
|
159
packages/netlify-cms-backend-git-gateway/src/implementation.js
Normal file
159
packages/netlify-cms-backend-git-gateway/src/implementation.js
Normal file
@ -0,0 +1,159 @@
|
||||
import GoTrue from "gotrue-js";
|
||||
import jwtDecode from 'jwt-decode';
|
||||
import { List } from 'immutable';
|
||||
import { get, pick, intersection } from "lodash";
|
||||
import { unsentRequest } from "netlify-cms-lib-util";
|
||||
import { GitHubBackend } from "netlify-cms-backend-github";
|
||||
import { GitLabBackend } from "netlify-cms-backend-gitlab";
|
||||
import BitBucketBackend from "Backends/bitbucket/implementation";
|
||||
import GitHubAPI from "./GitHubAPI";
|
||||
import GitLabAPI from "./GitLabAPI";
|
||||
import BitBucketAPI from "Backends/bitbucket/API";
|
||||
import AuthenticationPage from "./AuthenticationPage";
|
||||
|
||||
const localHosts = {
|
||||
localhost: true,
|
||||
'127.0.0.1': true,
|
||||
'0.0.0.0': true,
|
||||
};
|
||||
const defaults = {
|
||||
identity: '/.netlify/identity',
|
||||
gateway: '/.netlify/git',
|
||||
};
|
||||
|
||||
function getEndpoint(endpoint, netlifySiteURL) {
|
||||
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(/^\//, ''));
|
||||
return parts.join("");
|
||||
}
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
export default class GitGateway {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
this.branch = config.getIn(["backend", "branch"], "master").trim();
|
||||
this.squash_merges = config.getIn(["backend", "squash_merges"]);
|
||||
|
||||
const netlifySiteURL = localStorage.getItem("netlifySiteURL");
|
||||
const APIUrl = getEndpoint(config.getIn(["backend", "identity_url"], defaults.identity), netlifySiteURL);
|
||||
this.gatewayUrl = getEndpoint(config.getIn(["backend", "gateway_url"], defaults.gateway), netlifySiteURL);
|
||||
|
||||
const backendTypeRegex = /\/(github|gitlab|bitbucket)\/?$/;
|
||||
const backendTypeMatches = this.gatewayUrl.match(backendTypeRegex);
|
||||
if (backendTypeMatches) {
|
||||
this.backendType = backendTypeMatches[1];
|
||||
this.gatewayUrl = this.gatewayUrl.replace(backendTypeRegex, "/");
|
||||
} else {
|
||||
this.backendType = null;
|
||||
}
|
||||
|
||||
this.authClient = window.netlifyIdentity ? window.netlifyIdentity.gotrue : new GoTrue({ APIUrl });
|
||||
AuthenticationPage.authClient = this.authClient;
|
||||
|
||||
this.backend = null;
|
||||
}
|
||||
|
||||
requestFunction = req => this.tokenPromise()
|
||||
.then(token => unsentRequest.withHeaders({ Authorization: `Bearer ${ token }` }, req))
|
||||
.then(unsentRequest.performRequest);
|
||||
|
||||
authenticate(user) {
|
||||
this.tokenPromise = user.jwt.bind(user);
|
||||
return this.tokenPromise().then(async token => {
|
||||
if (!this.backendType) {
|
||||
const { github_enabled, gitlab_enabled, bitbucket_enabled, roles } = await fetch(`${ this.gatewayUrl }/settings`, {
|
||||
headers: { Authorization: `Bearer ${ token }` },
|
||||
}).then(res => res.json());
|
||||
this.acceptRoles = roles;
|
||||
if (github_enabled) {
|
||||
this.backendType = "github";
|
||||
} else if (gitlab_enabled) {
|
||||
this.backendType = "gitlab";
|
||||
} else if (bitbucket_enabled) {
|
||||
this.backendType = "bitbucket";
|
||||
}
|
||||
}
|
||||
|
||||
if (this.acceptRoles && this.acceptRoles.length > 0) {
|
||||
const userRoles = get(jwtDecode(token), 'app_metadata.roles', []);
|
||||
const validRole = intersection(userRoles, this.acceptRoles).length > 0;
|
||||
if (!validRole) {
|
||||
throw new Error("You don't have sufficient permissions to access Netlify CMS");
|
||||
}
|
||||
}
|
||||
|
||||
const userData = {
|
||||
name: user.user_metadata.name || user.email.split('@').shift(),
|
||||
email: user.email,
|
||||
avatar_url: user.user_metadata.avatar_url,
|
||||
metadata: user.user_metadata,
|
||||
};
|
||||
const apiConfig = {
|
||||
api_root: `${ this.gatewayUrl }/${ this.backendType }`,
|
||||
branch: this.branch,
|
||||
tokenPromise: this.tokenPromise,
|
||||
commitAuthor: pick(userData, ["name", "email"]),
|
||||
squash_merges: this.squash_merges,
|
||||
};
|
||||
|
||||
if (this.backendType === "github") {
|
||||
this.api = new GitHubAPI(apiConfig);
|
||||
this.backend = new GitHubBackend(this.config, { proxied: true, API: this.api });
|
||||
} else if (this.backendType === "gitlab") {
|
||||
this.api = new GitLabAPI(apiConfig);
|
||||
this.backend = new GitLabBackend(this.config, { proxied: true, API: this.api });
|
||||
} else if (this.backendType === "bitbucket") {
|
||||
this.api = new BitBucketAPI({
|
||||
...apiConfig,
|
||||
requestFunction: this.requestFunction,
|
||||
hasWriteAccess: async () => true,
|
||||
});
|
||||
console.log(this.api);
|
||||
this.backend = new BitBucketBackend(this.config, { proxied: true, API: this.api });
|
||||
}
|
||||
|
||||
if (!(await this.api.hasWriteAccess())) {
|
||||
throw new Error("You don't have sufficient permissions to access Netlify CMS");
|
||||
}
|
||||
});
|
||||
}
|
||||
restoreUser() {
|
||||
const user = this.authClient && this.authClient.currentUser();
|
||||
if (!user) return Promise.reject();
|
||||
return this.authenticate(user);
|
||||
}
|
||||
authComponent() {
|
||||
return AuthenticationPage;
|
||||
}
|
||||
logout() {
|
||||
if (window.netlifyIdentity) {
|
||||
return window.netlifyIdentity.logout();
|
||||
}
|
||||
const user = this.authClient.currentUser();
|
||||
return user && user.logout();
|
||||
}
|
||||
getToken() {
|
||||
return this.tokenPromise();
|
||||
}
|
||||
|
||||
entriesByFolder(collection, extension) { return this.backend.entriesByFolder(collection, extension); }
|
||||
entriesByFiles(collection) { return this.backend.entriesByFiles(collection); }
|
||||
fetchFiles(files) { return this.backend.fetchFiles(files); }
|
||||
getEntry(collection, slug, path) { return this.backend.getEntry(collection, slug, path); }
|
||||
getMedia() { return this.backend.getMedia(); }
|
||||
persistEntry(entry, mediaFiles, options) { return this.backend.persistEntry(entry, mediaFiles, options); }
|
||||
persistMedia(mediaFile, options) { return this.backend.persistMedia(mediaFile, options); }
|
||||
deleteFile(path, commitMessage, options) { return this.backend.deleteFile(path, commitMessage, options); }
|
||||
unpublishedEntries() { return this.backend.unpublishedEntries(); }
|
||||
unpublishedEntry(collection, slug) { return this.backend.unpublishedEntry(collection, slug); }
|
||||
updateUnpublishedEntryStatus(collection, slug, newStatus) { return this.backend.updateUnpublishedEntryStatus(collection, slug, newStatus); }
|
||||
deleteUnpublishedEntry(collection, slug) { return this.backend.deleteUnpublishedEntry(collection, slug); }
|
||||
publishUnpublishedEntry(collection, slug) { return this.backend.publishUnpublishedEntry(collection, slug); }
|
||||
traverseCursor(cursor, action) { return this.backend.traverseCursor(cursor, action); }
|
||||
}
|
3
packages/netlify-cms-backend-git-gateway/src/index.js
Normal file
3
packages/netlify-cms-backend-git-gateway/src/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
export GitGatewayBackend from './implementation';
|
||||
export AuthenticationPage from './AuthenticationPage';
|
||||
|
Reference in New Issue
Block a user