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

Authenticate requests #175

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 33 additions & 10 deletions lib/nuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function Nuts(opts) {
preFetch: true,

// Secret for GitHub webhook
refreshSecret: 'secret'
refreshSecret: 'secret',

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

// .init() is now a memoized version of ._init()
Expand Down Expand Up @@ -95,6 +98,13 @@ 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) {
Expand Down Expand Up @@ -173,7 +183,10 @@ 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 @@ -229,10 +242,13 @@ Nuts.prototype.onUpdate = function(req, res, next) {
channel: channel
});
})
.then(function(versions) {
.then(async function(versions) {
var latest = _.first(versions);
if (!latest || latest.tag == tag) return res.status(204).send('No updates');

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

var notesSlice = versions.slice(0, -1);
if (versions.length === 1) {
notesSlice = [versions[0]];
Expand Down Expand Up @@ -271,11 +287,14 @@ 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, latest)))
return res.sendStatus(403)

// File exists
var asset = _.find(latest.platforms, {
filename: 'RELEASES'
Expand Down Expand Up @@ -320,11 +339,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, latest)))
return res.sendStatus(403)

res.format({
'text/plain': function(){
res.send(notes.merge(versions));
Expand Down Expand Up @@ -363,14 +384,16 @@ Nuts.prototype.onServeVersionsFeed = function(req, res, next) {
});
})
.then(function(versions) {
_.each(versions, function(version) {
feed.addItem({
_.each(versions, async function(version) {
if (await that.checkAuth(req, version)) {
feed.addItem({
title: version.tag,
link: urljoin(fullUrl, '/../../../', '/download/version/'+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