Skip to content

Commit

Permalink
Merge pull request #30 from pontusmelke/1.0-auth
Browse files Browse the repository at this point in the history
Authentication support for driver
  • Loading branch information
jakewins committed Feb 25, 2016
2 parents ecb267c + 800f444 commit 3447cbb
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ We build a special browser version of the driver, which supports connecting to N
This will make a global `neo4j` object available, where you can access the `v1` API at `neo4j.v1`:

```javascript
var driver = neo4j.v1.driver("bolt://localhost");
var driver = neo4j.v1.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
```

## Usage examples

```javascript

// Create a driver instance
var driver = neo4j.driver("bolt://localhost");
// Create a driver instance, for the user neo4j with password neo4j.
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));

// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
Expand Down
11 changes: 8 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ gulp.task('default', ["test"]);

gulp.task('browser', function(cb){
runSequence('build-browser-test', 'build-browser', cb);
})
});

/** Build all-in-one files for use in the browser */
gulp.task('build-browser', function () {
Expand Down Expand Up @@ -168,7 +168,7 @@ gulp.task('watch-n-test', ['test-nodejs'], function () {

var neo4jLinuxUrl = 'http://alpha.neohq.net/dist/neo4j-enterprise-3.0.0-NIGHTLY-unix.tar.gz';
var neo4jWinUrl = 'http://alpha.neohq.net/dist/neo4j-enterprise-3.0.0-NIGHTLY-windows.zip';
var neo4jHome = './build/neo4j-enterprise-3.0.0-M02';
var neo4jHome = './build/neo4j-enterprise-3.0.0';
var isWin = /^win/.test(process.platform);

gulp.task('download-neo4j', function() {
Expand All @@ -187,6 +187,11 @@ gulp.task('download-neo4j', function() {
}
});

gulp.task('set-password', ['download-neo4j'], function() {
return gulp.src('test/resources/auth')
.pipe(gulp.dest(neo4jHome + "/data/dbms/"));
});

var featureFiles = 'https://s3-eu-west-1.amazonaws.com/remoting.neotechnology.com/driver-compliance/tck.tar.gz';
var featureHome = './build/tck';

Expand Down Expand Up @@ -232,7 +237,7 @@ gulp.task('set', function() {

});

gulp.task('start-neo4j', ['download-neo4j'], function() {
gulp.task('start-neo4j', ['set-password'], function() {
if(isWin) {
runPowershell('Install-Neo4jServer -Neo4jServer ' + neo4jHome + ' -Name neo4j-js;' +
'Start-Neo4jServer -Neo4jServer ' + neo4jHome + ' -ServiceName neo4j-js');
Expand Down
7 changes: 4 additions & 3 deletions src/v1/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class Driver {
* @constructor
* @param {string} url
* @param {string} userAgent
* @param {Object} token
*/
constructor(url, userAgent) {
constructor(url, userAgent, token) {
this._url = url;
this._userAgent = userAgent || 'neo4j-javascript/0.0';
this._openSessions = {};
this._sessionIdGenerator = 0;
this._token = token || {};
}

/**
Expand All @@ -44,8 +46,7 @@ class Driver {
session() {
let sessionId = this._sessionIdGenerator++;
let conn = connect(this._url);
conn.initialize(this._userAgent);

conn.initialize(this._userAgent, this._token);
let _driver = this;
let _session = new Session( conn, () => {
// On close of session, remove it from the list of open sessions
Expand Down
7 changes: 5 additions & 2 deletions src/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {VERSION} from '../version';
let USER_AGENT = "neo4j-javascript/" + VERSION;

export default {
driver: (url) => new Driver(url, USER_AGENT),
driver: (url, token) => new Driver(url, USER_AGENT, token),
int: int,
isInt: isInt
isInt: isInt,
auth : {
basic : (username, password) => { return { scheme:"basic", principal : username, credentials : password };}
}
}
4 changes: 2 additions & 2 deletions src/v1/internal/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ class Connection {
}

/** Queue an INIT-message to be sent to the database */
initialize( clientName, observer ) {
initialize( clientName, token, observer ) {
this._queueObserver(observer);
this._packer.packStruct( INIT, [clientName] );
this._packer.packStruct( INIT, [clientName, token] );
this._chunker.messageBoundary();
}

Expand Down
11 changes: 6 additions & 5 deletions test/internal/connector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ var DummyChannel = require('../../lib/v1/internal/ch-dummy.js');
var connect = require("../../lib/v1/internal/connector.js").connect;

describe('connector', function() {
it('should read/write basic messages', function(done) {

fit('should read/write basic messages', function(done) {
// Given
var conn = connect("bolt://localhost")

// When
conn.initialize( "mydriver/0.0.0", {
conn.initialize( "mydriver/0.0.0", {scheme: "basic", principal: "neo4j", credentials: "neo4j"}, {
onCompleted: function( msg ) {
expect( msg ).not.toBeNull();
conn.close();
Expand All @@ -44,7 +45,7 @@ describe('connector', function() {

// When
var records = [];
conn.initialize( "mydriver/0.0.0" );
conn.initialize( "mydriver/0.0.0", {scheme: "basic", principal: "neo4j", credentials: "neo4j"} );
conn.run( "RETURN 1.0", {} );
conn.pullAll( {
onNext: function( record ) {
Expand All @@ -66,10 +67,10 @@ describe('connector', function() {

// When
var records = [];
conn.initialize( "mydriver/0.0.0" );
conn.initialize( "mydriver/0.0.0", {scheme: "basic", principal: "neo4j", credentials: "neo4j"} );
conn.run( "RETURN 1", {} );
conn.sync();
expect( observer.instance.toHex() ).toBe( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 11 b1 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' );
expect( observer.instance.toHex() ).toBe( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 41 b2 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 a3 86 73 63 68 65 6d 65 85 62 61 73 69 63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72 65 64 65 6e 74 69 61 6c 73 85 6e 65 6f 34 6a 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' );
done();
});

Expand Down
5 changes: 2 additions & 3 deletions test/neo4j-driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ describe('neo4j-driver', function() {
it('should expose version 1 of the API as a property', function(done) {
// When
var neo4jDriver = require("../lib");

// Then I can access and use V1 of the API
var driver = neo4jDriver.v1.driver("bolt://localhost");
var driver = neo4jDriver.v1.driver("bolt://localhost", neo4jDriver.v1.auth.basic("neo4j", "neo4j"));
driver.session().run( "RETURN 1" )
.then( function() { driver.close(); })
.then( done );
Expand All @@ -34,7 +33,7 @@ describe('neo4j-driver', function() {
var neo4jV1 = require("../lib/v1");

// Then I can access and use V1 of the API
var driver = neo4jV1.driver("bolt://localhost");
var driver = neo4jV1.driver("bolt://localhost", neo4jV1.auth.basic("neo4j", "neo4j"));
driver.session().run( "RETURN 1" )
.then( function() { driver.close(); })
.then( done );
Expand Down
1 change: 1 addition & 0 deletions test/resources/auth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neo4j:SHA-256,E4B4C20A3933637A1EE5ADBDE16290F6915E516A67216E35CAE1208A86F91E3B,2801E325BE8A074BD92F1A4B692AE167:
3 changes: 2 additions & 1 deletion test/v1/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
var neo4j = require("../../lib/v1");

describe('driver', function() {

it('should expose sessions', function() {
// Given
var driver = neo4j.driver("bolt://localhost");
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));

// When
var session = driver.session();
Expand Down
4 changes: 2 additions & 2 deletions test/v1/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('transaction', function() {
var driver, session, out, console;

beforeEach(function(done) {
driver = neo4j.driver("bolt://localhost");
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
session = driver.session();

// Override console.log, to assert on stdout output
Expand All @@ -42,7 +42,7 @@ describe('transaction', function() {

it('should document a minimum viable snippet', function(done) {
// tag::minimum-snippet[]
var driver = neo4j.driver("bolt://localhost");
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();

session.run( "CREATE (neo:Person {name:'Neo', age:23})" );
Expand Down
2 changes: 1 addition & 1 deletion test/v1/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('session', function() {
var driver, session;

beforeEach(function(done) {
driver = neo4j.driver("bolt://localhost");
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
session = driver.session();

session.run("MATCH (n) DETACH DELETE n").then(done);
Expand Down
4 changes: 2 additions & 2 deletions test/v1/tck/steps/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function () {
var failedScenarios = []

this.Before("@reset_database", function( scenario, callback ) {
this.driver = neo4j.driver("bolt://localhost");
this.driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
this.session = this.driver.session();
this.session.run("MATCH (n) DETACH DELETE n").then( function( ) {
callback();
Expand All @@ -14,7 +14,7 @@ module.exports = function () {
});

this.Before("~@reset_database", function( scenario, callback ) {
this.driver = neo4j.driver("bolt://localhost");
this.driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
this.session = this.driver.session();
callback();
});
Expand Down
2 changes: 1 addition & 1 deletion test/v1/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('transaction', function() {
var driver, session;

beforeEach(function(done) {
driver = neo4j.driver("bolt://localhost");
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
session = driver.session();

session.run("MATCH (n) DETACH DELETE n").then(done);
Expand Down
8 changes: 4 additions & 4 deletions test/v1/types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('map values', function() {
describe('node values', function() {
it('should support returning nodes ', function(done) {
// Given
var driver = neo4j.driver("bolt://localhost");
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();

// When
Expand All @@ -84,7 +84,7 @@ describe('node values', function() {
describe('relationship values', function() {
it('should support returning relationships', function(done) {
// Given
var driver = neo4j.driver("bolt://localhost");
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();

// When
Expand All @@ -104,7 +104,7 @@ describe('relationship values', function() {
describe('path values', function() {
it('should support returning paths', function(done) {
// Given
var driver = neo4j.driver("bolt://localhost");
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();

// When
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('path values', function() {

function testVal( val ) {
return function( done ) {
var driver = neo4j.driver("bolt://localhost");
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();

session.run("RETURN {val} as v", {val: val})
Expand Down

0 comments on commit 3447cbb

Please sign in to comment.