Skip to content

Authentication -- Error Reference

Complete error messages and troubleshooting for the authentication module. See Setup & Configuration for initial setup.

Error Flow Overview

Complete Error Reference

All error messages from the authentication module, organized by source.


Component Errors (AuthenticateComponent)

Thrown during binding() when validating options and configuring services.

Error MessageStatusMethodWhen
[AuthenticateComponent] At least one of jwtOptions or basicOptions must be provided400bindingNeither JWT_OPTIONS nor BASIC_OPTIONS bound in DI container
[AuthenticateComponent] Unknown JOSE standard: {{standard}}400bindingjwtOptions.standard is not 'JWS' or 'JWKS'

defineJWSAuth Errors

Error MessageStatusMethodWhen
[defineJWSAuth] Invalid jwtSecret | Provided: {{jwtSecret}}400defineJWSAuthjwtSecret is falsy or equals 'unknown_secret'
[defineJWSAuth] getTokenExpiresFn is required400defineJWSAuthgetTokenExpiresFn not provided in JWS options

applicationSecret is no longer validated

applicationSecret was previously required and validated by the component. It is now optional — omitting it simply disables AES payload encryption.

defineJWKSAuth Errors (Issuer Mode)

Error MessageStatusMethodWhen
[defineJWKSAuth] keys.private and keys.public are required for issuer mode400defineJWKSAuthkeys.private or keys.public missing
[defineJWKSAuth] keys.format is required and must be one of: pem, jwk400defineJWKSAuthkeys.format missing or not in JWKSKeyFormats.SCHEME_SET
[defineJWKSAuth] kid is required for issuer mode400defineJWKSAuthkid (Key ID) not provided
[defineJWKSAuth] getTokenExpiresFn is required for issuer mode400defineJWKSAuthgetTokenExpiresFn not provided

defineJWKSAuth Errors (Verifier Mode)

Error MessageStatusMethodWhen
[defineJWKSAuth] jwksUrl is required for verifier mode400defineJWKSAuthjwksUrl not provided for verifier mode

defineJWKSAuth Errors (General)

Error MessageStatusMethodWhen
[defineJWKSAuth] Invalid JWKS mode: {{mode}}400defineJWKSAuthmode is not 'issuer' or 'verifier'

defineBasicAuth Errors

Error MessageStatusMethodWhen
[defineBasicAuth] verifyCredentials function is required400defineBasicAuthBASIC_OPTIONS bound without a verifyCredentials callback

defineControllers Errors

Error MessageStatusMethodWhen
[defineControllers] Auth controller requires jwtOptions to be configured400defineControllersuseAuthController: true but no jwtOptions provided

Bearer Token Service Errors (AbstractBearerTokenService)

Base class errors shared by JWSTokenService, JWKSIssuerTokenService, and JWKSVerifierTokenService.

extractCredentials Errors

Error MessageStatusMethodWhen
Unauthorized user! Missing authorization header401extractCredentialsRequest has no Authorization header
Unauthorized user! Invalid schema of request token!401extractCredentialsAuthorization header doesn't start with Bearer
Authorization header value is invalid format. It must follow the pattern: 'Bearer xx.yy.zz' where xx.yy.zz is a valid JWT token.401extractCredentialsAuthorization header doesn't have exactly 2 parts (type + token)

verify Errors

Error MessageStatusMethodWhen
[verify] Invalid request token!401verifyToken value is empty/falsy
[verify] Invalid or expired token401verifydoVerify() threw — token is expired, malformed, or signature invalid

Sanitized error messages

The verify() and generate() methods use sanitized error messages — they do NOT include the original error.message in the thrown error. The full error is logged at error level for debugging but not exposed to clients.

generate Errors

Error MessageStatusMethodWhen
[generate] Invalid token payload!401generatePayload is null/undefined
[generate] Failed to generate token500generateSigning failed (key issue, algorithm mismatch, etc.)

