🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

@travetto/auth-passport

Package Overview
Dependencies
Maintainers
1
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@travetto/auth-passport

Rest authentication integration support for the travetto framework

latest
Source
npmnpm
Version
3.0.0-rc.0
Version published
Maintainers
1
Created
Source

Rest Auth Passport

Rest authentication integration support for the travetto framework

Install: @travetto/auth-passport

npm install @travetto/auth-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-passport';

export class FbUser {
  username: string;
  roles: 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.roles,
        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. Note: you will need to provide the callback for the strategy to ensure you pass the external principal back into the framework
  • The conversion functions which defines the mapping between external and local identities.

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;
  }
}

Keywords

authentication

FAQs

Package last updated on 05 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts