<commit_before># feathers-authentication-oauth1

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

> A Feathers OAuth1 authentication strategy

## Installation

```
npm install feathers-authentication-oauth1 --save
```

## Documentation

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

## Supported Strategies

There aren't a ton of OAuth1 strategies anymore as most have moved to OAuth2, however this will work for any [Passport OAuth1 strategy](http://passportjs.org/). Most notably [Twitter](https://github.com/jaredhanson/passport-twitter).

## API

This module contains 2 core pieces:

1. The main entry function
2. The `Verifier` class

### Main Initialization

In most cases initializing the `feathers-authentication-oauth1` module is as simple as doing this:

```js
const TwitterStrategy = require('passport-twitter').Strategy;
app.configure(authentication(settings));
app.configure(oauth1({
  name: 'twitter',
  Strategy: TwitterStrategy,
  consumerKey: '<your consumer key>',
  consumerSecret: '<your consumer secret>'
}));
```

This will pull from your global `auth` object in your config file. It will also mix in the following defaults, which can be customized.

#### Default Options

```js
{
    idField: '<provider>Id', // The field to look up the entity by when logging in with the provider. Defaults to '<provider>Id' (ie. 'twitterId').
    path: '/auth/<provider>', // The route to register the middleware
    callbackURL: 'http(s)://hostame[:port]/auth/<provider>/callback', // The callback url. Will automatically take into account your host and port and whether you are in production based on your app environment to construct the url. (ie. in development http://localhost:3030/auth/twitter/callback)
    entity: 'user', // the entity that you are looking up
    service: 'users', // the service to look up the entity
    passReqToCallback: true, // whether the request object should be passed to `verify`
    session: true // whether to use sessions,
    handler: function, // Express middleware for handling the oauth callback. Defaults to the built in middleware.
    formatter: function, // The response formatter. Defaults the the built in feathers-rest formatter, which returns JSON.
    Verifier: Verifier // A Verifier class. Defaults to the built-in one but can be a custom one. See below for details.
}
```

Additional passport strategy options can be provided based on the OAuth1 strategy you are configuring.

### Verifier

This is the verification class that handles the OAuth1 verification by looking up the entity (normally a `user`) on a given service and either creates or updates the entity and returns them. It has the following methods that can all be overridden. All methods return a promise except `verify`, which has the exact same signature as [passport-oauth1](https://github.com/jaredhanson/passport-oauth1).

```js
{
    constructor(app, options) // the class constructor
    _updateEntity(entity) // updates an existing entity<commit_msg>Adding session usage in short example<commit_after># feathers-authentication-oauth1

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

> A Feathers OAuth1 authentication strategy

## Installation

```
npm install feathers-authentication-oauth1 --save
```

## Documentation

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

## Supported Strategies

There aren't a ton of OAuth1 strategies anymore as most have moved to OAuth2, however this will work for any [Passport OAuth1 strategy](http://passportjs.org/). Most notably [Twitter](https://github.com/jaredhanson/passport-twitter).

## API

This module contains 2 core pieces:

1. The main entry function
2. The `Verifier` class

### Main Initialization

In most cases initializing the `feathers-authentication-oauth1` module is as simple as doing this:

```js
const session = require('express-session');
const TwitterStrategy = require('passport-twitter').Strategy;
app.use(session({ secret: 'super secret', resave: true, saveUninitialized: true }));
app.configure(authentication(settings));
app.configure(oauth1({
  name: 'twitter',
  Strategy: TwitterStrategy,
  consumerKey: '<your consumer key>',
  consumerSecret: '<your consumer secret>'
}));
```

This will set up session middleware and authentication pulling from your global `auth` object in your config file. It will also mix in the following defaults, which can be customized.

#### Default Options

```js
{
    idField: '<provider>Id', // The field to look up the entity by when logging in with the provider. Defaults to '<provider>Id' (ie. 'twitterId').
    path: '/auth/<provider>', // The route to register the middleware
    callbackURL: 'http(s)://hostame[:port]/auth/<provider>/callback', // The callback url. Will automatically take into account your host and port and whether you are in production based on your app environment to construct the url. (ie. in development http://localhost:3030/auth/twitter/callback)
    entity: 'user', // the entity that you are looking up
    service: 'users', // the service to look up the entity
    passReqToCallback: true, // whether the request object should be passed to `verify`
    session: true // whether to use sessions,
    handler: function, // Express middleware for handling the oauth callback. Defaults to the built in middleware.
    formatter: function, // The response formatter. Defaults the the built in feathers-rest formatter, which returns JSON.
    Verifier: Verifier // A Verifier class. Defaults to the built-in one but can be a custom one. See below for details.
}
```

Additional passport strategy options can be provided based on the OAuth1 strategy you are configuring.

### Verifier

This is the verification class that handles the OAuth1 verification by looking up the entity (normally a `user`) on a given service and either creates or updates the entity and returns them. It has the following methods that can all be overridden. All methods return a promise except `verify`, which has the exact same signature as [passport-oauth1](https://github.com/jaredhanson/passport-oauth1).

```js
{
    constructor(app, options) // the class constructor
    _updateEntity(entity) // updates an existing entity
{'fraction_non_alphanumeric': 0.11403401443778294, 'fraction_numerical': 0.007341245564664138, 'mean_word_length': 4.927483683828862, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 24, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7544892', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2340, 'n_words': 759}