authelia/server/test/routes/secondfactor/u2f/sign/post.test.ts
Clement Michaud 6b78240d39 Fix endpoints redirection on errors
From this commit on, api endpoints reply with a 401 error code and non api
endpoints redirect to /error/40X.

This commit also fixes missing restrictions on /loggedin (the "already logged
in page). This was not a security issue, though.

The change also makes error pages automatically redirect the user after few
seconds based on the referrer or the default_redirection_url if provided in the
configuration.

Warning: The old /verify endpoint of the REST API has moved to /api/verify.
You will need to update your nginx configuration to take this change into
account.
2017-11-01 14:46:23 +01:00

102 lines
2.8 KiB
TypeScript

import sinon = require("sinon");
import BluebirdPromise = require("bluebird");
import Assert = require("assert");
import U2FSignPost = require("../../../../../src/lib/routes/secondfactor/u2f/sign/post");
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
import winston = require("winston");
import { ServerVariablesMockBuilder, ServerVariablesMock } from "../../../../mocks/ServerVariablesMockBuilder";
import ExpressMock = require("../../../../mocks/express");
import U2FMock = require("../../../../mocks/u2f");
import U2f = require("u2f");
describe("test u2f routes: sign", function () {
let req: ExpressMock.RequestMock;
let res: ExpressMock.ResponseMock;
let mocks: ServerVariablesMock;
let vars: ServerVariables;
beforeEach(function () {
req = ExpressMock.RequestMock();
req.app = {};
req.originalUrl = "/api/xxxx";
const s = ServerVariablesMockBuilder.build();
mocks = s.mocks;
vars = s.variables;
req.session = {
auth: {
userid: "user",
first_factor: true,
second_factor: false,
identity_check: {
challenge: "u2f-register",
userid: "user"
}
}
};
req.headers = {};
req.headers.host = "localhost";
const options = {
inMemoryOnly: true
};
res = ExpressMock.ResponseMock();
res.send = sinon.spy();
res.json = sinon.spy();
res.status = sinon.spy();
});
it("should return status code 204", function () {
const expectedStatus = {
keyHandle: "keyHandle",
publicKey: "pbk",
certificate: "cert"
};
mocks.u2f.checkSignatureStub.returns(expectedStatus);
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({
registration: {
publicKey: "PUBKEY"
}
}));
req.session.auth.sign_request = {
appId: "app",
challenge: "challenge",
keyHandle: "key",
version: "U2F_V2"
};
return U2FSignPost.default(vars)(req as any, res as any)
.then(function () {
Assert(req.session.auth.second_factor);
});
});
it("should return unauthorized error on registration request internal error", function () {
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({
registration: {
publicKey: "PUBKEY"
}
}));
mocks.u2f.checkSignatureStub.returns({ errorCode: 500 });
req.session.auth.sign_request = {
appId: "app",
challenge: "challenge",
keyHandle: "key",
version: "U2F_V2"
};
return U2FSignPost.default(vars)(req as any, res as any)
.then(function () {
Assert.equal(res.status.getCall(0).args[0], 200);
Assert.deepEqual(res.send.getCall(0).args[0],
{ error: "Operation failed." });
});
});
});