Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
first cut of jwt strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ekryski committed Nov 10, 2016
1 parent 8a4e0a8 commit 6848335
Show file tree
Hide file tree
Showing 18 changed files with 4,486 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"plugins": [ "add-module-exports" ],
"presets": [ "es2015" ]
}
13 changes: 13 additions & 0 deletions .editorconfig
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
33 changes: 33 additions & 0 deletions .gitignore
@@ -0,0 +1,33 @@
.DS_Store

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript

# The compiled/babelified modules
lib/
17 changes: 17 additions & 0 deletions .istanbul.yml
@@ -0,0 +1,17 @@
verbose: false
instrumentation:
root: ./src/
excludes:
- lib/
include-all-sources: true
reporting:
print: summary
reports:
- html
- text
- lcov
watermarks:
statements: [50, 80]
lines: [50, 80]
functions: [50, 80]
branches: [50, 80]
9 changes: 9 additions & 0 deletions .npmignore
@@ -0,0 +1,9 @@
.editorconfig
.jshintrc
.travis.yml
.istanbul.yml
.babelrc
.idea/
src/
test/
!lib/
15 changes: 15 additions & 0 deletions .travis.yml
@@ -0,0 +1,15 @@
language: node_js
node_js:
- 'node'
- '6'
- '4'
addons:
code_climate:
repo_token: 'your repo token'
notifications:
slack: feathersjs:T4tvpJFt27wJZABjLSg5OqMv
email: false
before_script:
- npm install -g codeclimate-test-reporter
after_script:
- codeclimate-test-reporter < coverage/lcov.info
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 Feathers

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.

53 changes: 52 additions & 1 deletion README.md
@@ -1,2 +1,53 @@
# feathers-authentication-jwt
JWT authentication strategy for feathers-authentication using Passport

