@travetto/auth-web-passport
v8.0.4
Published
Web authentication integration support for the Travetto framework
Maintainers
Readme
Web Auth Passport
Web authentication integration support for the Travetto framework
Install: @travetto/auth-web-passport
npm install @travetto/auth-web-passport
# or
yarn add @travetto/auth-web-passportThis is a primary integration for the Web Auth module.
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.
NOTE: Given that passport is oriented around express, this module relies on Web Connect Support as an adapter for the request/response handoff. There are some limitations listed in the module, and those would translate to any passport strategies that are being used.
Code: Sample Facebook/passport config
import { Strategy as FacebookStrategy } from 'passport-facebook';
import type { Authenticator, Authorizer, Principal } from '@travetto/auth';
import { PassportAuthenticator } from '@travetto/auth-web-passport';
import { InjectableFactory } from '@travetto/di';
export class FbUser {
username: string;
permissions: string[];
}
export const FbAuthSymbol = Symbol.for('auth_facebook');
export class AppConfig {
@InjectableFactory(FbAuthSymbol)
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, callback) => callback(undefined, profile)
),
(user: FbUser) => ({
id: user.username,
permissions: user.permissions,
details: user
})
);
}
@InjectableFactory()
static principalSource(): Authorizer {
return new (class implements Authorizer {
async authorize(principal: Principal) {
return principal;
}
})();
}
}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 endpoints using Facebook/passport provider
import type { Principal } from '@travetto/auth';
import { Authenticated, Login, Logout } from '@travetto/auth-web';
import { ContextParam, Controller, Get, Post, type WebRequest, WebResponse } from '@travetto/web';
import { FbAuthSymbol } from './config.ts';
@Controller('/auth')
export class SampleAuth {
@ContextParam()
request: WebRequest;
@ContextParam()
user: Principal;
@Get('/name')
async getName() {
return { name: 'bob' };
}
@Get('/facebook')
@Login(FbAuthSymbol)
async fbLogin() {}
@Get('/self')
@Authenticated()
async getSelf() {
return this.user;
}
@Get('/facebook/callback')
@Login(FbAuthSymbol)
async fbLoginComplete() {
return WebResponse.redirect('/auth/self');
}
@Post('/logout')
@Logout()
async logout() {}
/**
* Simple Echo
*/
@Post('/')
async echo(): Promise<unknown> {
return this.request.body;
}
}