@feathersjs/authentication

  • Version 4.5.15
  • Published
  • 193 kB
  • 9 dependencies
  • MIT license

Install

npm i @feathersjs/authentication
yarn add @feathersjs/authentication
pnpm add @feathersjs/authentication

Overview

Add Authentication to your FeathersJS app.

Index

Classes

class AuthenticationBase

class AuthenticationBase {}
  • A base class for managing authentication strategies and creating and verifying JWTs

constructor

constructor(app: Application, configKey?: string, options?: {});
  • Create a new authentication service.

    Parameter app

    The Feathers application instance

    Parameter configKey

    The configuration key name in app.get (default: authentication)

    Parameter options

    Optional initial options

property app

app: Application;

    property configKey

    configKey: string;

      property configuration

      readonly configuration: any;
      • Return the current configuration from the application

      property strategies

      strategies: { [key: string]: AuthenticationStrategy };

        property strategyNames

        readonly strategyNames: string[];
        • A list of all registered strategy names

        method authenticate

        authenticate: (
        authentication: AuthenticationRequest,
        params: Params,
        ...allowed: string[]
        ) => Promise<AuthenticationResult>;
        • Authenticate a given authentication request against a list of strategies.

          Parameter authentication

          The authentication request

          Parameter params

          Service call parameters

          Parameter allowed

          A list of allowed strategy names

        method createAccessToken

        createAccessToken: (
        payload: string | Buffer | object,
        optsOverride?: SignOptions,
        secretOverride?: Secret
        ) => Promise<any>;
        • Create a new access token with payload and options.

          Parameter payload

          The JWT payload

          Parameter optsOverride

          The options to extend the defaults (configuration.jwtOptions) with

          Parameter secretOverride

          Use a different secret instead

        method getStrategies

        getStrategies: (...names: string[]) => AuthenticationStrategy[];
        • Get the registered authentication strategies for a list of names.

          Parameter names

          The list or strategy names

        method handleConnection

        handleConnection: (
        event: ConnectionEvent,
        connection: any,
        authResult?: AuthenticationResult
        ) => Promise<void>;

          method parse

          parse: (
          req: IncomingMessage,
          res: ServerResponse,
          ...names: string[]
          ) => Promise<AuthenticationRequest>;
          • Parse an HTTP request and response for authentication request information.

            Parameter req

            The HTTP request

            Parameter res

            The HTTP response

            Parameter names

            A list of strategies to use

          method register

          register: (name: string, strategy: AuthenticationStrategy) => void;
          • Register a new authentication strategy under a given name.

            Parameter name

            The name to register the strategy under

            Parameter strategy

            The authentication strategy instance

          method verifyAccessToken

          verifyAccessToken: (
          accessToken: string,
          optsOverride?: JwtVerifyOptions,
          secretOverride?: Secret
          ) => Promise<any>;
          • Verifies an access token.

            Parameter accessToken

            The token to verify

            Parameter optsOverride

            The options to extend the defaults (configuration.jwtOptions) with

            Parameter secretOverride

            Use a different secret instead

          class AuthenticationBaseStrategy

          class AuthenticationBaseStrategy implements AuthenticationStrategy {}

            property app

            app?: Application;

              property authentication

              authentication?: AuthenticationBase;

                property configuration

                readonly configuration: any;

                  property entityService

                  readonly entityService: Service<any>;

                    property name

                    name?: string;

                      method setApplication

                      setApplication: (app: Application) => void;

                        method setAuthentication

                        setAuthentication: (auth: AuthenticationBase) => void;

                          method setName

                          setName: (name: string) => void;

                            class AuthenticationService

                            class AuthenticationService
                            extends AuthenticationBase
                            implements Partial<ServiceMethods<AuthenticationResult>> {}

                              constructor

                              constructor(app: Application, configKey?: string, options?: {});

                                method create

                                create: (data: AuthenticationRequest, params: Params) => Promise<any>;
                                • Create and return a new JWT for a given authentication request. Will trigger the login event.

                                  Parameter data

                                  The authentication request (should include strategy key)

                                  Parameter params

                                  Service call parameters

                                method getPayload

                                getPayload: (
                                _authResult: AuthenticationResult,
                                params: Params
                                ) => Promise<Params>;
                                • Return the payload for a JWT based on the authentication result. Called internally by the create method.

                                  Parameter _authResult

                                  The current authentication result

                                  Parameter params

                                  The service call parameters

                                method getTokenOptions

                                getTokenOptions: (
                                authResult: AuthenticationResult,
                                params: Params
                                ) => Promise<any>;
                                • Returns the JWT options based on an authentication result. By default sets the JWT subject to the entity id.

                                  Parameter authResult

                                  The authentication result

                                  Parameter params

                                  Service call parameters

                                method remove

                                remove: (id: string | null, params: Params) => Promise<AuthenticationResult>;
                                • Mark a JWT as removed. By default only verifies the JWT and returns the result. Triggers the logout event.

                                  Parameter id

                                  The JWT to remove or null

                                  Parameter params

                                  Service call parameters

                                method setup

                                setup: () => void;
                                • Validates the service configuration.

                                class JWTStrategy

                                class JWTStrategy extends AuthenticationBaseStrategy {}

                                  property configuration

                                  readonly configuration: any;

                                    property expirationTimers

                                    expirationTimers: WeakMap<object, any>;

                                      method authenticate

                                      authenticate: (
                                      authentication: AuthenticationRequest,
                                      params: Params
                                      ) => Promise<{
                                      accessToken: any;
                                      authentication: { strategy: string; accessToken: any; payload: any };
                                      }>;

                                        method getEntity

                                        getEntity: (id: string, params: Params) => Promise<any>;
                                        • Return the entity for a given id

                                          Parameter id

                                          The id to use

                                          Parameter params

                                          Service call parameters

                                        method getEntityId

                                        getEntityId: (authResult: AuthenticationResult, _params: Params) => Promise<any>;

                                          method getEntityQuery

                                          getEntityQuery: (_params: Params) => Promise<{}>;

                                            method handleConnection

                                            handleConnection: (
                                            event: ConnectionEvent,
                                            connection: any,
                                            authResult?: AuthenticationResult
                                            ) => Promise<void>;

                                              method parse

                                              parse: (
                                              req: IncomingMessage
                                              ) => Promise<{ strategy: string; accessToken: string }>;

                                                method verifyConfiguration

                                                verifyConfiguration: () => void;

                                                  Interfaces

                                                  interface AuthenticationRequest

                                                  interface AuthenticationRequest {}

                                                    property strategy

                                                    strategy?: string;

                                                      index signature

                                                      [key: string]: any;

                                                        interface AuthenticationResult

                                                        interface AuthenticationResult {}

                                                          index signature

                                                          [key: string]: any;

                                                            interface AuthenticationService

                                                            interface AuthenticationService extends ServiceAddons<AuthenticationResult> {}

                                                              interface AuthenticationStrategy

                                                              interface AuthenticationStrategy {}

                                                                method authenticate

                                                                authenticate: (
                                                                authentication: AuthenticationRequest,
                                                                params: Params
                                                                ) => Promise<AuthenticationResult>;
                                                                • Authenticate an authentication request with this strategy. Should throw an error if the strategy did not succeed.

                                                                  Parameter authentication

                                                                  The authentication request

                                                                  Parameter params

                                                                  The service call parameters

                                                                method handleConnection

                                                                handleConnection: (
                                                                event: ConnectionEvent,
                                                                connection: any,
                                                                authResult?: AuthenticationResult
                                                                ) => Promise<void>;
                                                                • Update a real-time connection according to this strategy.

                                                                  Parameter connection

                                                                  The real-time connection

                                                                  Parameter context

                                                                  The hook context

                                                                method parse

                                                                parse: (
                                                                req: IncomingMessage,
                                                                res: ServerResponse
                                                                ) => Promise<AuthenticationRequest | null>;
                                                                • Parse a basic HTTP request and response for authentication request information.

                                                                  Parameter req

                                                                  The HTTP request

                                                                  Parameter res

                                                                  The HTTP response

                                                                method setApplication

                                                                setApplication: (app: Application) => void;
                                                                • Implement this method to get access to the Feathers application

                                                                  Parameter app

                                                                  The Feathers application instance

                                                                method setAuthentication

                                                                setAuthentication: (auth: AuthenticationBase) => void;
                                                                • Implement this method to get access to the AuthenticationService

                                                                  Parameter auth

                                                                  The AuthenticationService

                                                                method setName

                                                                setName: (name: string) => void;
                                                                • Implement this method to get access to the strategy name

                                                                  Parameter name

                                                                  The name of the strategy

                                                                method verifyConfiguration

                                                                verifyConfiguration: () => void;
                                                                • Implement this method to verify the current configuration and throw an error if it is invalid.

                                                                Type Aliases

                                                                type ConnectionEvent

                                                                type ConnectionEvent = 'login' | 'logout' | 'disconnect';

                                                                  Namespaces

                                                                  namespace hooks

                                                                  module 'src/hooks/index.ts' {}

                                                                    variable authenticate

                                                                    const authenticate: (
                                                                    originalSettings: string | AuthenticateHookSettings,
                                                                    ...originalStrategies: string[]
                                                                    ) => (context: HookContext) => Promise<HookContext>;

                                                                      variable connection

                                                                      const connection: (
                                                                      event: ConnectionEvent
                                                                      ) => (context: HookContext) => Promise<HookContext>;

                                                                        variable event

                                                                        const event: (
                                                                        event: ConnectionEvent
                                                                        ) => (context: HookContext) => Promise<HookContext>;

                                                                          Package Files (9)

                                                                          Dependencies (9)

                                                                          Dev Dependencies (10)

                                                                          Peer Dependencies (0)

                                                                          No peer dependencies.

                                                                          Badge

                                                                          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@feathersjs/authentication.

                                                                          • Markdown
                                                                            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@feathersjs/authentication)
                                                                          • HTML
                                                                            <a href="https://www.jsdocs.io/package/@feathersjs/authentication"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>