All Products
Search
Document Center

Identity as a Service:Securely access Model Studio AI services without static keys using IDaaS M2M and AI Gateway

Last Updated:Jan 26, 2026

Integrate the Machine-to-Machine (M2M) authentication feature of Alibaba Cloud Identity as a Service (IDaaS) EIAM with AI Gateway. This enables clients to securely access Alibaba Cloud Model Studio AI services without static credentials.

Background

As artificial intelligence (AI) technology becomes more widespread, the integration between AI applications and large language model (LLM) services is increasing. In traditional access models, AI applications often rely on static AccessKey pairs (AccessKeys) to call LLM service APIs. However, using static AccessKeys poses the following security risks:

  • Lack of expiration limits: Keys have long-term validity and no automatic expiration mechanism. If a key is leaked, an attacker can use it indefinitely.

  • Insecure storage methods: Keys are often hard-coded in code, configuration files, or container images. This makes them vulnerable to theft through methods such as code scanning or image analysis.

  • Overly broad permissions: The permissions associated with keys are often too broad. This makes it difficult to adhere to the principle of least privilege and increases the potential attack surface.

  • Lack of rotation mechanisms: Keys cannot be rotated automatically. The manual replacement process is complex and costly, which often results in outdated keys.

To address these problems, this solution integrates IDaaS EIAM with AI Gateway to enable dynamic rotation and centralized management of client credentials. The core benefits are:

  • Credential-free client access: Clients do not need to hold any long-term static keys. They only need to obtain a short-lived Access Token from IDaaS at runtime.

  • Unified authentication and authorization: All calls to backend AI services pass through AI Gateway. The gateway is responsible for unified JWT-based authorization.

  • Centralized management of downstream keys: The API keys for backend services, such as Alibaba Cloud Model Studio, are securely stored in AI Gateway. This process is completely transparent to the client and reduces the risk of key leakage.

Architecture

AI Gateway supports JWT-formatted tokens. This means clients do not need to store plaintext keys. Instead, they request an M2M token from IDaaS EIAM to access AI Gateway. The gateway then completes the authorization and calls the Alibaba Cloud Model Studio API. This process implements dynamic token authentication for the client and centralizes static credential access for downstream services at the gateway layer. This improves the security and flexibility of the overall architecture. The process is as follows:

image
  1. Client obtains an Access Token: The client requests an Access Token from the IDaaS token endpoint.

  2. Client sends a request to AI Gateway: The client includes the obtained Access Token in the Authorization header of the request and calls the AI Gateway API.

  3. AI Gateway performs local signature verification: AI Gateway obtains the public key of IDaaS from the JWKS URL and verifies the Access Token locally. This process does not require a request to IDaaS for every verification, which enables efficient authorization.

  4. Call the Model Studio service: After authorization is successful, AI Gateway uses the pre-configured Model Studio API key to call the backend model service and returns the result to the client.

Procedure

Step 1: Obtain a Model Studio API key

  1. Go to the Key Management page of Alibaba Cloud Model Studio (Singapore| Beijing).

  2. If you have an available API key, copy it from the list. To create a new API key, follow these steps. For more information, see Obtain an API key.

    1. On the API Key tab, click Create API Key. If you cannot create one, contact your organization or IT administrator.

    2. In the Create API Key dialog box, select the Owner Account and Owner Workspace for the API key, and click OK.

      • Owner Account: Select Alibaba Cloud account.

      • Owner Workspace: Select Default Workspace.

    3. Click the image icon next to the newly created API key to copy it.

Step 2: Configure AI Gateway to proxy Model Studio

Create an Internet NAT gateway

  1. Go to the Internet NAT Gateway purchase page, specify the following configurations, and then click Buy Now. For more information, see Create an Internet NAT gateway and associate an EIP.

    Configuration Item

    Description

    Example Value

    Billing Method

    Only Pay-as-you-go is supported.

    Pay-as-you-go

    Region

    Select the region where you want to create the Internet NAT gateway.

    China (Hangzhou)

    Network and Zone

    Select the VPC and vSwitch for the Internet NAT gateway. You cannot change this after creation.

    TEST_VPC

    Elastic IP Address

    Select an option based on whether you have already created an elastic IP address (EIP).

    Purchase New EIP

  2. Log on to the VPC console and switch to the destination region at the top of the page. In the left navigation bar, select NAT Gateway > Internet NAT Gateway.

  3. Locate the target instance and click its instance ID to open the details page.

  4. Choose the SNAT Management tab and click Create SNAT Entry.

  5. On the Create SNAT Entry page, set SNAT Entry Granularity to VPC, set Elastic IP Address to the currently associated EIP, and click OK.

