Updated on 2024-07-01 GMT+08:00

Implementation Procedure

Prerequisites

  1. A custom authentication function has been created. For details, see Creating a Function. The function's return value must have a context field with key-value pairs, where the values are limited to string, boolean, or integer. The key-value pair corresponds to the Parameter Name and Condition Value of the System parameter - Frontend authentication parameter configured in Policy Conditions.
    Figure 1 Custom authentication function
  2. A custom frontend authorizer has been created. For details, see Custom Authorizer.

Configuring the API Frontend

  1. Log in to the APIG console.
  2. Select a gateway at the top of the navigation pane.
  3. In the navigation pane, choose API Management > APIs.
  4. Click Create API > Create API and configure the frontend.

    Table 1 Frontend configuration

    Parameter

    Description

    API Name

    Enter a name that conforms to specific rules to facilitate search.

    Group

    The default option is DEFAULT.

    URL

    Method: Request method of the API. Default: GET.

    Protocol: Request protocol of the API. Default: HTTPS.

    Subdomain Name: The system automatically allocates a subdomain name to each API group for internal testing. The subdomain name can be accessed 1,000 times a day.

    Path: Path for requesting the API. In this example, enter /1234.

    Gateway Response

    Select a response to be displayed if the gateway fails to process an API request.

    The default gateway response is default.

    Authentication Mode

    API authentication mode. Select Custom.

    Custom Authorizer

    Select the custom authorizer created in Prerequisites.

Configuring the API Backend

  1. After configuring the frontend, click Next.

    Set Backend Type to Mock and enter Default backend in the Response.

  2. Click to add a backend policy based on the following table.

    Table 2 Policy backend configuration

    Parameter

    Description

    Name

    Enter a policy name that conforms to specific rules to facilitate search.

    Backend Type

    Select Mock.

    Response

    Enter Policy backend.

    Policy Conditions

    • Source: Select System parameter - Frontend authentication parameter.
    • Parameter Name: Enter authstatus1 under the context field in the response body of the custom authentication function created in Prerequisites.
    • Condition Type: Select Equal.
    • Condition Value: Enter False under the context field in the response body of the custom authentication function created in Prerequisites.

  3. Click Finish.

Debugging the API

The Mock backend type helps you view the result with a response. For other backend types, you can check whether the backend address is successfully accessed.

  1. On the APIs page, click Debug to debug the created API.

    If 200 OK is displayed in Response, the API is successfully invoked. If the Policy backend is also returned, the configured policy backend condition matches the key-value pair in the context field in the response body of the custom authentication function, and the API invokes the policy backend.

  2. On the APIs page, click Modify. The API configuration page is displayed.
  3. Click Next. On the backend configuration page, change Condition Value of the added policy to True.
  4. Click Finish.
  5. Debug the API again.

    The policy backend parameter and condition value do not match any key-value pair of the frontend custom authentication function. Therefore, the API policy backend cannot be matched based on the frontend authentication parameters. In this case, 200 OK is displayed with the return result of Default backend, indicating that the default backend of the API is invoked.