Skip to content

Commit 3447cbb

Browse files
committed
Merge pull request #30 from pontusmelke/1.0-auth
Authentication support for driver
2 parents ecb267c + 800f444 commit 3447cbb

14 files changed

+43
-32
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ We build a special browser version of the driver, which supports connecting to N
2727
This will make a global `neo4j` object available, where you can access the `v1` API at `neo4j.v1`:
2828

2929
```javascript
30-
var driver = neo4j.v1.driver("bolt://localhost");
30+
var driver = neo4j.v1.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
3131
```
3232

3333
## Usage examples
3434

3535
```javascript
3636

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

4040
// Create a session to run Cypher statements in.
4141
// Note: Always make sure to close sessions when you are done using them!

gulpfile.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ gulp.task('default', ["test"]);
4848

4949
gulp.task('browser', function(cb){
5050
runSequence('build-browser-test', 'build-browser', cb);
51-
})
51+
});
5252

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

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

174174
gulp.task('download-neo4j', function() {
@@ -187,6 +187,11 @@ gulp.task('download-neo4j', function() {
187187
}
188188
});
189189

190+
gulp.task('set-password', ['download-neo4j'], function() {
191+
return gulp.src('test/resources/auth')
192+
.pipe(gulp.dest(neo4jHome + "/data/dbms/"));
193+
});
194+
190195
var featureFiles = 'https://s3-eu-west-1.amazonaws.com/remoting.neotechnology.com/driver-compliance/tck.tar.gz';
191196
var featureHome = './build/tck';
192197

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

233238
});
234239

235-
gulp.task('start-neo4j', ['download-neo4j'], function() {
240+
gulp.task('start-neo4j', ['set-password'], function() {
236241
if(isWin) {
237242
runPowershell('Install-Neo4jServer -Neo4jServer ' + neo4jHome + ' -Name neo4j-js;' +
238243
'Start-Neo4jServer -Neo4jServer ' + neo4jHome + ' -ServiceName neo4j-js');

src/v1/driver.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class Driver {
2929
* @constructor
3030
* @param {string} url
3131
* @param {string} userAgent
32+
* @param {Object} token
3233
*/
33-
constructor(url, userAgent) {
34+
constructor(url, userAgent, token) {
3435
this._url = url;
3536
this._userAgent = userAgent || 'neo4j-javascript/0.0';
3637
this._openSessions = {};
3738
this._sessionIdGenerator = 0;
39+
this._token = token || {};
3840
}
3941

4042
/**
@@ -44,8 +46,7 @@ class Driver {
4446
session() {
4547
let sessionId = this._sessionIdGenerator++;
4648
let conn = connect(this._url);
47-
conn.initialize(this._userAgent);
48-
49+
conn.initialize(this._userAgent, this._token);
4950
let _driver = this;
5051
let _session = new Session( conn, () => {
5152
// On close of session, remove it from the list of open sessions

src/v1/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {VERSION} from '../version';
2424
let USER_AGENT = "neo4j-javascript/" + VERSION;
2525

2626
export default {
27-
driver: (url) => new Driver(url, USER_AGENT),
27+
driver: (url, token) => new Driver(url, USER_AGENT, token),
2828
int: int,
29-
isInt: isInt
29+
isInt: isInt,
30+
auth : {
31+
basic : (username, password) => { return { scheme:"basic", principal : username, credentials : password };}
32+
}
3033
}

src/v1/internal/connector.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ class Connection {
273273
}
274274

275275
/** Queue an INIT-message to be sent to the database */
276-
initialize( clientName, observer ) {
276+
initialize( clientName, token, observer ) {
277277
this._queueObserver(observer);
278-
this._packer.packStruct( INIT, [clientName] );
278+
this._packer.packStruct( INIT, [clientName, token] );
279279
this._chunker.messageBoundary();
280280
}
281281

test/internal/connector.test.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ var DummyChannel = require('../../lib/v1/internal/ch-dummy.js');
2020
var connect = require("../../lib/v1/internal/connector.js").connect;
2121

2222
describe('connector', function() {
23-
it('should read/write basic messages', function(done) {
23+
24+
fit('should read/write basic messages', function(done) {
2425
// Given
2526
var conn = connect("bolt://localhost")
2627

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

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

6768
// When
6869
var records = [];
69-
conn.initialize( "mydriver/0.0.0" );
70+
conn.initialize( "mydriver/0.0.0", {scheme: "basic", principal: "neo4j", credentials: "neo4j"} );
7071
conn.run( "RETURN 1", {} );
7172
conn.sync();
72-
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 ' );
73+
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 ' );
7374
done();
7475
});
7576

test/neo4j-driver.test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ describe('neo4j-driver', function() {
2121
it('should expose version 1 of the API as a property', function(done) {
2222
// When
2323
var neo4jDriver = require("../lib");
24-
2524
// Then I can access and use V1 of the API
26-
var driver = neo4jDriver.v1.driver("bolt://localhost");
25+
var driver = neo4jDriver.v1.driver("bolt://localhost", neo4jDriver.v1.auth.basic("neo4j", "neo4j"));
2726
driver.session().run( "RETURN 1" )
2827
.then( function() { driver.close(); })
2928
.then( done );
@@ -34,7 +33,7 @@ describe('neo4j-driver', function() {
3433
var neo4jV1 = require("../lib/v1");
3534

3635
// Then I can access and use V1 of the API
37-
var driver = neo4jV1.driver("bolt://localhost");
36+
var driver = neo4jV1.driver("bolt://localhost", neo4jV1.auth.basic("neo4j", "neo4j"));
3837
driver.session().run( "RETURN 1" )
3938
.then( function() { driver.close(); })
4039
.then( done );

test/resources/auth

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
neo4j:SHA-256,E4B4C20A3933637A1EE5ADBDE16290F6915E516A67216E35CAE1208A86F91E3B,2801E325BE8A074BD92F1A4B692AE167:

test/v1/driver.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
var neo4j = require("../../lib/v1");
2121

2222
describe('driver', function() {
23+
2324
it('should expose sessions', function() {
2425
// Given
25-
var driver = neo4j.driver("bolt://localhost");
26+
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
2627

2728
// When
2829
var session = driver.session();

test/v1/examples.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('transaction', function() {
2626
var driver, session, out, console;
2727

2828
beforeEach(function(done) {
29-
driver = neo4j.driver("bolt://localhost");
29+
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
3030
session = driver.session();
3131

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

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

4848
session.run( "CREATE (neo:Person {name:'Neo', age:23})" );

test/v1/session.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('session', function() {
2525
var driver, session;
2626

2727
beforeEach(function(done) {
28-
driver = neo4j.driver("bolt://localhost");
28+
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
2929
session = driver.session();
3030

3131
session.run("MATCH (n) DETACH DELETE n").then(done);

test/v1/tck/steps/environment.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = function () {
55
var failedScenarios = []
66

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

1616
this.Before("~@reset_database", function( scenario, callback ) {
17-
this.driver = neo4j.driver("bolt://localhost");
17+
this.driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
1818
this.session = this.driver.session();
1919
callback();
2020
});

test/v1/transaction.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('transaction', function() {
2424
var driver, session;
2525

2626
beforeEach(function(done) {
27-
driver = neo4j.driver("bolt://localhost");
27+
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
2828
session = driver.session();
2929

3030
session.run("MATCH (n) DETACH DELETE n").then(done);

test/v1/types.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('map values', function() {
6464
describe('node values', function() {
6565
it('should support returning nodes ', function(done) {
6666
// Given
67-
var driver = neo4j.driver("bolt://localhost");
67+
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
6868
var session = driver.session();
6969

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

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

110110
// When
@@ -133,7 +133,7 @@ describe('path values', function() {
133133

134134
function testVal( val ) {
135135
return function( done ) {
136-
var driver = neo4j.driver("bolt://localhost");
136+
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
137137
var session = driver.session();
138138

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

0 commit comments

Comments
 (0)