Skip to content

Commit

Permalink
Prevent Git Gateway users with invalid tokens from logging in. (decap…
Browse files Browse the repository at this point in the history
…org#1209)

* Prevent Git Gateway users without permission from login.

* Handle Git Gateway token expiry explicitly.

This often happens when a user changes a repo from public to private, so
we want to make that specific case very clear.
  • Loading branch information
tech4him1 authored and brianlmacdonald committed May 23, 2018
1 parent efe22c9 commit 1149560
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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

0 comments on commit 1149560

Please sign in to comment.