Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Jul 26, 2016
0 parents commit ed3b088
Show file tree
Hide file tree
Showing 18 changed files with 593 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
"es2015"
],
"plugins": [
"syntax-async-functions",
"transform-regenerator",
"transform-object-rest-spread"
]
}
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{

"parser": "babel-eslint",

"env": {
"es6": true,
"mocha": true,
"node": true,
"browser": true
},

"rules": {
"strict": 0
},

"parserOptions": {
"ecmaVersion": 7,
"experimentalObjectRestSpread": true,
"sourceType": "module"
},

"extends": [ "eslint:recommended" ]

}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib
node_modules
coverage
test-local.sh
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
7 changes: 7 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2016 Gadi Cohen <dragon@wastelands.net>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# apollo-passport-local

Local strategy using email address and hashed, bcrypted password

Copyright (c) 2016 by Gadi Cohen, released under the MIT license.

## Usage

See https://github.com/gadicc/apollo-passport.
16 changes: 16 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
machine:

node:
version: 4.2.6

post:
- source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
- wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
- sudo apt-get update
- sudo apt-get install rethinkdb
- rethinkdb:
background: true

test:
post:
- npm run coveralls
66 changes: 66 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "apollo-passport-local",
"version": "0.0.1",
"description": "Local strategy using email address and hashed, bcrypted password",
"main": "lib/index.js",
"scripts": {
"compile": "babel -d lib/ src/",
"prepublish": "npm run compile",
"test": "mocha --compilers js:babel-core/register \"src/**/*.spec.js\"",
"coverage": "istanbul cover _mocha -- --compilers js:babel-core/register \"src/**/*.spec.js\" -R spec",
"coveralls": "istanbul cover _mocha --report lcovonly -- --compilers js:babel-core/register \"src/**/*.spec.js\" -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"repository": {
"type": "git",
"url": "https://github.com/apollo-passport/apollo-passport-local"
},
"bugs": {
"url": "https://github.com/apollo-passport/apollo-passport-local/issues"
},
"keywords": [
"apollo",
"passport",
"auth",
"authentication"
],
"author": "Gadi Cohen",
"license": "MIT",
"devDependencies": {
"apollo-passport": "^0.0.2",
"babel-cli": "^6.7.5",
"babel-core": "^6.10.4",
"babel-eslint": "^6.0.4",
"babel-plugin-syntax-async-functions": "^6.8.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-regenerator": "^6.9.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.11.1",
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"coveralls": "^2.11.9",
"eslint": "^2.10.2",
"eslint-plugin-jsx-control-statements": "^2.1.1",
"eslint-plugin-react": "^5.1.1",
"graphql-tag": "^0.1.9",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^2.5.3",
"mocha-lcov-reporter": "^1.2.0",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"proxyquire": "^1.7.10",
"regenerator-runtime": "^0.9.5",
"rethinkdbdash": "^2.3.16",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
"winston": "^2.2.0"
},
"peerDependencies": {
"apollo-passport": "^0.0.2",
"graphql-tag": "^0.1.7",
"regenerator-runtime": "^0.9.5"
},
"dependencies": {
"bcrypt": "^0.8.7",
"sha.js": "^2.4.5"
}
}
50 changes: 50 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import gql from 'graphql-tag';
import createHash from 'sha.js';

const mutation = gql`
mutation login (
$email: String!
$password: String!
) {
passportLoginEmail (
email: $email
password: $password
) {
error
token
}
}
`;

const extensionMethods = {

async loginWithEmail(email, password) {
this.loginStart();

const result = await this.apolloClient.mutate({
mutation,
variables: {
email,
password: createHash('sha256').update(password).digest('hex')
}
});

this.loginComplete(result, 'passportLoginEmail');
},

signupWithEmail(email, password) {

}

};

class LocalStrategy {

constructor(apolloPassport) {
this.ap = apolloPassport;
apolloPassport.extendWith(extensionMethods);
}

}

export default LocalStrategy;
82 changes: 82 additions & 0 deletions src/client/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import ApolloPassport from 'apollo-passport/lib/client';
import LocalStrategy from './index';
import chai from 'chai';
import 'regenerator-runtime/runtime';

// dupes from apollo-passport; TODO factor out
global.window = global;
const localStorage = window.localStorage = {
items: {},
getItem: (name) => localStorage.items[name],
setItem: (name, value) => localStorage.items[name] = value
// removeItem: (name) => delete localStorage.items[name]
};
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ';
const decodedToken = {
"sub": "1234567890",
"name": "John Doe",
"admin": true
};

const should = chai.should();

describe('ApolloPassport - strategies - local - client', () => {

describe('loginWithEmail()', () => {

it('handles errors', (done) => {
const apolloClient = {
mutate() {
return new Promise(resolve => {
resolve({
errors: [
{
// think this is right, double check TODO
location: '',
message: 'an error'
}
]
});
});
}
};

const ap = new ApolloPassport({ apolloClient });
ap.use('local', LocalStrategy);

ap.loginWithEmail('a', 'b').then(() => {
// Nothing to test for for now.
done();
});
});

it('sets state', (done) => {
const apolloClient = {
mutate() {
return new Promise(resolve => {
resolve({
data: {
passportLoginEmail: { token }
}
});
});
}
};

const ap = new ApolloPassport({ apolloClient });
ap.use('local', LocalStrategy);

ap.loginWithEmail('a', 'b').then(() => {
ap.getState().should.deep.equal({
data: decodedToken,
verified: true,
error: null
});
done();
});

});

});

});
20 changes: 20 additions & 0 deletions src/db/rethinkdbdash/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default function verify(email, password, done) {
this.db.users
.filter(this.db.r.row('emails').contains({ address: email }))
.limit(1)
.run()
.then(users => {
if (!users.length)
return done(null, false, "Invalid email");

const user = users[0];
this.comparePassword(password, user.password, (err, match) => {
if (err)
done(err);
else if (match)
done(null, user);
else
done(null, false, "Invalid password");
});
}).catch(err => done(err));
}
Loading

0 comments on commit ed3b088

Please sign in to comment.