Move denyNotLogged function to typescript

pull/33/head
Clement Michaud 2017-05-21 13:11:54 +02:00
parent fad23ff3be
commit e3257b81a5
6 changed files with 105 additions and 107 deletions

View File

@ -0,0 +1,18 @@
import objectPath = require("object-path");
import express = require("express");
export = function denyNotLogged(callback: (req: express.Request, res: express.Response) => void) {
return function (req: express.Request, res: express.Response) {
const auth_session = req.session.auth_session;
const first_factor = objectPath.has(req, "session.auth_session.first_factor")
&& req.session.auth_session.first_factor;
if (!first_factor) {
res.status(403);
res.send();
return;
}
callback(req, res);
};
};

View File

@ -1,19 +0,0 @@
module.exports = denyNotLogged;
var objectPath = require('object-path');
function denyNotLogged(next) {
return function(req, res) {
var auth_session = req.session.auth_session;
var first_factor = objectPath.has(req, 'session.auth_session.first_factor')
&& req.session.auth_session.first_factor;
if(!first_factor) {
res.status(403);
res.send();
return;
}
next(req, res);
}
}

View File

@ -1,18 +1,18 @@
var denyNotLogged = require('./deny_not_logged'); var DenyNotLogged = require('./DenyNotLogged');
var u2f = require('./u2f'); var u2f = require('./u2f');
var TOTPAuthenticator = require("./TOTPAuthenticator"); var TOTPAuthenticator = require("./TOTPAuthenticator");
module.exports = { module.exports = {
totp: denyNotLogged(TOTPAuthenticator), totp: DenyNotLogged(TOTPAuthenticator),
u2f: { u2f: {
register_request: u2f.register_request, register_request: u2f.register_request,
register: u2f.register, register: u2f.register,
register_handler_get: u2f.register_handler_get, register_handler_get: u2f.register_handler_get,
register_handler_post: u2f.register_handler_post, register_handler_post: u2f.register_handler_post,
sign_request: denyNotLogged(u2f.sign_request), sign_request: DenyNotLogged(u2f.sign_request),
sign: denyNotLogged(u2f.sign), sign: DenyNotLogged(u2f.sign),
} }
} }

View File

@ -15,7 +15,7 @@ export interface ResponseMock {
sendStatus: sinon.SinonStub; sendStatus: sinon.SinonStub;
sendFile: sinon.SinonStub; sendFile: sinon.SinonStub;
sendfile: sinon.SinonStub; sendfile: sinon.SinonStub;
status: sinon.SinonStub; status: sinon.SinonStub | sinon.SinonSpy;
json: sinon.SinonStub; json: sinon.SinonStub;
links: sinon.SinonStub; links: sinon.SinonStub;
jsonp: sinon.SinonStub; jsonp: sinon.SinonStub;

View File

@ -0,0 +1,82 @@
import sinon = require("sinon");
import Promise = require("bluebird");
import assert = require("assert");
import express = require("express");
import ExpressMock = require("../mocks/express");
import DenyNotLogged = require("../../../src/lib/routes/DenyNotLogged");
describe("test not logged", function () {
it("should return status code 403 when auth_session has not been previously created", function () {
return test_auth_session_not_created();
});
it("should return status code 403 when auth_session has failed first factor", function () {
return test_auth_first_factor_not_validated();
});
it("should return status code 204 when auth_session has succeeded first factor stage", function () {
return test_auth_with_first_factor_validated();
});
});
function test_auth_session_not_created() {
return new Promise(function (resolve, reject) {
const send = sinon.spy(resolve);
const status = sinon.spy(function (code: number) {
assert.equal(403, code);
});
const req = ExpressMock.RequestMock();
const res = ExpressMock.ResponseMock();
req.session = {};
res.send = send;
res.status = status;
DenyNotLogged(reject)(req as any, res as any);
});
}
function test_auth_first_factor_not_validated() {
return new Promise(function (resolve, reject) {
const send = sinon.spy(resolve);
const status = sinon.spy(function (code: number) {
assert.equal(403, code);
});
const req = {
session: {
auth_session: {
first_factor: false,
second_factor: false
}
}
};
const res = {
send: send,
status: status
};
DenyNotLogged(reject)(req as any, res as any);
});
}
function test_auth_with_first_factor_validated() {
return new Promise(function (resolve, reject) {
const req = {
session: {
auth_session: {
first_factor: true,
second_factor: false
}
}
};
const res = {
send: sinon.spy(),
status: sinon.spy()
};
DenyNotLogged(resolve)(req as any, res as any);
});
}

View File

@ -1,83 +0,0 @@
var sinon = require('sinon');
var Promise = require('bluebird');
var assert = require('assert');
var denyNotLogged = require('../../../src/lib/routes/deny_not_logged');
describe('test not logged', function() {
it('should return status code 403 when auth_session has not been previously created', function() {
return test_auth_session_not_created();
});
it('should return status code 403 when auth_session has failed first factor', function() {
return test_auth_first_factor_not_validated();
});
it('should return status code 204 when auth_session has succeeded first factor stage', function() {
return test_auth_with_first_factor_validated();
});
});
function test_auth_session_not_created() {
return new Promise(function(resolve, reject) {
var send = sinon.spy(resolve);
var status = sinon.spy(function(code) {
assert.equal(403, code);
});
var req = {
session: {}
}
var res = {
send: send,
status: status
}
denyNotLogged(reject)(req, res);
});
}
function test_auth_first_factor_not_validated() {
return new Promise(function(resolve, reject) {
var send = sinon.spy(resolve);
var status = sinon.spy(function(code) {
assert.equal(403, code);
});
var req = {
session: {
auth_session: {
first_factor: false,
second_factor: false
}
}
}
var res = {
send: send,
status: status
}
denyNotLogged(reject)(req, res);
});
}
function test_auth_with_first_factor_validated() {
return new Promise(function(resolve, reject) {
var req = {
session: {
auth_session: {
first_factor: true,
second_factor: false
}
}
}
var res = {
send: sinon.spy(),
status: sinon.spy()
}
denyNotLogged(resolve)(req, res);
});
}