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:
Client obtains an Access Token: The client requests an Access Token from the IDaaS token endpoint.
Client sends a request to AI Gateway: The client includes the obtained Access Token in the
Authorizationheader of the request and calls the AI Gateway API.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.
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
Go to the Key Management page of Alibaba Cloud Model Studio (Singapore| Beijing).
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.
On the API Key tab, click Create API Key. If you cannot create one, contact your organization or IT administrator.
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.
Click the
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
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
Log on to the VPC console and switch to the destination region at the top of the page. In the left navigation bar, select .
Locate the target instance and click its instance ID to open the details page.
Choose the SNAT Management tab and click Create SNAT Entry.
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
Go to the AI Gateway purchase page, specify the following configurations as needed, and then click Buy Now.
NoteThe 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.
NoteSelect 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.
NoteSelect 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
-
On the Confirm Order page, review the AI Gateway configuration details and click Buy Now.
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
Log on to the AI Gateway console and switch to the destination region at the top. In the navigation pane on the left, choose .
Locate the target instance and click its ID to open the instance details page.
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.
In the Create Service panel that appears, configure the following parameters and then click OK.
NoteThe 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
On the AI Gateway instance details page, choose Model API in the navigation pane on the left and click Create Model API.
Select a scenario and click its Create button. In the panel that appears, specify the following parameters and click OK.
NoteThe 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
BasePathmust be unique.To add a new domain name, click Add Domain Name on the right to create a domain name.
NoteIf 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 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
On the API details page, choose the Consumer Authentication tab. Click . 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
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.
In the area on the right, click . In the panel that appears, set the Description, select the Alibaba Cloud Product Service Agreement checkbox, and then click Create.
In the EIAM instance list, locate the target instance and click Manage in the Actions column to open the management backend for the instance.
In the navigation pane on the left of the EIAM instance management backend, choose . In the Note on Upgrade dialog box, click Upgrade Now.
On the upgrade page, enable M2M Management, configure other options as needed, and then click Buy Now.
Create an M2M Client application
In the navigation pane on the left of the EIAM instance management backend, choose .
Click Add Application, set Application Name to "M2M Client", and click Add to open the application details page.
On the General tab, click . Select a Client Secret Validity Period and click Confirm.
Click View next to client_secret. Copy and save the values of client_id and client_secret for later use.
Copy and save the URL from for later use.
On the Client Permission Management tab, turn on the Custom Permissions switch.
Create an M2M Server application
In the navigation pane on the left of the EIAM instance management backend, choose .
Click Add Application, set Application Name to "M2M Server", and click Add to open the application details page.
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.
NoteSet ResourceServer Identifier to
cloud:idaas:aigateway:alibabacloud:<account-id>. Replace<account-id>with your current Alibaba Cloud account ID.Click Create Scope under Permission Management. Set the Scope Name and Scope Value, and then click Confirm.
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.
On the General tab, copy and save the URL from for later use.
Step 4: Configure AI Gateway JWT authentication
Create a consumer and configure JWT authentication
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 .
On the Consumers list page, click Create Consumer in the upper-left corner.
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.
Creation Method: Remote Fetch.
JWKS-URL: Set this to the URL from the Signature Verification Public Key Endpoint section in Create an M2M Server application.
Consumer ID in JWKS Payload:
key:
scope.Value: Set this to the Permission Identifier from Create an M2M Server application.
Configure other settings as needed or keep the default values.
JWT
Grant API permissions to the consumer
On the Consumers list page, click the name of the target consumer to open its details page, and then choose the Consumer Authorization tab.
Choose the Model API tab and click Grant.
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
This example demonstrates the client_secret_post Authentication Method. For information about other authentication methods, see M2M Client Token call examples.
You can use federated authentication methods such as OpenID Connect (OIDC) federation, PKCS#7 signature authentication, or PrivateCA certificate authentication.
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>'<M2M client application's Token Endpoint>: Set this to the Token Endpoint URL that you saved in the Create an M2M Client application step.<M2M Client application client_id>: Set this to the client_id that you saved in the Create an M2M Client application step.<The client_secret for your M2M Client application>: Set this to the client_secret that you saved in the Create an M2M Client application step.<M2M Server application ResourceServer Identifier>: Set this to the ResourceServer Identifier that you configured in the Create an M2M Server application step.<M2M Server application Scope Value>: Set this to the Scope Value that you configured in the Create an M2M Server application step.
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
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.
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
Authorizationparameter.Key input box:
Authorization.Value input box:
Bearer <M2M Token>.ImportantThere must be exactly one space between
Bearerand<M2M Token>. Replace<M2M Token>with theaccess_tokenobtained 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.
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 section of the AI Gateway instance details page.NoteThe 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 theaccess_tokenobtained in the previous step.<prompt>: Replace this with the prompt to send to the large language model.
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.