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

Commit

Permalink
Authenticate requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rastiqdev committed Jun 22, 2023
1 parent 9dd1a52 commit e38df2b
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions lib/nuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function Nuts(opts) {

// Prefix for all routes
routePrefix: "/",

// Authenticator for non-api endpoints
authHandler: undefined,
})

if (
Expand Down Expand Up @@ -128,6 +131,12 @@ Nuts.prototype._init = function () {
})
}

Nuts.prototype.checkAuth = async function (req, version) {
if (!this.opts.authHandler) return true

return await this.opts.authHandler(req, version)
}

// Perform a hook using promised functions
Nuts.prototype.performQ = function (name, arg, fn) {
var that = this
Expand Down Expand Up @@ -206,7 +215,9 @@ Nuts.prototype._onDownload = function (req, res, next) {
})

// Serve downloads
.then(function (version) {
.then(async function (version) {
if (!(await that.checkAuth(req, version))) return res.sendStatus(403)

var asset

if (filename) {
Expand Down Expand Up @@ -283,13 +294,15 @@ Nuts.prototype.onUpdate = function (req, res, next) {
stripChannel: true,
})
})
.then(function (versions) {
.then(async function (versions) {
var latest = versions[0]

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

if (!(await that.checkAuth(req, version))) return res.sendStatus(403)

// Extract release notes from all versions in range
var notesSlice =
versions.length === 1 ? [versions[0]] : versions.slice(0, -1)
Expand Down Expand Up @@ -339,11 +352,13 @@ Nuts.prototype.onUpdateWin = function (req, res, next) {
channel: channel,
})
})
.then(function (versions) {
.then(async function (versions) {
// Update needed?
var latest = _.first(versions)
if (!latest) throw new Error("Version not found")

if (!(await that.checkAuth(req, version))) return res.sendStatus(403)

// File exists
var asset = _.find(latest.platforms, {
filename: "RELEASES",
Expand Down Expand Up @@ -392,11 +407,13 @@ Nuts.prototype.onServeNotes = function (req, res, next) {
channel: "*",
})
})
.then(function (versions) {
.then(async function (versions) {
var latest = _.first(versions)

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

if (!(await that.checkAuth(req, version))) return res.sendStatus(403)

res.format({
"application/json": function () {
res.send({
Expand Down Expand Up @@ -432,18 +449,20 @@ Nuts.prototype.onServeVersionsFeed = function (req, res, next) {
})
})
.then(function (versions) {
_.each(versions, function (version) {
feed.addItem({
title: version.tag,
link: urljoin(
fullUrl,
"/../../../",
`download/version/${version.tag}`,
),
description: version.notes,
date: version.published_at,
author: [],
})
_.each(versions, async function (version) {
if (await that.checkAuth(req, version)) {
feed.addItem({
title: version.tag,
link: urljoin(
fullUrl,
"/../../../",
`download/version/${version.tag}`,
),
description: version.notes,
date: version.published_at,
author: [],
})
}
})

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

0 comments on commit e38df2b

Please sign in to comment.