Create an AI Gateway instance

  1. Go to the AI Gateway purchase page, specify the following configurations as needed, and then click Buy Now.

    Note

    The following configurations are for creating an instance with Network Access Type set to Public Network and the minimum Gateway Specifications. Keep the default values for any unmentioned configurations. For more information, see Create a gateway instance.

    Configuration Item

    Description

    Example Value

    Product Type

    Supports Dedicated Instance (Pay-as-you-go), Dedicated Instance (Subscription), and Serverless (Pay-as-you-go).

    Dedicated Instance (Pay-as-you-go)

    Region

    Select the destination region.

    Note

    Select the same region as the Internet NAT gateway instance created in the previous step.

    China (Hangzhou)

    Gateway Name

    Enter a custom name for the gateway.

    test_gateway

    Gateway Specifications

    Select the node specifications as needed. Serverless versions do not have gateway specifications.

    aigw.small.x1

    Network Access Type

    Supports Public Network, Private Network, and Public and Private Network access types.

    Public Network

    VPC

    Select the VPC where the gateway instance runs.

    Note

    Select the same VPC as the Internet NAT gateway instance created in the previous step.

    TEST_VPC

    Zone Selection

    Select Automatic Assignment or Manual Selection.

    • Automatic Assignment: Select a vSwitch, and the system automatically assigns two zones to deploy the gateway nodes.

    • Manual Selection: Manually select the zones and vSwitches to deploy the gateway nodes.

    Automatic Assignment

    vSwitch

    Select the vSwitch where the gateway instance runs.

    VSW_1

    Service-linked Role

    Click all Create Service-linked Role buttons to create the following two roles. For more information, see Service-linked Role.

    • AliyunServiceRoleForNativeApiGw

    • AliyunServiceRoleForNativeApiGwInvokeFC

    -

  2. On the Confirm Order page, review the AI Gateway configuration details and click Buy Now.

  3. Return to the AI Gateway Instances page. When the Status of the created gateway changes to Running (this may take 1 to 5 minutes), the gateway has been successfully created.

Create an AI Gateway service

  1. Log on to the AI Gateway console and switch to the destination region at the top. In the navigation pane on the left, choose AI Gateway > Instances.

  2. Locate the target instance and click its ID to open the instance details page.

  3. In the navigation pane on the left, choose Services and click Create Service. If the Configure Default Alerting panel appears, configure it as needed or ignore it.

  4. In the Create Service panel that appears, configure the following parameters and then click OK.

    Note

    The following configurations use Alibaba Cloud Model Studio as an example. Keep the default values for any unmentioned configurations. For more information, see Create a service.

    Configuration Item

    Description

    Example Value

    Service Source

    Select the type of service to proxy.

    AI Service

    Service Name

    Enter a custom service name.

    test_bailian

    Large Model Provider

    Select the large model provider to proxy.

    Alibaba Cloud Model Studio

    Model Protocol

    Specify the model protocol. Keep the default value.

    OpenAI/v1

    Endpoint (base_url)

    Set the endpoint provided by the large model provider. Keep the default value.

    https://dashscope.aliyuncs.com/compatible-mode/v1

    Generation Method

    Specify the generation method for the key required to access the large model.

    Manual Input

    API-KEY

    Set the credential required to access the large model.

    sk-4901******8a59

Create a Model API

  1. On the AI Gateway instance details page, choose Model API in the navigation pane on the left and click Create Model API.

  2. Select a scenario and click its Create button. In the panel that appears, specify the following parameters and click OK.

    Note

    The following configurations use the Text Generation scenario as an example. Keep the default values for any unmentioned configurations. For more information, see Manage Model APIs.

    Configuration Item

    Description

    Example Value

    Protocol

    Different protocols correspond to a set of built-in default route descriptions for that scenario. This is used to quickly generate compatible interfaces such as OpenAI, DashScope, and vLLM.

    OpenAI Compatible

    API Name

    Enter a custom API name. It must be globally unique within your account.

    test_api

    Domain Name

    • The domain name to access this API. You can select multiple domain names.

    • The combination of the domain name and the base path BasePath must be unique.

    • To add a new domain name, click Add Domain Name on the right to create a domain name.

    Note
    • If you set Network Access Type to Public Network or Public and Private Network when creating the AI Gateway instance, or if you configured a public endpoint after the instance was created, the instance can be accessed through the public endpoint.

    • The public endpoint provided by AI Gateway has a daily call limit of 1,000. It is for testing only and must not be used in a production environment.

    • To call AI Gateway through a specified domain name in a production environment, resolve this domain name to the public endpoint of the current AI Gateway instance using a CNAME record. The public endpoint is located under Overview > Endpoints > Endpoints and IPs on the AI Gateway instance details page.

    example.aliyun.com

    Service Type

    Supports Single Model Service, Multi-model Service (by Model Name), and Multi-model Service (by Weight).

    Single Model Service

    Service Name

    Select the target service.

    test_bailian

  3. On the API details page, choose the Consumer Authentication tab. Click Configuration Information > Edit. In the panel that appears, turn on the Enable switch, set Authentication Method to JWT, and click OK.

