|
1 | | -#node-oauth2-server-component |
| 1 | +# Node OAuth2 Server Implementation |
2 | 2 |
|
3 | | -postman: https://www.getpostman.com/collections/37afd82600127fbeef28 |
| 3 | +References: |
| 4 | +https://github.com/dsquier/oauth2-server-php-mysql |
4 | 5 |
|
5 | | -see sql file in sql folder |
| 6 | +# [](https://travis-ci.org/thomseddon/node-oauth2-server) |
6 | 7 |
|
7 | | -References: |
8 | | -https://github.com/dsquier/oauth2-server-php-mysql |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +``` |
| 12 | +git clone https://github.com/manjeshpv/node-oauth2-server-implementation |
| 13 | +npm install |
| 14 | +npm start or node ./bin/www |
| 15 | +``` |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +The module provides two middlewares, one for authorization and routing, another for error handling, use them as you would any other middleware: |
| 20 | + |
| 21 | +```js |
| 22 | +var express = require('express'); |
| 23 | +var oauthServer = require('oauth2-server'); |
| 24 | +var Request = oauthServer.Request; |
| 25 | +var Response = oauthServer.Response; |
| 26 | + |
| 27 | +var app = express(); |
| 28 | + |
| 29 | +app.use(bodyParser.urlencoded({ extended: true })); |
| 30 | + |
| 31 | +app.use(bodyParser.json()); |
| 32 | + |
| 33 | +// https://github.com/manjeshpv/node-oauth2-server-implementation/blob/master/components/oauth/models.js |
| 34 | +var oauth = new oauthServer({ |
| 35 | + model: require('./models.js') |
| 36 | +}); |
| 37 | + |
| 38 | +app.all('/oauth/token', function(req,res,next){ |
| 39 | + var request = new Request(req); |
| 40 | + var response = new Response(res); |
| 41 | + |
| 42 | + oauth |
| 43 | + .token(request,response) |
| 44 | + .then(function(token) { |
| 45 | + // Todo: remove unnecessary values in response |
| 46 | + return res.json(token) |
| 47 | + }).catch(function(err){ |
| 48 | + return res.status( 500).json(err) |
| 49 | + }) |
| 50 | + }); |
| 51 | + |
| 52 | + app.post('/authorise', function(req, res){ |
| 53 | + var request = new Request(req); |
| 54 | + var response = new Response(res); |
| 55 | + |
| 56 | + return oauth.authorize(request, response).then(function(success) { |
| 57 | + res.json(success) |
| 58 | + }).catch(function(err){ |
| 59 | + res.status(err.code || 500).json(err) |
| 60 | + }) |
| 61 | + }); |
| 62 | + |
| 63 | +app.get('/secure', authenticate(), function(req,res){ |
| 64 | + res.json({message: 'Secure data'}) |
| 65 | +}); |
| 66 | + |
| 67 | +app.get('/me', authenticate(), function(req,res){ |
| 68 | + res.json({ |
| 69 | + me: req.user, |
| 70 | + messsage: 'Authorization success, Without Scopes, Try accessing /profile with `profile` scope', |
| 71 | + description: 'Try postman https://www.getpostman.com/collections/37afd82600127fbeef28', |
| 72 | + more: 'pass `profile` scope while Authorize' |
| 73 | + }) |
| 74 | +}); |
| 75 | + |
| 76 | +app.get('/profile', authenticate({scope:'profile'}), function(req,res){ |
| 77 | + res.json({ |
| 78 | + profile: req.user |
| 79 | + }) |
| 80 | +}); |
| 81 | + |
| 82 | +app.listen(3000); |
| 83 | +``` |
| 84 | + |
| 85 | +After running with node, visting http://127.0.0.1:3000 should present you with a json response saying your access token could not be found. |
| 86 | + |
| 87 | +To simulate, Use Postman: https://www.getpostman.com/collections/37afd82600127fbeef28 |
| 88 | + |
| 89 | +## Features |
| 90 | + |
| 91 | +- Supports authorization_code, password, refresh_token, client_credentials and extension (custom) grant types |
| 92 | +- Implicitly supports any form of storage e.g. PostgreSQL, MySQL, Mongo, Redis... |
| 93 | +- Full test suite |
| 94 | + |
| 95 | +## Model Specification |
| 96 | + |
| 97 | +See SQL file in `/sql` folder |
| 98 | + |
| 99 | +The module requires a model object through which some aspects or storage, retrieval and custom validation are abstracted. |
| 100 | +The last parameter of all methods is a callback of which the first parameter is always used to indicate an error. |
| 101 | + |
| 102 | +Note: see https://github.com/manjeshpv/node-oauth2-server-implementation/blob/master/components/oauth/models.js for a full model example using MySQL. |
0 commit comments