Skip to content

Commit

Permalink
Redefining behavior of guard() middleware to respond with a 401 whe…
Browse files Browse the repository at this point in the history
…n unauthenticated, swapping `mimetype` for `mime-db` as source of media types, creating `mime()` to access media types by filename (path)
  • Loading branch information
avoidwork committed Nov 11, 2019
1 parent 3b8e560 commit 96f9d9b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("path"),
fs = require("fs"),
retsu = require("retsu"),
coerce = require("tiny-coerce"),
mime = require("mimetype"),
mime = require(path.join(__dirname, "mime.js")),
{canGet, each, hasBody, jsonWrap} = require(path.join(__dirname, "shared.js")),
regex = require(path.join(__dirname, "regex.js")),
rateHeaders = [
Expand Down Expand Up @@ -72,7 +72,7 @@ function guard (req, res, next) {
if (req.parsed.pathname === login || req.isAuthenticated()) {
next();
} else {
res.redirect(login, false);
res.error(401);
}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ function rate (req, res, next) {

function stream (req, res) {
res.header("content-length", req.file.stats.size);
res.header("content-type", mime.lookup(req.file.path));
res.header("content-type", mime(req.file.path));
res.header("last-modified", req.file.stats.mtime.toUTCString());

let status = 200,
Expand Down
17 changes: 17 additions & 0 deletions lib/mime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {extname} = require("path"),
valid = Object.entries(require("mime-db")).filter(i => "extensions" in i[1]),
extensions = valid.reduce((a, v) => {
const result = Object.assign({type: v[0]}, v[1]);

for (const key of result.extensions) {
a[`.${key}`] = result;
}

return a;
}, {});

module.exports = (arg = "") => {
const ext = extname(arg);

return ext in extensions ? extensions[ext].type : "";
};
6 changes: 2 additions & 4 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function auth (obj, config) {
authMap = {},
authUris = [];

let keys, sesh, fnCookie, fnSession, luscaCsp, luscaCsrf, luscaXframe, luscaP3p, luscaHsts, luscaXssProtection,
let sesh, fnCookie, fnSession, luscaCsp, luscaCsrf, luscaXframe, luscaP3p, luscaHsts, luscaXssProtection,
luscaNoSniff,
passportInit, passportSession;

Expand Down Expand Up @@ -355,9 +355,7 @@ function auth (obj, config) {
}

if (authUris.length > 0) {
keys = Object.keys(authMap).length > 0;

if (keys) {
if (Object.keys(authMap).length > 0) {
config.routes.get[config.auth.uri.root] = authMap;
}

Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is an HTTP/HTTP2 REST API framework",
"version": "14.0.1",
"version": "15.0.0",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"express-session": "^1.17.0",
"keysort": "^1.0.2",
"lusca": "^1.6.1",
"mimetype": "^0.0.8",
"mime-db": "^1.42.0",
"moment": "^2.24.0",
"passport": "^0.4.0",
"passport-http": "^0.3.0",
Expand Down
3 changes: 1 addition & 2 deletions test/auth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ describe("Local", function () {
it("GET /uuid (invalid) - returns an 'unauthorized' error", function () {
return tinyhttptest({url: "http://localhost:" + port + "/uuid"})
.cookies()
.expectStatus(302)
.expectHeader("location", login)
.expectStatus(401)
.end();
});

Expand Down
3 changes: 1 addition & 2 deletions test/auth_test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ describe("Local (HTTP2)", function () {
it("GET /uuid (invalid) - returns an 'unauthorized' error", function () {
return tinyhttptest({http2: true, url: "https://localhost:" + port + "/uuid"})
.cookies()
.expectStatus(302)
.expectHeader("location", login)
.expectStatus(401)
.end();
});

Expand Down

0 comments on commit 96f9d9b

Please sign in to comment.