@travetto/auth-rest-passport
v5.0.17
Published
Rest authentication integration support for the Travetto framework
Downloads
103
Maintainers
Readme
Rest Auth Passport
Rest authentication integration support for the Travetto framework
Install: @travetto/auth-rest-passport
npm install @travetto/auth-rest-passport
# or
yarn add @travetto/auth-rest-passport
This is a primary integration for the Rest Auth module. This is another level of scaffolding allowing for compatible authentication frameworks to integrate.
Within the node ecosystem, the most prevalent auth framework is passport. With countless integrations, the desire to leverage as much of it as possible, is extremely high. To that end, this module provides support for passport baked in. Registering and configuring a passport strategy is fairly straightforward.
Code: Sample Facebook/passport config
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { InjectableFactory } from '@travetto/di';
import { Authenticator, Authorizer, Principal } from '@travetto/auth';
import { PassportAuthenticator } from '@travetto/auth-rest-passport';
export class FbUser {
username: string;
permissions: string[];
}
export const FB_AUTH = Symbol.for('auth_facebook');
export class AppConfig {
@InjectableFactory(FB_AUTH)
static facebookPassport(): Authenticator {
return new PassportAuthenticator('facebook',
new FacebookStrategy(
{
clientID: '<appId>',
clientSecret: '<appSecret>',
callbackURL: 'http://localhost:3000/auth/facebook/callback',
profileFields: ['id', 'username', 'displayName', 'photos', 'email'],
},
(accessToken, refreshToken, profile, cb) =>
cb(undefined, profile)
),
(user: FbUser) => ({
id: user.username,
permissions: user.permissions,
details: user
})
);
}
@InjectableFactory()
static principalSource(): Authorizer {
return new class implements Authorizer {
async authorize(p: Principal) {
return p;
}
}();
}
}
As you can see, PassportAuthenticator will take care of the majority of the work, and all that is required is:
- Provide the name of the strategy (should be unique)
- Provide the strategy instance.
- The conversion functions which defines the mapping between external and local identities.
Note: You will need to provide the callback for the strategy to ensure you pass the external principal back into the framework After that, the provider is no different than any other, and can be used accordingly. Additionally, because passport runs first, in it's entirety, you can use the provider as you normally would any passport middleware.
Code: Sample routes using Facebook/passport provider
import { Controller, Get, Redirect, Post, Request } from '@travetto/rest';
import { AuthService, Authenticate, Authenticated, Unauthenticated } from '@travetto/auth-rest';
import { Inject } from '@travetto/di';
import { FB_AUTH } from './conf';
@Controller('/auth')
export class SampleAuth {
@Inject()
auth: AuthService;
@Get('/name')
async getName() {
return { name: 'bob' };
}
@Get('/facebook')
@Authenticate(FB_AUTH)
async fbLogin() {
}
@Get('/self')
@Authenticated()
async getSelf(req: Request) {
return req.auth;
}
@Get('/facebook/callback')
@Authenticate(FB_AUTH)
async fbLoginComplete() {
return new Redirect('/auth/self', 301);
}
@Post('/logout')
@Unauthenticated()
async logout(req: Request) {
await this.auth.logout(req);
}
/**
* Simple Echo
*/
@Post('/')
async echo(req: Request): Promise<object> {
return req.body;
}
}