[![Build Status](https://travis-ci.org/feathersjs/feathers-authentication-jwt.png?branch=master)](https://travis-ci.org/feathersjs/feathers-authentication-jwt)
[![Code Climate](https://codeclimate.com/github/feathersjs/feathers-authentication-jwt/badges/gpa.svg)](https://codeclimate.com/github/feathersjs/feathers-authentication-jwt)
[![Test Coverage](https://codeclimate.com/github/feathersjs/feathers-authentication-jwt/badges/coverage.svg)](https://codeclimate.com/github/feathersjs/feathers-authentication-jwt/coverage)
[![Dependency Status](https://img.shields.io/david/feathersjs/feathers-authentication-jwt.svg?style=flat-square)](https://david-dm.org/feathersjs/feathers-authentication-jwt)
[![Download Status](https://img.shields.io/npm/dm/feathers-authentication-jwt.svg?style=flat-square)](https://www.npmjs.com/package/feathers-authentication-jwt)

> JWT authentication strategy for feathers-authentication using Passport
## Installation

```
npm install feathers-authentication-jwt --save
```

## Documentation

Please refer to the [feathers-authentication-jwt documentation](http://docs.feathersjs.com/) for more details.

## Complete Example

Here's an example of a Feathers server that uses `feathers-authentication-jwt`.

```js
const feathers = require('feathers');
const rest = require('feathers-rest');
const hooks = require('feathers-hooks');
const bodyParser = require('body-parser');
const errorHandler = require('feathers-errors/handler');
const plugin = require('feathers-authentication-jwt');

// Initialize the application
const app = feathers()
.configure(rest())
.configure(hooks())
// Needed for parsing bodies (login)
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
// Initialize your feathers plugin
.use('/plugin', plugin())
.use(errorHandler());

app.listen(3030);

console.log('Feathers app started on 127.0.0.1:3030');
```

## License

Copyright (c) 2016

Licensed under the [MIT license](LICENSE).
16 changes: 16 additions & 0 deletions example/README.md
@@ -0,0 +1,16 @@
# feathers-authentication-jwt Example

This provides a complete working example on how to use `feathers-authentication-jwt` to provide JWT authentication and get a JWT access token upon successful authentication or to enforce authentication of a service or endpoint.

1. Start the app by running `npm start`
2. Make a request using the JWT access token outputted in the console.

**Getting a new access token**
```bash
curl -H "Authorization: <your access token>" -X POST http://localhost:3030/authentication
```

**Hitting a protected resource**
```bash
curl -H "Authorization: <your access token>" http://localhost:3030/users
```
71 changes: 71 additions & 0 deletions example/app.js
@@ -0,0 +1,71 @@
var feathers = require('feathers');
var rest = require('feathers-rest');
var socketio = require('feathers-socketio');
var hooks = require('feathers-hooks');
var memory = require('feathers-memory');
var bodyParser = require('body-parser');
var errorHandler = require('feathers-errors/handler');
var auth = require('feathers-authentication');
var jwt = require('../lib/index');

// Initialize the application
var app = feathers();

app.configure(rest())
.configure(socketio())
.configure(hooks())
// Needed for parsing bodies (login)
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
// Configure feathers-authentication
.configure(auth({ secret: 'super secret' }))
// Initialize a user service. This must come before
// the JWT plugin because it depends on that service.
.use('/users', memory())
.configure(jwt())
.use(errorHandler());

const issueJWT = () => {
return hook => {
const app = hook.app;
const id = hook.result.id;
return app.passport.createJWT({ id }, app.get('auth')).then(accessToken => {
hook.result.accessToken = accessToken;
return Promise.resolve(hook);
});
};
};

// Authenticate the user using the default
// email/password strategy and if successful
// return a JWT.
app.service('authentication').hooks({
before: {
create: [auth.hooks.authenticate('jwt')]
}
});

// Add a hook to the user service that automatically replaces
// the password with a hash of the password before saving it.
app.service('users').hooks({
before: {
find: [auth.hooks.authenticate('jwt')]
},
after: {
create: [issueJWT()]
}
});

// Create a user that we can use to log in
var User = {
email: 'admin@feathersjs.com',
permissions: ['*']
};

app.service('users').create(User).then(user => {
console.log('Created default user', user);
}).catch(console.error);

app.listen(3030);

console.log('Feathers authentication with local auth started on 127.0.0.1:3030');
2 changes: 2 additions & 0 deletions mocha.opts
@@ -0,0 +1,2 @@
--recursive test/
--compilers js:babel-core/register
80 changes: 80 additions & 0 deletions package.json
@@ -0,0 +1,80 @@
{
"name": "feathers-authentication-jwt",
"description": "JWT authentication strategy for feathers-authentication using Passport",
"version": "0.0.0",
"homepage": "https://github.com/feathersjs/feathers-authentication-jwt",
"main": "lib/",
"keywords": [
"feathers",
"feathers-plugin"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/feathersjs/feathers-authentication-jwt.git"
},
"author": {
"name": "Feathers contributors",
"email": "hello@feathersjs.com",
"url": "https://feathersjs.com"
},
"contributors": [],
"bugs": {
"url": "https://github.com/feathersjs/feathers-authentication-jwt/issues"
},
"engines": {
"node": ">= 4.6.0"
},
"scripts": {
"prepublish": "npm run compile",
"publish": "git push origin --tags && npm run changelog && git push origin",
"release:patch": "npm version patch && npm publish",
"release:minor": "npm version minor && npm publish",
"release:major": "npm version major && npm publish",
"changelog": "github_changelog_generator && git add CHANGELOG.md && git commit -am \"Updating changelog\"",
"compile": "rimraf lib/ && babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"lint": "semistandard src/**/*.js test/**/*.js --fix",
"mocha": "mocha --opts mocha.opts",
"coverage": "istanbul cover _mocha -- --opts mocha.opts",
"test": "npm run compile && npm run lint && npm run coverage",
"start": "npm run compile && node example/app"
},
"semistandard": {
"sourceType": "module",
"env": [
"mocha"
]
},
"directories": {
"lib": "lib"
},
"dependencies": {
"debug": "^2.3.1",
"feathers-errors": "^2.5.0",
"lodash.merge": "^4.6.0",
"lodash.omit": "^4.5.0",
"passport-jwt": "^2.2.1"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.18.0",
"body-parser": "^1.15.2",
"chai": "^3.5.0",
"feathers": "^2.0.2",
"feathers-authentication": "^0.7.11",
"feathers-hooks": "^1.6.1",
"feathers-memory": "^0.8.1",
"feathers-rest": "^1.5.2",
"feathers-socketio": "^1.4.2",
"istanbul": "^1.1.0-alpha.1",
"jsonwebtoken": "^7.1.9",
"mocha": "^3.1.2",
"rimraf": "^2.5.4",
"semistandard": "^9.1.0",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0"
}
}

0 comments on commit 6848335

Please sign in to comment.