4.0.7 • Published 2 months ago

@travetto/auth-rest-passport v4.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

2 months ago

4.0.6

2 months ago

4.0.5

2 months ago

4.0.4

2 months ago

4.0.3

2 months ago

4.0.1

3 months ago

4.0.0

3 months ago

4.0.2

3 months ago

4.0.0-rc.10

3 months ago

4.0.0-rc.9

3 months ago

4.0.0-rc.8

3 months ago

3.4.6

3 months ago

4.0.0-rc.7

3 months ago

4.0.0-rc.6

3 months ago

4.0.0-rc.5

3 months ago

4.0.0-rc.3

3 months ago

4.0.0-rc.4

3 months ago

4.0.0-rc.1

3 months ago

4.0.0-rc.2

3 months ago

4.0.0-rc.0

4 months ago

3.4.4

5 months ago

3.4.5

5 months ago

3.3.6

7 months ago

3.4.0

6 months ago

3.2.2

10 months ago

3.2.1

10 months ago

3.4.3

6 months ago

3.2.5

10 months ago

3.4.2

6 months ago

3.2.4

10 months ago

3.4.1

6 months ago

3.2.3

10 months ago

3.4.0-rc.7

6 months ago

3.4.0-rc.8

6 months ago

3.4.0-rc.5

6 months ago

3.4.0-rc.6

6 months ago

3.4.0-rc.3

6 months ago

3.4.0-rc.4

6 months ago

3.4.0-rc.1

6 months ago

3.4.0-rc.2

6 months ago

3.4.0-rc.0

6 months ago

3.3.1

9 months ago

3.3.0

9 months ago

3.3.5

8 months ago

3.3.4

8 months ago

3.3.3

8 months ago

3.3.2

8 months ago

3.2.0

11 months ago

3.1.17

11 months ago

3.2.0-rc.0

11 months ago

3.1.12

12 months ago

3.1.11

12 months ago

3.1.14

12 months ago

3.1.13

12 months ago

3.1.16

11 months ago

3.1.15

11 months ago

3.1.10

12 months ago

3.1.9

12 months ago

3.1.8

1 year ago

3.1.7

1 year ago

3.1.6

1 year ago

3.1.5

1 year ago

3.1.4

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

3.0.3

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.1.0-rc.2

1 year ago

3.1.0-rc.3

1 year ago

3.1.0-rc.0

1 year ago

3.1.0-rc.1

1 year ago

3.1.0-rc.6

1 year ago

3.1.0-rc.7

1 year ago

3.1.0-rc.4

1 year ago

3.1.0-rc.5

1 year ago

3.1.0-rc.8

1 year ago

3.0.0-rc.26

1 year ago

3.0.0-rc.27

1 year ago

3.0.1-rc.1

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

3.0.2-rc.1

1 year ago

3.0.2-rc.0

1 year ago

3.0.0-rc.24

1 year ago

3.0.0-rc.23

1 year ago

3.0.0-rc.15

1 year ago

3.0.0-rc.25

1 year ago

3.0.0-rc.17

1 year ago

3.0.0-rc.16

1 year ago

3.0.0-rc.19

1 year ago

3.0.0-rc.18

1 year ago

3.0.0-rc.20

1 year ago

3.0.0-rc.22

1 year ago

3.0.0-rc.21

1 year ago

3.0.0-rc.13

1 year ago

3.0.0-rc.12

1 year ago

3.0.0-rc.14

1 year ago

3.0.0-rc.9

1 year ago

3.0.0-rc.8

1 year ago

3.0.0-rc.7

1 year ago

3.0.0-rc.6

1 year ago

3.0.0-rc.11

1 year ago

3.0.0-rc.10

1 year ago

3.0.0-rc.4

2 years ago

3.0.0-rc.3

2 years ago