Skip to main content

Class: AuthMiddleware

Defined in: service-core/src/auth/AuthMiddleware.ts:19

A set of common utilities for performing authentication using one or more strategies.

Constructors​

Constructor​

new AuthMiddleware(): AuthMiddleware

Returns​

AuthMiddleware

Properties​

_objectFactory?​

protected optional _objectFactory?: ObjectFactory

Defined in: service-core/src/auth/AuthMiddleware.ts:21


strategies​

readonly strategies: Map<string, AuthStrategy>

Defined in: service-core/src/auth/AuthMiddleware.ts:30

The authentication strategies that have been registered.

Methods​

authenticate()​

authenticate(strategies, req, res?, required?): Promise<AuthResult | undefined>

Defined in: service-core/src/auth/AuthMiddleware.ts:59

Performs authentication of the given request using one of the provided strategies.

Each strategy is tried in order until one succeeds. A strategy that throws doesn't stop the others from being tried, since several strategies can claim the same credential (e.g. jwt and oauth_bearer both read an Authorization: Bearer header). If no strategy succeeds and at least one threw, the first error thrown is rethrown, whether or not required is set, so callers can still tell a bad credential from a missing one.

Parameters​

strategies​

string[]

The list of strategy names to attempt authentication with.

req​

HttpRequest

The request containing data to perform authenticate with.

res?​

HttpResponse

The response to use when writing back directly to the client.

required?​

boolean

Set to true to if authentication is required to pass, otherwise set to false.

Returns​

Promise<AuthResult | undefined>


authenticateSync()​

authenticateSync(strategies, req, res?, required?): AuthResult | undefined

Defined in: service-core/src/auth/AuthMiddleware.ts:93

Performs authentication of the given request using one of the provided strategies.

This is the synchronous version of authenticate that performs blocking based authentication. It follows the same rules for strategies that throw.

Parameters​

strategies​

string[]

The list of strategy names to attempt authentication with.

req​

HttpRequest

The request containing data to perform authenticate with.

res?​

HttpResponse

The response to use when writing back directly to the client.

required?​

boolean

Set to true to if authentication is required to pass, otherwise set to false.

Returns​

AuthResult | undefined


authWebSocket()​

authWebSocket(required, strategies?): RequestHandler

Defined in: service-core/src/auth/AuthMiddleware.ts:176

Returns a request handler function that will perform authentication of a websocket connection. Authentication can be handled in two ways:

  1. Authorization header (or any other credential a strategy reads from the upgrade request)
  2. Negotiation via handshake

Pre-upgrade auth (see RouteUtils.registerRoute()) can only run strategies synchronously. When it didn't authenticate the connection, this handler first runs the strategies asynchronously against the upgrade request, which covers async-only strategies such as oauth_bearer. If that doesn't authenticate either, it waits for a LOGIN message whose data token is also verified through strategies, sent to them as an Authorization: Bearer header.

When required is false, a failed or missing credential never closes the connection; the handler proceeds anonymously.

Parameters​

required​

boolean

Set to true to indicate that auth is required, otherwise false.

strategies?​

string[] = ...

The strategy names to authenticate with. Defaults to ["jwt"], matching RouteUtils.

Returns​

RequestHandler


register()​

register(name, strategy): void

Defined in: service-core/src/auth/AuthMiddleware.ts:331

Registers the provided authentication strategy to be used

Parameters​

name​

string

The name of the authentication type to associate the given strategy with

strategy​

AuthStrategy

The strategy to register

Returns​

void