JWS Token Service Errors (JWSTokenService)

Constructor validation errors thrown during DI resolution.

Error MessageStatusMethodWhen
[JWSTokenService] Invalid jwtSecret500constructorjwtSecret is falsy in injected IJWSTokenServiceOptions
[JWSTokenService] Invalid getTokenExpiresFn500constructorgetTokenExpiresFn is falsy in injected options
[getSigningKey] Invalid jwtSecret!400getSigningKeyjwtSecret Uint8Array is null (should not happen after constructor validation)

applicationSecret no longer validated

JWSTokenService no longer validates applicationSecret in its constructor. If not provided, AES payload encryption is simply disabled.


JWKS Issuer Token Service Errors (JWKSIssuerTokenService)

Errors thrown during lazy initialization (ensureInitialized()) and key operations.

Initialization Errors

Error MessageStatusMethodWhen
[JWKSIssuerTokenService] Unknown key driver: {{driver}}500resolveKeyContentkeys.driver is not 'text' or 'file'
[JWKSIssuerTokenService] Invalid raw.priv key!500parseKeyMaterialResolved private key content is empty
[JWKSIssuerTokenService] Invalid raw.pub key!500parseKeyMaterialResolved public key content is empty
[JWKSIssuerTokenService] Invalid JWK key material500parseKeyMaterialJWK JSON parsing or importJWK() failed (PEM format works, JWK content is invalid)
[JWKSIssuerTokenService] Unknown key format: {{format}}500parseKeyMaterialkeys.format is not 'pem' or 'jwk'

File read errors

When using JWKSKeyDrivers.FILE, file read errors from readFile() propagate as Node.js filesystem errors (e.g., ENOENT, EACCES). These are not wrapped — the raw error surfaces during initialization.

Runtime Errors

Error MessageStatusMethodWhen
[getSigningKey] Invalid privateKey!400getSigningKeyPrivate key is null (initialization incomplete or failed)
[JWKSIssuerTokenService] JWKS not initialized yet. Call getJWKSAsync() instead.500getJWKSSync getJWKS() called before lazy init completed

JWKS Verifier Token Service Errors (JWKSVerifierTokenService)

Error MessageStatusMethodWhen
[JWKSVerifierTokenService] Verifier mode cannot sign tokens500getSignerAttempt to call generate() on a verify-only service
[JWKSVerifierTokenService] Verifier mode cannot sign tokens500getSigningKeyAttempt to access signing key on a verify-only service
[JWKSVerifierTokenService] Verifier mode has no token expiry500getDefaultTokenExpiresFnAttempt to access token expiry on a verify-only service

Verifier mode is read-only

JWKSVerifierTokenService cannot generate tokens. Calling generate() throws because both getSigner() and getSigningKey() throw. Only verify() and extractCredentials() are functional.


Basic Token Service Errors (BasicTokenService)

Error MessageStatusMethodWhen
[BasicTokenService] Invalid verifyCredentials function500constructorverifyCredentials not provided in injected TBasicTokenServiceOptions
Unauthorized! Missing authorization header401extractCredentialsRequest has no Authorization header
Unauthorized! Invalid authorization schema, expected Basic401extractCredentialsAuthorization header doesn't start with Basic
Unauthorized! Invalid authorization header format401extractCredentialsHeader doesn't have exactly 2 parts (Basic + base64 value)
Unauthorized! Invalid base64 credentials format401extractCredentialsBase64 decoding failed, no colon separator, or empty username
Unauthorized! Invalid username or password401verifyverifyCredentials callback returned null/falsy

Strategy Registry Errors (AuthenticationStrategyRegistry)

Inherited from AbstractAuthRegistry.

