Skip to content

Commit

Permalink
Feat/retrieve csp (#70)
Browse files Browse the repository at this point in the history
* feat: add NetlifyToml class for retrieving netlify.toml file through Github API

* feat: add netlifyToml route file for retrieving, decoding, and sending netlify.toml

* feat: adds netlifyTomlRouter and auth
  • Loading branch information
gweiying authored Nov 16, 2020
1 parent 5ec11df commit 4a91244
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
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 @@ -115,6 +115,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

0 comments on commit 4a91244

Please sign in to comment.