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/retrieve csp #70

Merged
merged 3 commits into from
Nov 16, 2020
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
44 changes: 44 additions & 0 deletions classes/NetlifyToml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const axios = require('axios')
const validateStatus = require('../utils/axios-utils')

// Import error
const { NotFoundError } = require('../errors/NotFoundError')

const GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME
const BRANCH_REF = process.env.BRANCH_REF

class NetlifyToml {
constructor(accessToken, siteName) {
this.accessToken = accessToken
this.siteName = siteName
}

async read() {
try {
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/netlify.toml`

const params = {
"ref": BRANCH_REF,
}

const resp = await axios.get(endpoint, {
validateStatus,
params,
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json"
}
})

if (resp.status === 404) throw new NotFoundError ('netlify.toml file does not exist')

const { content, sha } = resp.data
return { content, sha }

} catch (err) {
throw err
}
}
}

module.exports = { NetlifyToml }
3 changes: 3 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ auth.post('/sites/:siteName/resources/:resourceName/rename/:newResourceName', ve
auth.get('/sites/:siteName/settings', verifyJwt)
auth.post('/sites/:siteName/settings', verifyJwt)

// Netlify toml
auth.get('/sites/:siteName/netlify-toml', verifyJwt)

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

Expand Down
32 changes: 32 additions & 0 deletions routes/netlifyToml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express')
const router = express.Router()
const toml = require('toml')

// Import middleware
const { attachRouteHandlerWrapper } = require('../middleware/routeHandler')

// Import classes
const { NetlifyToml } = require('../classes/NetlifyToml')

// List resources
async function getNetlifyToml (req, res, next) {
const { accessToken } = req
const { siteName } = req.params

const netlifyTomlFile = new NetlifyToml(accessToken, siteName)

const { content } = await netlifyTomlFile.read()

// Convert to readable form
const netlifyTomlReadableContent = toml.parse(Base64.decode(content))

// Headers is an array of objects, specifying a set of access rules for each specified path
// Under our current assumption, we apply the first set of access rules to all paths
const netlifyTomlHeaderValues = netlifyTomlReadableContent.headers[0].values

res.status(200).json({ netlifyTomlHeaderValues })
}

router.get('/:siteName/netlify-toml', attachRouteHandlerWrapper(getNetlifyToml))

module.exports = router;
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const menuRouter = require('./routes/menus')
const homepageRouter = require('./routes/homepage')
const menuDirectoryRouter = require('./routes/menuDirectory')
const settingsRouter = require('./routes/settings')
const netlifyTomlRouter = require('./routes/netlifyToml')

const app = express();

Expand Down Expand Up @@ -61,6 +62,7 @@ app.use('/sites', menuRouter)
app.use('/sites', homepageRouter)
app.use('/sites', menuDirectoryRouter)
app.use('/sites', settingsRouter)
app.use('/sites', netlifyTomlRouter)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down