Error MessageStatusMethodWhen
[getKey] Invalid name | name: {{name}}400getKeyStrategy name is empty or falsy
[AuthenticationStrategyRegistry] No items registered400getDefaultNameNo strategies have been registered
[AuthenticationStrategyRegistry] Descriptor not found: {{name}}400resolveDescriptorStrategy with given name is not registered
[AuthenticationStrategyRegistry] Failed to resolve: {{name}}400resolveDescriptorStrategy registered but DI container returned null

Authentication Provider Errors (AuthenticationProvider)

The middleware that executes strategies in the configured mode.

Error MessageStatusMethodWhen
Authentication failed. Tried strategies: {{strategies}}401executeAnyModeAll strategies failed in 'any' mode — each strategy threw during authenticate()
Failed to identify authenticated user!401executeAllModeAll strategies succeeded in 'all' mode but the first strategy's authUser.userId is falsy
Invalid authentication mode | mode: {{mode}}500createAuthenticateMiddlewaremode is not 'any' or 'all'

Auth Controller Factory Errors

Error MessageStatusMethodWhen
[AuthController] Failed to init auth controller | Invalid injectable authentication service!400constructorDI could not resolve the authentication service (service key not bound)

Troubleshooting

"[AuthenticateComponent] At least one of jwtOptions or basicOptions must be provided"

Cause: The component requires at least one authentication method. Neither JWT_OPTIONS nor BASIC_OPTIONS was bound before calling this.component(AuthenticateComponent).

Fix: Bind at least one set of options before registering the component:

typescript
// Option A: JWS (symmetric JWT)
this.bind<TJWTTokenServiceOptions>({ key: AuthenticateBindingKeys.JWT_OPTIONS }).toValue({
  standard: JOSEStandards.JWS,
  options: {
    jwtSecret: env.get('JWT_SECRET'),
    getTokenExpiresFn: () => 86_400,
  },
});

// Option B: JWKS Issuer (asymmetric JWT)
this.bind<TJWTTokenServiceOptions>({ key: AuthenticateBindingKeys.JWT_OPTIONS }).toValue({
  standard: JOSEStandards.JWKS,
  options: {
    mode: JWKSModes.ISSUER,
    algorithm: 'RS256',
    kid: 'my-key-1',
    keys: { driver: JWKSKeyDrivers.FILE, format: JWKSKeyFormats.PEM, private: './keys/private.pem', public: './keys/public.pem' },
    getTokenExpiresFn: () => 86_400,
  },
});

// Then register the component
this.component(AuthenticateComponent);

"[defineJWSAuth] Invalid jwtSecret"

Cause: jwtSecret is missing, empty, or set to the default placeholder 'unknown_secret'. The component validates this during defineJWSAuth().

Fix: Set a strong, unique JWT secret:

bash
# .env
JWT_SECRET=your-strong-jwt-secret-at-least-32-chars
typescript
this.bind<TJWTTokenServiceOptions>({ key: AuthenticateBindingKeys.JWT_OPTIONS }).toValue({
  standard: JOSEStandards.JWS,
  options: {
    jwtSecret: env.get('JWT_SECRET'),
    getTokenExpiresFn: () => 86_400,
  },
});

"[defineJWSAuth] getTokenExpiresFn is required"

Cause: getTokenExpiresFn was not provided in the JWS options.

Fix: Include a function that returns the token expiry time in seconds:

typescript
options: {
  jwtSecret: env.get('JWT_SECRET'),
  getTokenExpiresFn: () => Number(env.get('JWT_EXPIRES_IN') || 86_400),
}

"[defineJWKSAuth] keys.private and keys.public are required for issuer mode"

Cause: JWKS issuer mode requires both a private key (for signing) and a public key (for the /certs JWKS endpoint). One or both were not provided.

Fix: Provide both keys in the options:

typescript
options: {
  mode: JWKSModes.ISSUER,
  algorithm: 'RS256',
  kid: 'my-key-1',
  keys: {
    driver: JWKSKeyDrivers.FILE,
    format: JWKSKeyFormats.PEM,
    private: './keys/private.pem',
    public: './keys/public.pem',
  },
  getTokenExpiresFn: () => 86_400,
}

