Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Git Gateway users with invalid tokens from logging in. #1209

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/backends/git-gateway/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ export default class API extends GithubAPI {
this.repoURL = "";
}

hasWriteAccess() {
return this.getBranch()
.then(() => true)
.catch(error => {
if (error.status === 401) {
if (error.message === "Bad credentials") {
throw new Error("Git Gateway Error: Please ask your site administrator to reissue the Git Gateway token.");
} else {
return false;
}
} else {
console.error("Problem fetching repo data from GitHub");
throw error;
}
});
}

getRequestHeaders(headers = {}) {
return this.tokenPromise()
Expand Down
11 changes: 10 additions & 1 deletion src/backends/git-gateway/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ export default class GitGateway extends GitHubBackend {
} else {
throw new Error("You don't have sufficient permissions to access Netlify CMS");
}
});
})
.then(userData =>
this.api.hasWriteAccess().then(canWrite => {
if (canWrite) {
return userData;
} else {
throw new Error("You don't have sufficient permissions to access Netlify CMS");
}
})
);
}

logout() {
Expand Down