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

Feat: add endpoint to check if user has access to site #89

Merged
merged 2 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ auth.get('/sites/:siteName/netlify-toml', verifyJwt)

// Sites
auth.get('/sites', verifyJwt)
auth.get('/sites/:siteName', verifyJwt)

auth.use((req, res, next) => {
if (!req.route) {
Expand Down
28 changes: 28 additions & 0 deletions routes/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const router = express.Router();
const axios = require('axios');
const _ = require('lodash');
const { attachRouteHandlerWrapper } = require('../middleware/routeHandler');
// Import error
const { NotFoundError } = require('../errors/NotFoundError')

const ISOMER_GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME
const ISOMER_ADMIN_REPOS = [
Expand Down Expand Up @@ -90,6 +92,32 @@ async function getSites (req, res, next) {
res.status(200).json({ siteNames })
}

/* Checks if a user has access to a repo. */
async function checkHasAccess (req, res, next) {
try {
const { accessToken, userId } = req
const { siteName } = req.params

const endpoint = `https://api.github.com/repos/${ISOMER_GITHUB_ORG_NAME}/${siteName}/collaborators/${userId}`
const resp = await axios.get(endpoint, {
headers: {
Authorization: `token ${accessToken}`,
"Content-Type": "application/json",
}
})
console.log(resp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this log statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops thanks for catch that, resolved in ba92d4b


res.status(200).json()
} catch (err) {
const status = err.response.status
// If user is unauthorized or site does not exist, show the same NotFoundError
if (status === 404 || status === 403) throw new NotFoundError('Site does not exist')
console.log(err)
throw err
}
}

router.get('/', attachRouteHandlerWrapper(getSites));
router.get('/:siteName', attachRouteHandlerWrapper(checkHasAccess));

module.exports = router;