"[defineJWKSAuth] keys.format is required and must be one of: pem, jwk"

Cause: keys.format is missing or invalid. The component validates against JWKSKeyFormats.SCHEME_SET (values are lowercase: 'pem', 'jwk').

Fix: Use one of the supported formats:

typescript
keys: {
  driver: JWKSKeyDrivers.FILE,
  format: JWKSKeyFormats.PEM,  // or JWKSKeyFormats.JWK
  private: './keys/private.pem',
  public: './keys/public.pem',
}

"[defineJWKSAuth] kid is required for issuer mode"

Cause: The Key ID (kid) is required for the issuer to include in the JWKS and JWT headers. It allows verifiers to identify which key was used to sign a token.

Fix: Provide a unique key identifier:

typescript
options: {
  mode: JWKSModes.ISSUER,
  kid: 'my-service-key-2024',
  // ...
}

"[defineJWKSAuth] jwksUrl is required for verifier mode"

Cause: JWKS verifier mode needs the URL of the issuer's /certs endpoint to fetch the public key set.

Fix: Provide the JWKS endpoint URL:

typescript
this.bind<TJWTTokenServiceOptions>({ key: AuthenticateBindingKeys.JWT_OPTIONS }).toValue({
  standard: JOSEStandards.JWKS,
  options: {
    mode: JWKSModes.VERIFIER,
    jwksUrl: 'https://auth-service.example.com/certs',
  },
});

"[JWKSIssuerTokenService] Invalid JWK key material"

Cause: When using JWKSKeyFormats.JWK, the private or public key content failed to parse as valid JSON, or importJWK() rejected it. The full error is logged at error level.

Fix: Ensure your JWK keys are valid JSON objects:

json
{
  "kty": "RSA",
  "n": "...",
  "e": "AQAB",
  "d": "...",
  "alg": "RS256",
  "use": "sig"
}

If stored as files, ensure the file contains valid JSON (not PEM). If stored as text (JWKSKeyDrivers.TEXT), ensure the string is parseable JSON.

"[JWKSIssuerTokenService] JWKS not initialized yet. Call getJWKSAsync() instead."

Cause: getJWKS() (synchronous) was called before the lazy initialization completed. The issuer loads keys asynchronously on first use.

Fix: Use the async variant:

typescript
const jwks = await issuerService.getJWKSAsync();

The built-in JWKSController already uses getJWKSAsync(), so this error only occurs if you call getJWKS() directly in custom code.

"[JWKSVerifierTokenService] Verifier mode cannot sign tokens"

Cause: generate() was called on a JWKSVerifierTokenService. The verifier mode only has access to public keys (via the remote JWKS URL) and cannot sign tokens.

Fix: Token generation should only happen on the issuer service. If you need both signing and verification in the same application, use JWKSModes.ISSUER — the issuer can both sign and verify.

"Authentication failed. Tried strategies: jwt, jwks"

Cause: All configured strategies failed in 'any' mode. Each strategy attempted to authenticate the request and threw.

Fix: Verify the client is sending the correct Authorization header:

  • For Bearer (JWS/JWKS): Authorization: Bearer <token>
  • For Basic: Authorization: Basic <base64(username:password)>

Common causes:

  • Token expired
  • Token signed with a different key
  • Wrong strategy name in route config (e.g., 'jwt' when using JWKS)
  • Strategy not registered (see below)

"[AuthenticationStrategyRegistry] Descriptor not found: jwt"

Cause: A route references strategy name 'jwt' but no strategy with that name has been registered in the AuthenticationStrategyRegistry.

Fix: Strategies are not auto-registered by AuthenticateComponent. You must manually register them after the component:

typescript
// In preConfigure() or after component registration
this.component(AuthenticateComponent);

