@feathersjs/authentication-oauth2
An OAuth2 authentication strategy for feathers-authentication using Passport
Installation
npm install @feathersjs/authentication-oauth2 --save
Note: This is only compatibile with feathers-authentication@1.x
and above.
Documentation
Supported Strategies
and many many more. Any Passport OAuth2 strategy will work.
API
This module contains 2 core pieces:
- The main entry function
- The
Verifier
class
Main Initialization
In most cases initializing the @feathersjs/authentication-oauth2
module is as simple as doing this:
const FacebookStrategy = require('passport-facebook').Strategy;
app.configure(authentication(settings));
app.configure(oauth2({
name: 'facebook',
Strategy: FacebookStrategy,
clientID: '<your client id>',
clientSecret: '<your client secret>',
scope: ['public_profile', 'email']
}));
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
{
idField: '<provider>Id',
path: '/auth/<provider>',
callbackPath: '/auth/<provider>/callback',
callbackURL: 'http(s)://hostname[:port]/auth/<provider>/callback',
successRedirect: undefined,
failureRedirect: undefined,
entity: 'user',
service: 'users',
passReqToCallback: true,
session: false
handler: function,
formatter: function,
Verifier: Verifier
}
Additional passport strategy options can be provided based on the OAuth2 strategy you are configuring.
Verifier
This is the verification class that handles the OAuth2 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-oauth2.
{
constructor(app, options)
_updateEntity(entity)
_createEntity(entity)
_normalizeResult(result)
verify(req, accessToken, refreshToken, profile, done)
}
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:
import oauth2, { Verifier } from '@feathersjs/authentication-oauth2';
class CustomVerifier extends Verifier {
verify(req, accessToken, refreshToken, profile, done) {
done(null, user);
}
}
app.configure(oauth2({
name: 'facebook',
Strategy: FacebookStrategy,
clientID: '<your client id>',
clientSecret: '<your client secret>',
scope: ['public_profile', 'email'],
Verifier: CustomVerifier
}));
Customizing The OAuth Response
Whenever you authenticate with an OAuth2 provider such as Facebook, the provider sends back an accessToken
, refreshToken
, and a profile
that contains the authenticated entity's information based on the OAuth2 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.configure(oauth2({
name: 'github',
entity: 'user',
service: 'users',
Strategy,
clientID: 'your client id',
clientSecret: 'your client secret'
}));
function customizeGithubProfile() {
return function(hook) {
console.log('Customizing Github Profile');
if (hook.data.github) {
hook.data.email = hook.data.github.profile.emails.find(email => email.primary).value;
}
if (hook.params.oauth) {
}
if (hook.params.oauth.provider === 'github') {
}
return Promise.resolve(hook);
};
}
app.service('users').hooks({
before: {
create: [customizeGithubProfile()],
update: [customizeGithubProfile()]
}
});
Complete Example
Here's a basic example of a Feathers server that uses @feathersjs/authentication-oauth2
. You can see a fully working example in the example/ directory.
const feathers = require('feathers');
const rest = require('feathers-rest');
const hooks = require('feathers-hooks');
const memory = require('feathers-memory');
const bodyParser = require('body-parser');
const GithubStrategy = require('passport-github').Strategy;
const errorHandler = require('feathers-errors/handler');
const auth = require('feathers-authentication');
const oauth2 = require('@feathersjs/authentication-oauth2');
const app = feathers()
.configure(rest())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'super secret' }))
.configure(oauth2({
name: 'github',
Strategy: GithubStrategy,
clientID: '<your client id>',
clientSecret: '<your client secret>',
scope: ['user']
}))
.use('/users', memory())
.use(errorHandler());
app.listen(3030);
console.log('Feathers app started on 127.0.0.1:3030');
License
Copyright (c) 2016
Licensed under the MIT license.