AWSWebCore class implements AWS-based operations for Lemoncloud authentication logic. Provides comprehensive token management, credential building, and authenticated request capabilities.

Implements

Constructors

  • Creates an instance of AWSWebCore with the specified configuration. Initializes internal services including token storage, logging, and HTTP client.

    Parameters

    • config: WebCoreConfig<"aws">

      The AWS-specific configuration object containing OAuth endpoints, region settings, and other AWS parameters

    Returns AWSWebCore

Properties

config: WebCoreConfig<"aws">

The AWS-specific configuration object containing OAuth endpoints, region settings, and other AWS parameters

sharedAxiosInstance: AxiosInstance
tokenStorage: AWSStorageService

Methods

  • Private

    Builds AWS credentials from cached storage data. Private method that creates AWS.Credentials object from stored credential data.

    Returns Promise<void>

    Promise that resolves when credentials are built and set

    Throws

    Throws if cached credentials are missing or invalid

  • Private

    Builds AWS credentials from an OAuth token and saves to storage. Private method that processes token data, saves it to storage, and creates AWS credentials.

    Parameters

    Returns Promise<void>

    Promise that resolves when token is saved and credentials are created

    Throws

    Throws if token is missing required fields or credential creation fails

  • Builds AWS credentials from cached storage data and sets them in AWS.config. Uses previously stored credential information to recreate AWS credentials.

    Returns Promise<Credentials>

    Promise resolving to the built AWS credentials

    Throws

    Throws if cached credentials are invalid or AWS credentials cannot be created

    Example

    const credentials = await webCore.buildCredentialsByStorage();
    // AWS.config.credentials is now set from cached data
  • Builds AWS credentials from an OAuth token and sets them in AWS.config. Saves the token to storage and creates AWS credentials for subsequent API calls.

    Parameters

    • token: LemonOAuthToken

      The OAuth token containing AWS credential information

    Returns Promise<Credentials>

    Promise resolving to the built AWS credentials

    Throws

    Throws if token is invalid or AWS credentials cannot be created

    Example

    const credentials = await webCore.buildCredentialsByToken(oauthToken);
    // AWS.config.credentials is now set and ready for use
  • Creates an HTTP request builder for unsigned requests. Use this for requests that don't require AWS signature authentication.

    Parameters

    • config: AxiosRequestConfig<any>

      The Axios request configuration object containing method, URL, headers, and other request parameters

    Returns HttpRequestBuilder

    A configured HTTP request builder instance

    Example

    const builder = webCore.buildRequest({
    method: 'GET',
    url: '/api/public-endpoint'
    });
    const response = await builder.execute();
  • Creates an HTTP request builder with AWS signature authentication. Use this for requests to AWS services or signed API endpoints.

    Parameters

    • config: AxiosRequestConfig<any>

      The Axios request configuration object

    Returns AWSHttpRequestBuilder

    A configured AWS HTTP request builder with signature capabilities

    Example

    const builder = webCore.buildSignedRequest({
    method: 'POST',
    url: '/api/aws-protected-endpoint'
    });
  • Changes the user's active site and obtains new credentials for the target site. Useful for multi-tenant applications where users can switch between different sites/organizations.

    Parameters

    • changeSiteBody: ChangeSiteBody

      Object containing siteId and userId for the target site

    • Optional url: string

      Optional custom URL for the site change endpoint

    Returns Promise<Credentials>

    Promise resolving to new AWS credentials for the target site

    Throws

    Throws if changeSiteBody is invalid, authId is missing, or site change fails

    Example

    const credentials = await webCore.changeUserSite({
    siteId: 'new-site-123',
    userId: 'user-456'
    });
    // User is now authenticated for the new site
  • Private

    Creates and sets AWS credentials in the global AWS configuration. Private method that instantiates AWS.Credentials and assigns it to AWS.config.credentials.

    Parameters

    Returns void

    No return value, sets AWS.config.credentials directly

  • Retrieves current AWS credentials, refreshing them if necessary. Checks token validity and performs refresh if the token is expired or near expiration.

    Returns Promise<Credentials>

    Promise resolving to current AWS credentials, or null if no valid token exists or refresh fails

    Throws

    Logs errors but returns null instead of throwing

    Example

    const credentials = await webCore.getCredentials();
    if (credentials) {
    // Use credentials for AWS API calls
    } else {
    // No valid credentials, authentication required
    }
  • Private

    Retrieves the current AWS credentials from AWS.config. Private method that validates and returns the currently configured AWS credentials.

    Returns Promise<Credentials>

    Promise resolving to current AWS credentials

    Throws

    Throws if no credentials are configured or credential validation fails

  • Retrieves all saved tokens from storage as a key-value map. Useful for debugging, backup, or migration purposes.

    Returns Promise<{
        [key: string]: string;
    }>

    Promise resolving to an object containing all stored token data with keys and values

  • Retrieves the shared Axios instance used for HTTP requests. This instance is pre-configured and shared across all requests to maintain consistency.

    Returns AxiosInstance

    The configured Axios instance for making HTTP requests

  • Generates a cryptographic signature for token-based operations. Creates a time-based signature using stored token information for secure API calls.

    Returns Promise<TokenSignature>

    Promise resolving to signature object containing: - authId: Authentication identifier - current: Current timestamp in ISO format - signature: Calculated cryptographic signature - originToken: Original token data used for signature

    Throws

    Throws if cached token is invalid or signature calculation fails

  • Retrieves the token storage service instance. Provides access to the underlying storage service for advanced token management operations.

    Returns AWSStorageService

    The storage service that manages OAuth tokens and credentials

  • Initializes the AWS WebCore service by validating cached tokens and setting up credentials. Performs token refresh if necessary or builds credentials from cached data. This method should be called before using any authenticated operations.

    Returns Promise<AWSWebCoreState>

    A promise that resolves to the initialization state: - 'no-token': No valid token found - 'refreshed': Token was refreshed successfully - 'build': Credentials built from existing valid token

    Throws

    Throws an error if token refresh fails or credentials cannot be built

    Example

    const webCore = new AWSWebCore(config);
    const state = await webCore.init();
    if (state === 'no-token') {
    // Handle authentication required
    }
  • Checks if the user is currently authenticated with valid credentials. Performs token validation and refresh if necessary before determining authentication status.

    Returns Promise<boolean>

    Promise resolving to true if authenticated with valid credentials, false if no token exists or authentication fails

    Throws

    Logs errors but doesn't throw, returning false on any authentication failure

    Example

    if (await webCore.isAuthenticated()) {
    // User is authenticated, proceed with protected operations
    } else {
    // Redirect to login or handle unauthenticated state
    }
  • Logs out the user by clearing AWS credentials and removing stored tokens. Performs complete cleanup of authentication state.

    Returns Promise<void>

    Promise that resolves when logout is complete

    Throws

    Throws if token cleanup fails

    Example

    await webCore.logout();
    // User is now logged out, all credentials cleared
  • Refreshes the cached OAuth token by calling the refresh endpoint. Obtains new credentials and updates AWS.config with fresh authentication data.

    Parameters

    • Optional domain: string = ''

      Optional domain parameter for multi-tenant refresh requests

    • Optional url: string = ''

      Optional custom URL for the refresh endpoint, defaults to config.oAuthEndpoint

    Returns Promise<Credentials>

    Promise resolving to new AWS credentials on success, null if refresh fails or token is invalid

    Throws

    Logs errors but returns null instead of throwing

    Example

    const newCredentials = await webCore.refreshCachedToken();
    if (newCredentials) {
    // Token successfully refreshed
    } else {
    // Refresh failed, user may need to re-authenticate
    }
  • Executes an HTTP request without AWS signature authentication. Suitable for public endpoints or non-AWS services.

    Type Parameters

    • T

      The expected response data type

    Parameters

    • method: string

      The HTTP method (GET, POST, PUT, DELETE, etc.)

    • url: string

      The complete request URL or base URL

    • Optional params: Params = {}

      Query parameters to append to the URL

    • Optional body: Body

      The request body for POST/PUT requests

    • Optional config: AxiosRequestConfig<any>

      Additional Axios configuration options

    Returns Promise<HttpResponse<T>>

    Promise resolving to the HTTP response with typed data

    Throws

    Throws on network errors, HTTP errors, or request configuration issues

    Example

    const response = await webCore.request<UserData>(
    'GET',
    '/api/users/123',
    { include: 'profile' }
    );
  • Saves KMS (Key Management Service) configuration to storage. Stores KMS ARN and other encryption-related configuration for later use.

    Parameters

    • kms: LemonKMS

      The KMS configuration object containing ARN and encryption settings

    Returns Promise<void>

    Promise that resolves when KMS configuration is successfully saved

    Throws

    Throws if KMS configuration cannot be saved to storage

  • Configures whether to use the X-Lemon-Identity header in requests. Controls identity header inclusion for request identification and tracking.

    Parameters

    • use: boolean

      True to include X-Lemon-Identity header, false to exclude it

    Returns Promise<void>

    Promise that resolves when setting is saved

  • Configures whether to use the X-Lemon-Language header with a specific key. Controls language header inclusion for localization and language preference tracking.

    Parameters

    • use: boolean

      True to include X-Lemon-Language header, false to exclude it

    • Optional key: string

      The language key to use when use is true; required if use is true

    Returns Promise<void>

    Promise that resolves when setting is saved

    Example

    await webCore.setUseXLemonLanguage(true, 'en-US');
    // X-Lemon-Language header will be included with 'en-US' value
  • Executes an HTTP request with AWS signature authentication. Automatically signs the request using stored AWS credentials.

    Type Parameters

    • T

      The expected response data type

    Parameters

    • method: string

      The HTTP method (GET, POST, PUT, DELETE, etc.)

    • url: string

      The complete request URL or base URL

    • Optional params: Params = {}

      Query parameters to append to the URL

    • Optional body: Body

      The request body for POST/PUT requests

    • Optional config: AxiosRequestConfig<any>

      Additional Axios configuration options

    Returns Promise<HttpResponse<T>>

    Promise resolving to the signed HTTP response

    Throws

    Throws on authentication errors, network errors, or signature failures

    Example

    const response = await webCore.signedRequest<ApiResponse>(
    'POST',
    '/api/protected-resource',
    {},
    { data: 'sensitive information' }
    );