Step 3: Configure an IDaaS M2M application

Create an IDaaS instance

  1. Log on to the IDaaS Management Console and switch to the destination region at the top. In the navigation pane on the left, choose EIAM.

  2. In the area on the right, click IDaaS > Create Instance. In the panel that appears, set the Description, select the Alibaba Cloud Product Service Agreement checkbox, and then click Create.

  3. In the EIAM instance list, locate the target instance and click Manage in the Actions column to open the management backend for the instance.

  4. In the navigation pane on the left of the EIAM instance management backend, choose Application Management > M2M Applications. In the Note on Upgrade dialog box, click Upgrade Now.

  5. On the upgrade page, enable M2M Management, configure other options as needed, and then click Buy Now.

Create an M2M Client application

  1. In the navigation pane on the left of the EIAM instance management backend, choose Application Management > M2M Applications.

  2. Click Add Application, set Application Name to "M2M Client", and click Add to open the application details page.

  3. On the General tab, click Credential Management > Client Secret Credential > Add client_secret. Select a Client Secret Validity Period and click Confirm.

  4. Click View next to client_secret. Copy and save the values of client_id and client_secret for later use.

  5. Copy and save the URL from Application Settings > Token Endpoint for later use.

  6. On the Client Permission Management tab, turn on the Custom Permissions switch.

Create an M2M Server application

  1. In the navigation pane on the left of the EIAM instance management backend, choose Application Management > M2M Applications.

  2. Click Add Application, set Application Name to "M2M Server", and click Add to open the application details page.

  3. On the Server Permission Control tab, under Authorized application, turn on the Server Permission Control switch. In the panel that appears, set the ResourceServer Identifier and click Confirm.

    Note

    Set ResourceServer Identifier to cloud:idaas:aigateway:alibabacloud:<account-id>. Replace <account-id> with your current Alibaba Cloud account ID.

  4. Click Create Scope under Permission Management. Set the Scope Name and Scope Value, and then click Confirm.

  5. Turn on the M2M Client switch under Authorized Applications. In the Permission list, select the permission you added in the previous step and click Confirm.

  6. On the General tab, copy and save the URL from Application Settings > Public Key Endpoint for later use.

Step 4: Configure AI Gateway JWT authentication

Create a consumer and configure JWT authentication

  1. Log on to the AI Gateway console and select the destination region in the top menu bar. In the navigation pane on the left, click AI Gateway > Consumers.

  2. On the Consumers list page, click Create Consumer in the upper-left corner.

  3. On the Create Consumer page, specify the following parameters and click Create. For more information, see Create a consumer.

    Configuration Item

    Description

    Example Value

    Consumer Name

    Enter a custom name for the consumer.

    test_consumer

    Status

    The consumer status can be Enabled or Disabled. You can manually enable or disable the consumer after it is created.

    Enabled

    Authentication Method

    Set the authentication method supported by the consumer: API Key, JWT, or HMAC. This example uses JWT.

    Configure other settings as needed or keep the default values.

    JWT

Grant API permissions to the consumer

  1. On the Consumers list page, click the name of the target consumer to open its details page, and then choose the Consumer Authorization tab.

  2. Choose the Model API tab and click Grant.

  3. In the authorization panel, select the Model API created in the previous steps and click OK. For more information, see Authorization management.

Step 5: Verify the configuration

Obtain an M2M token

Important

In a terminal that can access the internet, run the following curl command to obtain an Access Token using the client_secret_post method.

