feathers-authentication-oauth1
A Feathers OAuth1 authentication strategy
Installation
npm install feathers-authentication-oauth1 --save
Note: This is only compatibile with feathers-authentication@1.x
and above.
Documentation
Please refer to the feathers-authentication-oauth1 documentation 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. Most notably Twitter.
API
This module contains 2 core pieces:
- The main entry function
- The
Verifier
class
Main Initialization
In most cases initializing the feathers-authentication-oauth1
module is as simple as doing this:
const session = ;const TwitterStrategy = Strategy;app;app;app;
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
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 callbackPath: '/auth/<provider>/callback' // The route to register the callback handler 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,
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.
// the class constructor _updateEntity(entity) // updates an existing entity _createEntity(entity) // creates an entity if they didn't exist already _normalizeResult(result) // normalizes result from service to account for pagination verify(req, accessToken, refreshToken, profile, done) // queries the service and calls the other internal functions.}
Customizing the Verifier
The Verifier
class can be extended so that you customize it's behavior without having to rewrite and test a totally custom local Passport implementation. Although that is always an option if you don't want use this plugin.
An example of customizing the Verifier:
; // The verify function has the exact same inputs and // return values as a vanilla passport strategy { // do your custom stuff. You can call internal Verifier methods // and reference this.app and this.options. This method must be implemented. // the 'user' variable can be any truthy value ; } app;
Customizing The OAuth Response
Whenever you authenticate with an OAuth1 provider such as Twitter, the provider sends back an accessToken
, refreshToken
, and a profile
that contains the authenticated entity's information based on the OAuth1 scopes
you have requested and been granted.
By default the Verifier
takes everything returned by the provider and attaches it to the entity
(ie. the user object) under the provider name. You will likely want to customize the data that is returned. This can be done by adding a before
hook to both the update
and create
service methods on your entity
's service.
app; { return { console; // If there is a twitter field they signed up or // signed in with twitter so let's pull the email. If if hookdatatwitter hookdataemail = hookdatatwitteremail; // If you want to do something whenever any OAuth // provider authentication occurs you can do this. if hookparamsoauth // do something for all OAuth providers if hookparamsoauthprovider === 'twitter' // do something specific to the twitter provider return Promise; };} app;
Complete Example
Here's a basic example of a Feathers server that uses feathers-authentication-oauth1
. You can see a fully working example in the example/ directory.
Note: You must setup some session middleware. OAuth1 strategies rely on sessions in order to authenticate.
const feathers = ;const rest = ;const hooks = ;const memory = ;const bodyParser = ;const session = ;const TwitterStrategy = Strategy;const errorHandler = ;const auth = ;const oauth1 = ; // Initialize the applicationconst app = // Needed for parsing bodies (login) // set up session support. This is required for OAuth1 strategies // Configure feathers-authentication ; app; console;
License
Copyright (c) 2016
Licensed under the MIT license.