* Prevent unauthorized CMS access and enable use of GitHub Enterprise
This commit is contained in:
parent
b294110db7
commit
6805a6936d
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,3 +6,5 @@ npm-debug.log
|
|||||||
.tern-project
|
.tern-project
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
.vscode/
|
.vscode/
|
||||||
|
manifest.yml
|
||||||
|
.imdone/
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import { currentBackend } from '../backends/backend';
|
import { currentBackend } from '../backends/backend';
|
||||||
|
import { actions as notifActions } from 'redux-notifications';
|
||||||
|
|
||||||
|
const { notifSend } = notifActions;
|
||||||
|
|
||||||
export const AUTH_REQUEST = 'AUTH_REQUEST';
|
export const AUTH_REQUEST = 'AUTH_REQUEST';
|
||||||
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
|
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
|
||||||
@ -60,6 +63,11 @@ export function loginUser(credentials) {
|
|||||||
dispatch(authenticate(user));
|
dispatch(authenticate(user));
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
dispatch(notifSend({
|
||||||
|
message: `${ error.message }`,
|
||||||
|
kind: 'warning',
|
||||||
|
dismissAfter: 8000,
|
||||||
|
}));
|
||||||
dispatch(authError(error));
|
dispatch(authError(error));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -19,6 +19,19 @@ export default class API {
|
|||||||
return this.request("/user");
|
return this.request("/user");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isCollaborator(user) {
|
||||||
|
return this.request('/user/repos').then((repos) => {
|
||||||
|
let contributor = false
|
||||||
|
for (const repo of repos) {
|
||||||
|
if (repo.full_name === this.repo && repo.permissions.push) contributor = true;
|
||||||
|
}
|
||||||
|
return contributor;
|
||||||
|
}).catch((error) => {
|
||||||
|
console.error("Problem with response of /user/repos from GitHub");
|
||||||
|
throw error;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
requestHeaders(headers = {}) {
|
requestHeaders(headers = {}) {
|
||||||
const baseHeader = {
|
const baseHeader = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -242,7 +255,6 @@ export default class API {
|
|||||||
const uploadPromises = [];
|
const uploadPromises = [];
|
||||||
const files = mediaFiles.concat(entry);
|
const files = mediaFiles.concat(entry);
|
||||||
|
|
||||||
|
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
if (file.uploaded) { return; }
|
if (file.uploaded) { return; }
|
||||||
uploadPromises.push(this.uploadBlob(file));
|
uploadPromises.push(this.uploadBlob(file));
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
.root {
|
.root {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex-flow: column nowrap;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
|
@ -2,6 +2,8 @@ import React from 'react';
|
|||||||
import Button from 'react-toolbox/lib/button';
|
import Button from 'react-toolbox/lib/button';
|
||||||
import Authenticator from '../../lib/netlify-auth';
|
import Authenticator from '../../lib/netlify-auth';
|
||||||
import { Icon } from '../../components/UI';
|
import { Icon } from '../../components/UI';
|
||||||
|
import { Notifs } from 'redux-notifications';
|
||||||
|
import { Toast } from '../../components/UI/index';
|
||||||
import styles from './AuthenticationPage.css';
|
import styles from './AuthenticationPage.css';
|
||||||
|
|
||||||
export default class AuthenticationPage extends React.Component {
|
export default class AuthenticationPage extends React.Component {
|
||||||
@ -16,7 +18,7 @@ export default class AuthenticationPage extends React.Component {
|
|||||||
const cfg = {
|
const cfg = {
|
||||||
base_url: this.props.base_url,
|
base_url: this.props.base_url,
|
||||||
site_id: (document.location.host.split(':')[0] === 'localhost') ? 'cms.netlify.com' : this.props.siteId
|
site_id: (document.location.host.split(':')[0] === 'localhost') ? 'cms.netlify.com' : this.props.siteId
|
||||||
}
|
};
|
||||||
const auth = new Authenticator(cfg);
|
const auth = new Authenticator(cfg);
|
||||||
|
|
||||||
auth.authenticate({ provider: 'github', scope: 'repo' }, (err, data) => {
|
auth.authenticate({ provider: 'github', scope: 'repo' }, (err, data) => {
|
||||||
@ -33,6 +35,7 @@ export default class AuthenticationPage extends React.Component {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={styles.root}>
|
<section className={styles.root}>
|
||||||
|
<Notifs CustomComponent={Toast} />
|
||||||
{loginError && <p>{loginError}</p>}
|
{loginError && <p>{loginError}</p>}
|
||||||
<Button
|
<Button
|
||||||
className={styles.button}
|
className={styles.button}
|
||||||
|
@ -15,6 +15,7 @@ export default class GitHub {
|
|||||||
|
|
||||||
this.repo = config.getIn(["backend", "repo"], "");
|
this.repo = config.getIn(["backend", "repo"], "");
|
||||||
this.branch = config.getIn(["backend", "branch"], "master");
|
this.branch = config.getIn(["backend", "branch"], "master");
|
||||||
|
this.api_root = config.getIn(["backend", "api_root"], "https://api.github.com");
|
||||||
this.token = '';
|
this.token = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,11 +30,16 @@ export default class GitHub {
|
|||||||
|
|
||||||
authenticate(state) {
|
authenticate(state) {
|
||||||
this.token = state.token;
|
this.token = state.token;
|
||||||
this.api = new API({ token: this.token, branch: this.branch, repo: this.repo });
|
this.api = new API({ token: this.token, branch: this.branch, repo: this.repo, api_root: this.api_root });
|
||||||
return this.api.user().then((user) => {
|
return this.api.user().then(user =>
|
||||||
|
this.api.isCollaborator(user.login).then((isCollab) => {
|
||||||
|
// Unauthorized user
|
||||||
|
if (!isCollab) throw new Error("Your GitHub user account does not have access to this repo.");
|
||||||
|
// Authorized user
|
||||||
user.token = state.token;
|
user.token = state.token;
|
||||||
return user;
|
return user;
|
||||||
});
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getToken() {
|
getToken() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user