curl --location --request POST '<Token endpoint of the M2M Client application. Example: https://******.aliyunidaas.com/api/v2/iauths_system/oauth2/token>' \
     --header 'Content-Type: application/x-www-form-urlencoded' \
     --data-urlencode 'grant_type=client_credentials' \
     --data-urlencode 'client_id=<client_id of the M2M Client application. Example: app_m5doo******>' \
     --data-urlencode 'client_secret=<client_secret of the M2M Client application. Example: CS5v3F******>' \
     --data-urlencode 'scope=<Audience of the M2M Server application. Example: cloud:idaas:aigateway:alibabacloud:******>|<Permission identifier of the M2M Server application>'
Note

The scope parameter must include both the ResourceServer Identifier and the Scope Value, separated by a VERTICAL LINE (|).

A successful request returns JSON-formatted data similar to the following. Copy the value of access_token, which is the M2M token.

{
  "token_type": "Bearer",
  "access_token": "eyJraWQi******Nsn2u7Jcw",
  "expires_in": 3600,
  "expires_at": 1765448721
}

Call AI Gateway using the M2M token

Go to the AI Gateway page, locate the target instance, and click its ID to open the instance details page.

Verify using the AI Gateway debug feature

  1. In the navigation pane on the left, choose Model API. Click the name of the Model API that you created in the previous steps to open the API details page.

  2. In the upper-right corner, click Debug. In the dialog box that appears, configure the following parameters:

    • Domain Name: Select a public endpoint, such as env-d4t******kv0-cn-hangzhou.alicloudapi.com.

    • Custom Parameters: Under Header, click Add Parameter to add an Authorization parameter.

      • Key input box: Authorization.

      • Value input box: Bearer <M2M Token>.

        Important
        • There must be exactly one space between Bearer and <M2M Token>. Replace <M2M Token> with the access_token obtained in the previous step.

        • Construct the complete string in a local text editor to ensure that the format is correct before you paste it into the Value field.

    • Prompt: The prompt to send to the large language model.

  3. In the upper-right corner, click Send Request. You can view the result in the Model Response section below.

Verify using code

In a terminal that can access the internet, use the curl command or integrate it into your code to verify the final call chain.

Placeholder descriptions:

  • <Domain name>: Replace this with the Public Endpoint from the Overview > Endpoints > Endpoints and IPs section of the AI Gateway instance details page.

    Note
    • The default public endpoint provided by AI Gateway has a daily call limit of 1,000. It is for testing only. Do not use it in a production environment.

    • For production environments, resolve your business domain name to this public endpoint using a CNAME record and initiate calls through your business domain name.

  • <M2M Token>: Replace this with the access_token obtained in the previous step.

  • <prompt>: Replace this with the prompt to send to the large language model.

Important

There must be exactly one space between Bearer and <M2M Token>. Replace <M2M Token> with the access_token obtained in the previous step.

Call using curl
curl --location '<Domain name. Example: http://env-d4t6******kv0-cn-hangzhou.alicloudapi.com>/v1/chat/completions' \
   --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <M2M Token>' \
  --data '{
    "model": "qwen-max",
    "stream": true,
    "max_tokens": 1024,
    "top_p": 0.95,
    "temperature": 1,
    "messages": [
        {
            "role": "user",
            "content": "<prompt>"
        }
    ]
}'
Call using Python code
from openai import OpenAI

# Configure
API_BASE_URL = "<Domain name. Example: http://env-d4t6******kv0-cn-hangzhou.alicloudapi.com>/v1"
AUTH_TOKEN = "Bearer <M2M Token>"

# Initialize the client
client = OpenAI(
    base_url=API_BASE_URL,
    api_key="unset",
    default_headers={"Authorization": AUTH_TOKEN}
)

# Send the request and process the streaming response
stream = client.chat.completions.create(
    model="qwen-max",
    messages=[{"role": "user", "content": "<prompt>"}],
    stream=True,
    max_tokens=1024,
    top_p=0.95,
    temperature=1
)

for chunk in stream:
    # Check if the choices list is not empty before accessing its content
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Going live

  • Obtaining M2M tokens

    We recommend using federated credential authentication methods, such as OIDC federated authentication, PKCS#7 signature authentication, and PrivateCA certificate authentication.

  • Token caching

    Cache the Access Token on the client and reuse it within its validity period (expires_in) to avoid requesting a new token from IDaaS for every call. This reduces latency and authentication overhead.

  • Principle of least privilege

    • Grant only the necessary model access permissions to an Alibaba Cloud Model Studio API key to execute tasks.

    • Limit permissions for an M2M client in IDaaS to the minimum set required to perform its intended functions.