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

fix(auth): also use Basic Authentication information database opts #208

Merged
merged 1 commit into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ function getSessionUrl(db) {
}

function getBasicAuthHeaders(db) {
var url = urlParse(db.name);
if (!url.auth) {
var auth;

if (db.__opts.auth) {
auth = db.__opts.auth;
} else {
var url = urlParse(db.name);
if (url.auth) {
auth = url;
}
}

if (!auth) {
return {};
}

var str = url.username + ':' + url.password;
var str = auth.username + ':' + auth.password;
var token = btoa(unescape(encodeURIComponent(str)));
return {Authorization: 'Basic ' + token};
}
Expand Down
62 changes: 62 additions & 0 deletions test/test.issue.204.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

var PouchDB = require('pouchdb-memory');
var Authentication = require('../lib');
PouchDB.plugin(Authentication);

var utils = require('./test-utils');
var chai = require('chai');
chai.should();

var serverHost = utils.getConfig().serverHost;

describe('issue-204', function () {

var dbName = serverHost + '/testdb';

var db;

beforeEach(function () {
db = new PouchDB(dbName);
return utils.ensureUsersDatabaseExists(db).then(function () {
return db.signUpAdmin('anna', 'secret');
}).then(function () {
return db.signup('spiderman', 'will-remember');
});
});

afterEach(function () {
return db.logIn('anna', 'secret').then(function () {
return db.deleteUser('spiderman');
}).then(function () {
return db.deleteAdmin('anna');
}).then(function () {
return db.logOut();
}).then(function () {
return db.destroy();
});
});

function testGetUser(db) {
return db.getUser('spiderman').then(function (res) {
res.name.should.equal('spiderman');
});
}

it('Test get user with basic authentication through URL', function () {
var dbNameWithAuth = dbName.replace('http://', 'http://spiderman:will-remember@');
var dbWithBasicAuth = new PouchDB(dbNameWithAuth, {skip_setup: true});
return testGetUser(dbWithBasicAuth);
});

it('Test get user with basic authentication through PouchDB opts', function () {
var dbWithBasicAuth = new PouchDB(dbName, {
skip_setup: true,
auth: {
username: 'spiderman',
password: 'will-remember',
},
});
return testGetUser(dbWithBasicAuth);
});
});