Skip to content

Commit

Permalink
Add test for digest auth
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Jul 7, 2019
1 parent 04fe2c7 commit b3b1654
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
20 changes: 12 additions & 8 deletions source/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ function generateBasicAuthHeader(username, password) {
return `Basic ${encoded}`;
}

function generateTokenAuthHeader(tokenInfo) {
return `${tokenInfo.token_type} ${tokenInfo.access_token}`;
}

function generateDigestAuthHeader(options, digest) {
const url = options.url.replace("//", "");
const uri = url.indexOf("/") == -1 ? "/" : url.slice(url.indexOf("/"));

const method = options.method ? options.method.toUpperCase() : "GET";

const qop = /(^|,)\s*auth\s*($|,)/.test(digest.qop) ? "auth" : false;
const ncString = `00000000${digest.nc}`.slice(-8);
const cnonce = digest.cnonce;
const ha1 = ha1Compute(digest.algorithm, digest.username, digest.realm, digest.password, digest.nonce, digest.cnonce);
const ha1 = ha1Compute(
digest.algorithm,
digest.username,
digest.realm,
digest.password,
digest.nonce,
digest.cnonce
);
const ha2 = md5(`${method}:${uri}`);

const digestResponse = qop
? md5(`${ha1}:${digest.nonce}:${ncString}:${digest.cnonce}:${qop}:${ha2}`)
: md5(`${ha1}:${digest.nonce}:${ha2}`);
Expand Down Expand Up @@ -53,6 +53,10 @@ function generateDigestAuthHeader(options, digest) {
return `Digest ${authHeader.join(", ")}`;
}

function generateTokenAuthHeader(tokenInfo) {
return `${tokenInfo.token_type} ${tokenInfo.access_token}`;
}

module.exports = {
generateBasicAuthHeader,
generateTokenAuthHeader,
Expand Down
15 changes: 11 additions & 4 deletions test/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ function createServer(dir, authType) {
}
const userManager = new ws.SimpleUserManager();
const user = userManager.addUser("webdav-user", "pa$$w0rd!");
const auth =
!authType || authType === "basic"
? new ws.HTTPBasicAuthentication(userManager)
: new ws.HTTPDigestAuthentication(userManager, "test");
let auth;
switch (authType) {
case "digest":
auth = new ws.HTTPDigestAuthentication(userManager, "test");
break;
case "basic":
/* falls-through */
default:
auth = new ws.HTTPBasicAuthentication(userManager);
break;
}
const privilegeManager = new ws.SimplePathPrivilegeManager();
privilegeManager.setRights(user, "/", ["all"]);
const server = new ws.WebDAVServer({
Expand Down
31 changes: 28 additions & 3 deletions test/specs/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe("Authentication", function() {
afterEach(function() {
nock.cleanAll();
});
it("should go unauthenticated if no credentials are passed", function() {

it("should connect unauthenticated if no credentials are passed", function() {
nock(DUMMYSERVER)
.get("/file")
.reply(200, function() {
Expand All @@ -18,7 +19,7 @@ describe("Authentication", function() {
return webdav.getFileContents("/file");
});

it("should use HTTP Basic if user and password are provided", function() {
it("should connect using HTTP Basic if user and password are provided", function() {
nock(DUMMYSERVER)
.get("/file")
.reply(200, function() {
Expand All @@ -32,7 +33,7 @@ describe("Authentication", function() {
return webdav.getFileContents("/file");
});

it("should use Bearer if an object is provided", function() {
it("should connect using a Bearer token if an object is provided", function() {
nock(DUMMYSERVER)
.get("/file")
.reply(200, function() {
Expand All @@ -47,4 +48,28 @@ describe("Authentication", function() {
});
return webdav.getFileContents("/file");
});

describe("using Digest-enabled server", function() {
beforeEach(function() {
this.client = createWebDAVClient("http://localhost:9988/webdav/server", {
username: createWebDAVServer.test.username,
password: createWebDAVServer.test.password,
digest: true
});
clean();
this.server = createWebDAVServer("digest");
return this.server.start();
});

afterEach(function() {
return this.server.stop();
});

it("should connect using Digest authentication if digest enabled", function() {
return this.client.getDirectoryContents("/").then(function(contents) {
expect(contents).to.be.an("array");
expect(contents[0]).to.be.an("object");
});
});
});
});

0 comments on commit b3b1654

Please sign in to comment.