npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@travetto/auth-rest-passport

v4.0.7

Published

Rest authentication integration support for the Travetto framework

Downloads

166

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