项目作者: willviles

项目描述 :
OAuth2 authorization server & middleware for Lux API framework.
高级语言: JavaScript
项目地址: git://github.com/willviles/lux-oauth2.git
创建时间: 2017-04-30T20:22:50Z
项目社区:https://github.com/willviles/lux-oauth2

开源协议:MIT License

下载


Download count all time npm Gitter

Lux OAuth2 is an OAuth2 authorization server & middleware for Lux API framework.

Install

  1. $ npm install --save lux-oauth2

Usage

Lux OAuth2 has been built with extension in mind. More grant types will soon be available out-of-the-box, along with details of how to define your own custom grant types.

Currently, Lux OAuth2 only supports a password with refresh_token grant type flow.


1. Database

Ready your database with the required models listed below. Check out the example app for more guidance.

2. OAuth2 Server

Initialize a new OAuth2 server instance. Ensure to add all the required models and any grant types you wish to use.

  1. // app/middleware/oauth2.js
  2. import { OAuth2BaseServer, OAuth2PasswordGrantType } from 'lux-oauth2';
  3. import OAuthAccessToken from 'app/models/oauth-access-token';
  4. import OAuthClient from 'app/models/oauth-client';
  5. import OAuthRefreshToken from 'app/models/oauth-refresh-token';
  6. import User from 'app/models/user';
  7. class OAuth2Server extends OAuth2BaseServer {
  8. static models = {
  9. accessToken: OAuthAccessToken,
  10. client: OAuthClient,
  11. refreshToken: OAuthRefreshToken,
  12. user: User
  13. };
  14. static grantTypes = [
  15. OAuth2PasswordGrantType
  16. ];
  17. }
  18. export default new OAuth2Server();

3. Token route

The token endpoint will require a POST action. OAuth2 recommends using the /oauth/token route.

  1. // app/routes.js
  2. this.resource('oauth', {
  3. only: []
  4. }, function(){
  5. this.post('/token', 'token');
  6. });

The payload sent to the server must be wrapped in a data attribute. The following controller setup allows the parameters through to the controller, where the requestToken function is then called.

  1. // app/controllers/oauth.js
  2. import { Controller } from 'lux-framework';
  3. import OAuth2Server from 'app/middleware/oauth2';
  4. class OauthController extends Controller {
  5. params = [
  6. 'grantType',
  7. 'username',
  8. 'password'
  9. ]
  10. query = [
  11. 'data'
  12. ]
  13. token(request, response) {
  14. return OAuth2Server.requestToken(request, response);
  15. }
  16. }
  17. export default OauthController;

4. Authenticate

Add the authenticate action to the application controller’s beforeAction array to ensure the OAuth2 server attempts to authenticate a user for each request.

  1. import { Controller } from 'lux-framework';
  2. import OAuth2Server from 'app/middleware/oauth2';
  3. class ApplicationController extends Controller {
  4. beforeAction = [
  5. OAuth2Server.authenticate
  6. ];
  7. }
  8. export default ApplicationController;

This adds an oauth2 object to the request, containing an isAuthenticated boolean value and the currentUser.

  1. console.log(request.oauth2);
  2. // => { isAuthenticated: true, currentUser: User }

5. Authenticated route

Add the authenticatedRoute action to any resource you wish to protect.

  1. // app/controllers/user.js
  2. import { Controller } from 'lux-framework';
  3. import OAuth2Server from 'app/middleware/oauth2';
  4. class UsersController extends Controller {
  5. beforeAction = [
  6. OAuth2Server.authenticatedRoute
  7. ];
  8. }
  9. export default UsersController;

Keep certain endpoints from requiring authentication using lux-unless.

  1. beforeAction = [
  2. unless({ path: ['/users/stats'] }, OAuth2Server.authenticatedRoute)
  3. ];

Options

Server Options

The following additional options can be set on the OAuth2 server.

  1. class OAuth2Server extends OAuth2BaseServer {
  2. accessTokenLifetime = 3600;
  3. refreshTokenLifetime = 1209600;
  4. }

Overriding methods

If you need to override one of the OAuth2Server’s core methods, simply redefine the method in the OAuth2Server.

  1. class OAuth2Server extends OAuth2BaseServer {
  2. getUser = async (email, password, done) => {
  3. // add your custom method of retrieving the user...
  4. }
  5. }

Custom Grant types

Coming soon™…

Example

  1. $ cd /examples/lux-oauth2-example
  2. $ npm install
  3. $ lux db:create && lux db:migrate && lux db:seed
  4. $ lux serve

Use the Lux OAuth2 Example Postman Collection to check the following:

  • Request a token as the test user.
  • Try modifying test user email & password sent to the token endpoint to check credentials errors.
  • Use the refresh_token value to auth via refresh token.
  • Try to access /users to find it requires authentication.
  • Add Bearer <YOUR_ACCESS_TOKEN> to the Authorization header to access the /users data.

Tests

  1. $ npm install
  2. $ npm test

License

This project is licensed under the MIT license. See the LICENSE file for more info.