# jab-oauth2-server

> OAuth2 server for Jab env used with express

Latest version **0.2.1** (published 2018-01-31) · 0 weekly downloads

## Install

```sh
npm install jab-oauth2-server
pnpm add jab-oauth2-server
yarn add jab-oauth2-server
bun add jab-oauth2-server
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2018-01-31 |
| First published | 2013-04-19 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10.x |
| Dependencies | 8 |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| Author | A. Mélon |
| Maintainers | amelon |
| Keywords | oauth2, jab, express, web, server, rest, restfull |

## Links

- npm: https://www.npmjs.com/package/jab-oauth2-server
- Repository: https://github.com/amelon/jab-oauth2-server
- Homepage: https://github.com/amelon/jab-oauth2-server#readme
- Issues: https://github.com/amelon/jab-oauth2-server/issues
- npm.io page: https://npm.io/package/jab-oauth2-server

## Dependencies (8)

- [slide](https://npm.io/package/slide.md) ^1.1.5
- [lodash](https://npm.io/package/lodash.md) ^2.4.1
- [passport](https://npm.io/package/passport.md) ^0.2.1
- [serializer](https://npm.io/package/serializer.md) ~0.0.3
- [oauth2orize](https://npm.io/package/oauth2orize.md) *
- [passport-http](https://npm.io/package/passport-http.md) *
- [passport-local](https://npm.io/package/passport-local.md) *
- [passport-http-bearer](https://npm.io/package/passport-http-bearer.md) ^1.0.1

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@oxyhq/services](https://npm.io/package/@oxyhq/services.md) — 2.3K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2018-01-31
- 0.2.0 — 2016-10-05
- 0.1.0 — 2016-01-26
- 0.0.15 — 2015-03-19
- 0.0.14 — 2014-08-08
- 0.0.13 — 2014-05-23
- 0.0.12 — 2014-03-17
- 0.0.11 — 2013-12-11
- 0.0.10 — 2013-12-05
- 0.0.9 — 2013-12-05
- 0.0.8 — 2013-08-26
- 0.0.7 — 2013-07-05
- 0.0.6 — 2013-07-05
- 0.0.5 — 2013-06-27
- 0.0.3 — 2013-04-19

## README

## Depracted

see: https://www.npmjs.com/package/@b-flower/bdn-oauth2-server


  OAuth2 server installed quickly, used with express

  Using oauth2orize & passport

```js
//server side
var express = require('express');
var app = express();
var oauth_server = require('jab-oauth2-server');
var passport = require('passport');


//mimic mongoose db user collection - DO NOT USE ON PRODUCTION
var users = require('jab-oauth2-server/default_db_users');

//fake user creation
new users('id', 'superuser', 'superpassword').save();

//mimic mongoose db token collection - DO NOT USE ON PRODUCTION
var tokens = require('jab-oauth2-server/default_db_tokens');

oauth_server.init({dbUsers: users, dbTokens: tokens, clientId: 'james', clientSecret: '007'});
//could use dbClients
// var clients = require('jab-oauth2-server/default_db_clients');
// new clients('james', '007').save();
// oauth_server.init({dbUsers: users, dbTokens: tokens, dbClients: clients});

app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
oauth_server.attach(app, {dbUsers: users, dbTokens: tokens, clientId: 'james', clientSecret: '007'});

app.listen(3000);

//unprotected resource
app.get('/', function(req, res) {
  res.send('Hello World');
});

//protected resource
app.get('/need_bearer'
, passport.authenticate('bearer', {session: false})
, function(req, res) {
    res.json({ user_id: req.user.id, name: req.user.name, scope: req.authInfo.scope });
  }
);

app.listen(3000);
```

```js
//client side - using request

  request({
    uri: 'http://localhost:3000/oauth/token'
  , method: 'POST'
  , headers: {
    'authorization': 'Basic '+ new Buffer('james:007', 'binary').toString('base64')
  }
  , json: true
  , form: {
      username: 'superuser'
    , password: 'superpassword'
    , grant_type: 'password'
    }
  }, function(err, resp, body) {
    // => body: {access_token: 'NNNNNNNNN...', token_type: 'bearer'}
  });

```


```js
//client side - with jQuery

  var bearer_token;

  //exchange token
  $.ajax('oauth/token', {
    dataType: 'json'
  , type: 'POST'
  , headers: {
      'authorization': 'Basic '+btoa('james:007')
    }
  , data: {
      username: 'superuser'
    , password: 'superpassword'
    , grant_type: 'password'
    }
  }).done(function(data, textStatus) {
    if (data.token_type == 'bearer' && data.access_token) {
      bearer_token = data.access_token;

      accessProtectedResource('test_bearer');
    }
    console.log('done data', data, 'textStatus', textStatus);

  }).fail(function(jqXhr, textStatus, errorThrown) {
    console.log('fail textStatus', textStatus, 'errorThrown', errorThrown);
  });

  //...
  //access protected resource
  function accessProtectedResource(url, done, fail) {
    $.ajax(url, {
      dataType: 'json'
    , headers: {
        'authorization': 'Bearer ' + bearer_token
      }
    }).done(function(data, textStatus) {
      console.log('done data', data, 'textStatus', textStatus);
      if (done) done(data);
    }).fail(function(jqXhr, textStatus, errorThrown) {
      console.log('fail textStatus', textStatus, 'errorThrown', errorThrown);
      if (fail) fail(data);
    });
  }

```

## Installation

    $ npm install jab-oauth2-server


## Implementation

The following methods MUST be implemented in your db models (see default_db_*.js)

### db_client

  * static   : DBClients.findOneByClientId
  * prototype: client.clientSecretCompare

### db_user

  * static   : findOneByUsername
  * static   : findOneById
  * prototype: comparePassword
  * prototype: toObject


  * prototype: isActive

  _Optional method_ :
  If implemented, refuse connection if `!user.isActive()`

### db_tokens

  * static   : createByParams
  * static   : findOneByToken

## More Information


## Running Tests

To run the test suite first invoke the following command within the repo, installing the development dependencies:

    $ npm install

then run the tests:

    $ grunt test


## License

(The MIT License)

Copyright (c) 2009-2012 A. Mélon &lt;paztaga@gmail.com&gt;

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.

---
_Source: https://npm.io/package/jab-oauth2-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