// Manual strategy registration
AuthenticationStrategyRegistry.getInstance().register({
  container: this,
  strategies: [
    { strategy: JWSAuthenticationStrategy, name: 'jwt' },
  ],
});

See Usage & Examples for the full registration flow.

"[AuthController] Failed to init auth controller | Invalid injectable authentication service!"

Cause: The auth controller factory could not resolve the authentication service from DI. The service key (configured via controllerOpts.serviceKey) is not bound.

Fix: Register your AuthenticationService before registering the component:

typescript
// 1. Register your auth service
this.service(AuthenticationService);

// 2. Configure REST options with matching service key
this.bind<TAuthenticationRestOptions>({ key: AuthenticateBindingKeys.REST_OPTIONS }).toValue({
  useAuthController: true,
  controllerOpts: {
    restPath: '/auth',
    serviceKey: 'services.AuthenticationService',
  },
});

// 3. Register component
this.component(AuthenticateComponent);

"[defineBasicAuth] verifyCredentials function is required"

Cause: BASIC_OPTIONS was bound but without a verifyCredentials callback.

Fix: Provide a verifyCredentials function:

typescript
this.bind<TBasicTokenServiceOptions>({ key: AuthenticateBindingKeys.BASIC_OPTIONS }).toValue({
  verifyCredentials: async ({ credentials, context }) => {
    const user = await userRepo.findByUsername(credentials.username);
    if (user && await bcrypt.compare(credentials.password, user.passwordHash)) {
      return { userId: user.id };
    }
    return null;
  },
});

"[verify] Invalid or expired token"

Cause: Token verification failed. This is a sanitized error — the original error (expired, wrong signature, malformed) is logged internally but not exposed to clients.

Fix: Check the application logs for the full error. Common causes:

  • Token has expired (check exp claim)
  • Token signed with a different secret/key
  • Token was tampered with
  • Algorithm mismatch between signing and verification
bash
# Look for the full error in logs
grep "Failed to verify token" logs/application.log

Error Categories

Startup Errors (400)

These errors prevent the application from starting. They occur during component binding or DI resolution. The component uses getError() without an explicit status code, which defaults to 400.

CategoryErrorsWhen
Missing optionsAt least one of jwtOptions or basicOptionsNo auth options bound
Invalid secretsInvalid jwtSecretJWS secret missing or placeholder
Missing functionsgetTokenExpiresFn is requiredNo expiry function
JWKS key validationkeys.private and keys.public are requiredMissing key material
JWKS format validationkeys.format is requiredInvalid key format
Mode validationUnknown JOSE standard, Invalid JWKS modeInvalid discriminated union value
DI resolutionInvalid injectable authentication serviceService not registered

Why 400 and not 500?

The AuthenticateComponent and AbstractAuthRegistry use getError({ message }) without specifying statusCode. The getError() helper defaults to statusCode: 400. Service-level initialization errors (file I/O, key parsing) explicitly set statusCode: 500.

Runtime Errors (401)

These errors occur during request processing and return 401 Unauthorized to clients.

CategoryErrorsWhen
Missing headerMissing authorization headerNo Authorization header
Wrong schemeInvalid schema of request tokenHeader uses wrong auth type
Invalid formatInvalid authorization header formatMalformed header value
Token verificationInvalid or expired tokenJWT verification failed
Credential verificationInvalid username or passwordBasic auth credentials rejected
Strategy exhaustionAuthentication failed. Tried strategies:All strategies failed

Structural Errors (500)

These errors indicate programming mistakes, I/O failures, or misconfiguration that should be fixed in code.

CategoryErrorsWhen
Unsupported operationVerifier mode cannot sign tokensWrong service used for signing
Uninitialized stateJWKS not initialized yetSync access before async init
Key/file I/O errorsUnknown key driver, Invalid raw.priv key, Invalid JWK key materialJWKS key loading/parsing failed
Invalid modeInvalid authentication modeUnknown mode passed to provider

See Also