Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit e38df2b

Browse files
committed
Authenticate requests
From GitbookIO#175
1 parent 9dd1a52 commit e38df2b

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

lib/nuts.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ function Nuts(opts) {
4040

4141
// Prefix for all routes
4242
routePrefix: "/",
43+
44+
// Authenticator for non-api endpoints
45+
authHandler: undefined,
4346
})
4447

4548
if (
@@ -128,6 +131,12 @@ Nuts.prototype._init = function () {
128131
})
129132
}
130133

134+
Nuts.prototype.checkAuth = async function (req, version) {
135+
if (!this.opts.authHandler) return true
136+
137+
return await this.opts.authHandler(req, version)
138+
}
139+
131140
// Perform a hook using promised functions
132141
Nuts.prototype.performQ = function (name, arg, fn) {
133142
var that = this
@@ -206,7 +215,9 @@ Nuts.prototype._onDownload = function (req, res, next) {
206215
})
207216

208217
// Serve downloads
209-
.then(function (version) {
218+
.then(async function (version) {
219+
if (!(await that.checkAuth(req, version))) return res.sendStatus(403)
220+
210221
var asset
211222

212223
if (filename) {
@@ -283,13 +294,15 @@ Nuts.prototype.onUpdate = function (req, res, next) {
283294
stripChannel: true,
284295
})
285296
})
286-
.then(function (versions) {
297+
.then(async function (versions) {
287298
var latest = versions[0]
288299

289300
// Already using latest version?
290301
if (!latest || latest.tag == tag)
291302
return res.status(204).send("No updates")
292303

304+
if (!(await that.checkAuth(req, version))) return res.sendStatus(403)
305+
293306
// Extract release notes from all versions in range
294307
var notesSlice =
295308
versions.length === 1 ? [versions[0]] : versions.slice(0, -1)
@@ -339,11 +352,13 @@ Nuts.prototype.onUpdateWin = function (req, res, next) {
339352
channel: channel,
340353
})
341354
})
342-
.then(function (versions) {
355+
.then(async function (versions) {
343356
// Update needed?
344357
var latest = _.first(versions)
345358
if (!latest) throw new Error("Version not found")
346359

360+
if (!(await that.checkAuth(req, version))) return res.sendStatus(403)
361+
347362
// File exists
348363
var asset = _.find(latest.platforms, {
349364
filename: "RELEASES",
@@ -392,11 +407,13 @@ Nuts.prototype.onServeNotes = function (req, res, next) {
392407
channel: "*",
393408
})
394409
})
395-
.then(function (versions) {
410+
.then(async function (versions) {
396411
var latest = _.first(versions)
397412

398413
if (!latest) throw new Error("No versions matching")
399414

415+
if (!(await that.checkAuth(req, version))) return res.sendStatus(403)
416+
400417
res.format({
401418
"application/json": function () {
402419
res.send({
@@ -432,18 +449,20 @@ Nuts.prototype.onServeVersionsFeed = function (req, res, next) {
432449
})
433450
})
434451
.then(function (versions) {
435-
_.each(versions, function (version) {
436-
feed.addItem({
437-
title: version.tag,
438-
link: urljoin(
439-
fullUrl,
440-
"/../../../",
441-
`download/version/${version.tag}`,
442-
),
443-
description: version.notes,
444-
date: version.published_at,
445-
author: [],
446-
})
452+
_.each(versions, async function (version) {
453+
if (await that.checkAuth(req, version)) {
454+
feed.addItem({
455+
title: version.tag,
456+
link: urljoin(
457+
fullUrl,
458+
"/../../../",
459+
`download/version/${version.tag}`,
460+
),
461+
description: version.notes,
462+
date: version.published_at,
463+
author: [],
464+
})
465+
}
447466
})
448467

449468
res.set("Content-Type", "application/atom+xml; charset=utf-8")

0 commit comments

Comments
 (0)