Options
All
  • Public
  • Public/Protected
  • All
Menu
label

COMMON

description

This class serves as an authentication handler for users. This can be used to authenticate the access to [a subset] of your dApp routes and modules.

You can use this service to generate authentication challenges, or to validate the presence of authentication challenges on-chain, or to get access tokens and refresh tokens for authenticated users.

since

v0.2.0

Hierarchy

  • AuthService

Index

Constructors

Properties

authSecret: string

Contains the authentication secret as set in config.security.ts. It defines a symmetric secret key and is used for signing access tokens.

access

private

var

{string}

challengeSize: number

Contains the challenge size as set in config.security.ts.

Note that by modifying the default challenge size, you may affect the operations processor and thereby the data that is returned by the backend.

access

protected

var

{number}

Contains the generated cookie payload. This property is filled using configuration fields in the config/security.ts configuration file.

access

protected

var

{CookiePayload}

Methods

  • findRecentChallenge(registry: string, challenge: string): Promise<TransferTransaction>
  • This method fetches recent transactions using the network service (promises are delegated), and interprets the content of messages in transfer transactions.

    A TransferTransaction instance will only be returned given that the challenge can be found in a transaction, otherwise it returns undefined.

    async
    access

    protected

    Parameters

    • registry: string

      The authentication registry address to search for in recent transactions.

    • challenge: string

      The authentication challenge to search for in recent transactions.

    Returns Promise<TransferTransaction>

    A transfer transaction instance given a validated authentication challenge, or undefined.

  • This method accepts a AuthenticationPayload object of which the address field is used to find the related account-sessions document by address.

    We read the access/refresh token(s) from the account-sessions document if possible, otherwise we generate them and sign them using the authentication secret as provided in the configuration file config/security.ts.

    Note that the expiration of access tokens automatically happens after one hour of lifetime.

    Parameters

    Returns Promise<AccessTokenDTO>

    An access token for the authenticated user and possibly a refresh token (first time).

  • getAccount(request: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, cookieName?: string): Promise<AccountDocument>
  • This method extract an access token from the AuthService class and use it to query the owner account from the database.

    Parameters

    • request: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      An express Request object.

    • cookieName: string = AppConfiguration.dappName

      The name of the cookie to perform access token extraction from.

    Returns Promise<AccountDocument>

    The account that has the provided cookie.

  • getChallenge(): string
  • This method generates a random authentication challenge.

    The size of the generated authentication challenges can be changed in the configuration file config/security.ts.

    Note that by modifying the default challenge size, you may affect the operations processor and thereby the data that is returned by the backend.

    Returns string

    An authentication challenge that can be attached on-chain.

  • This method returns the configured cookie payload. Note that we do not permit the secret to be shared by destructuring it out of the returned payload, i.e. the secret is never returned with this method.

    Returns CookiePayload

    A cookie payload without the secret field.

  • getTransactionQuery(registry: string): TransactionSearchCriteria
  • This method returns a transactions query that is compatible with dHealth Network Node's REST gateway.

    To change the registry, you can modify the auth.registries configuration field in config/security.ts. Please, note that changing this configuration field may affect ongoing log-in operations of end-users.

    link

    https://docs.dhealth.com/reference/searchconfirmedtransactions

    access

    protected

    Parameters

    • registry: string

    Returns TransactionSearchCriteria

    A list of REST-gateway compatible transaction search queries.

  • refreshAccessToken(userAddress: string, refreshToken: string): Promise<AccessTokenDTO>
  • This method accepts an end-user address (string) and a refresh token which must be valid as a pair.

    This method will generate a new access token, sign it using the authentication secret as provided in the configuration file config/security.ts and returns it in a AccessTokenDTO object.

    Note that the expiration of access tokens automatically happens after one hour of lifetime.

    access

    public

    async
    throws

    {HttpException} Given invalid log-in state for the requested account session.

    Parameters

    • userAddress: string

      The address of the end-user for which a new access token must be generated ("Refresh").

    • refreshToken: string

    Returns Promise<AccessTokenDTO>

    An access token for the authenticated user.

  • This method validates the presence of an authentication challenge on-chain, in transfer transaction messages. When we can't find a recent transaction (confirmed/unconfirmed) that contains the challenge value this method will throw a HttpException with status code 401.

    In case of a successful validation attempts, i.e. when the challenge parameter has been found in a recent transfer transaction's message, a document will be insert in the collection authChallenges.

    throws

    {HttpException} Given challenge could not be found in recent transactions.

    Parameters

    • param0: AccessTokenRequest

      An authentication challenge, as created with getChallenge.

    • markAsUsed: boolean = true

      A boolean flag that determine whether the challenge should be marked as used.

    Returns Promise<AuthenticationPayload>

    An authenticated account session described with AuthenticationPayload.

  • extractToken(request: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, cookieName?: string): string
  • This method extracts a JWT Token from different parts of the request object with following order:

    • Signed cookies
    • Unsigned cookies
    • Authorization header

    Parameters

    • request: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      A express Request object.

    • cookieName: string = AppConfiguration.dappName

    Returns string

Generated using TypeDoc