Environment preparation
Before you use the IDaaS TypeScript SDK to obtain an M2M client token, prepare the environment as described in this topic. The same steps apply to JavaScript applications.
Environment requirements
-
Node.js 20, 22, or 24 is installed. TypeScript applications require TypeScript 4.7 or later.
-
The SDK can be installed from npm, and the runtime can reach the IDaaS token endpoint of your instance.
-
An M2M client application is created, its credentials or federated trust are configured, and the target audience and permissions are granted.
The SDK is designed for Node.js server-side use. It does not support browser environments or interactive user sign-in.
Install the SDK
Core SDK
Source code: idaas-typescript-core-sdk. Versions: npm. The following example uses 0.1.0-beta.2. Run this command in the directory of an existing Node.js project:
npm install --save-exact --registry=https://registry.npmjs.org/ @cloud-idaas/core-sdk@0.1.0-beta.2
TypeScript and ESM use import, and CommonJS uses require. For complete examples, see Code integration.
Alibaba Cloud authentication plugin
To call EIAM OpenAPI with Alibaba Cloud credentials and obtain an M2M client token, also install the authentication plugin.
Source code: idaas-typescript-core-alibabacloud-authentication-plugin. Versions: npm.
npm install --save-exact --registry=https://registry.npmjs.org/ @cloud-idaas/core-alibabacloud-authentication-plugin@0.1.0-beta.2
Register the plugin explicitly before the factory is initialized. For the registration method, see the Alibaba Cloud OpenAPI authentication example in Code integration.
Configuration file path
The default path of the configuration file is ~/.cloud_idaas/client-config.json. You can specify a different path by using an environment variable:
export CLOUD_IDAAS_CONFIG_PATH=/absolute/path/to/client-config.json
You can also pass a file path when you initialize the factory. The file is selected in the following order of precedence:
-
The path that is explicitly passed to
init(). -
The
CLOUD_IDAAS_CONFIG_PATHenvironment variable. -
The default path
~/.cloud_idaas/client-config.json.
If the selected file cannot be read or its JSON cannot be parsed, an error is returned and no other path is tried. You can also pass a configuration object directly by calling init(configObject). The SDK does not populate regular configuration fields from individual environment variables.
Configuration file
Replace the instance, client application, endpoints, audience, and permissions in the following example with your actual values. openApiEndpoint is required only for plugin authentication, and developerApiEndpoint is the optional Developer API endpoint.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"openApiEndpoint": "eiam.cn-hangzhou.aliyuncs.com",
"developerApiEndpoint": "eiam-developerapi.cn-hangzhou.aliyuncs.com",
"authnConfiguration": {
"authnMethod": "CLIENT_SECRET_POST",
"identityType": "CLIENT",
"clientSecretEnvVarName": "IDAAS_CLIENT_SECRET"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
Parameters
|
Field |
Description |
|
idaasInstanceId |
Required. The ID of the IDaaS EIAM instance. |
|
clientId |
Required. The ID of the M2M client application. |
|
issuer |
Required. The actual issuer endpoint copied from the M2M application configuration of the target instance. |
|
tokenEndpoint |
Required. The complete token endpoint URL, including the protocol and path. |
|
scope |
Required. The audience and permission of the target server application, in the format |
|
openApiEndpoint |
Required for plugin authentication. The EIAM OpenAPI endpoint of the region where the target instance is deployed. |
|
developerApiEndpoint |
Optional. The EIAM Developer API endpoint of the target region. |
|
authnConfiguration |
Required. The authentication method and the configuration that the method requires. See the following table. |
|
authnConfiguration.identityType |
Optional. Only |
|
httpConfiguration.connectTimeout |
Optional. The connection timeout. Default value: 5000. Unit: milliseconds. The value is an integer from 2000 to 60000. |
|
httpConfiguration.readTimeout |
Optional. The read timeout. Default value: 10000. Unit: milliseconds. The value is an integer from 2000 to 60000. |
|
httpConfiguration.userAgent |
Optional. A custom request identifier. The value is 1 to 512 printable ASCII characters in length. |
|
failureMode |
Optional. The default value |
The Hangzhou endpoints in the example are for format reference only. Use the region of your actual instance. The configuration file stores the names of the environment variables that hold credentials, and the runtime injects the actual secrets. For example, when you use CLIENT_SECRET_POST, inject the actual client secret into IDAAS_CLIENT_SECRET.
Relationship between authnMethod values and authnConfiguration fields
|
authnMethod |
Required authnConfiguration fields |
Description |
|
CLIENT_SECRET_BASIC |
clientSecretEnvVarName |
The client secret is sent over HTTP Basic authentication. |
|
CLIENT_SECRET_POST |
clientSecretEnvVarName |
The client secret is sent in a form body. |
|
CLIENT_SECRET_JWT |
clientSecretEnvVarName |
The client secret is used for HS256 signing. |
|
PRIVATE_KEY_JWT |
privateKeyEnvVarName |
A PEM private key is used for signing. |
|
PCA |
privateKeyEnvVarName, clientX509Certificate, x509CertChains, applicationFederatedCredentialName |
Private CA certificate authentication. |
|
PKCS7 |
applicationFederatedCredentialName, clientDeployEnvironment |
PKCS#7 federated authentication. |
|
OIDC |
applicationFederatedCredentialName, clientDeployEnvironment |
OIDC federated authentication. |
|
PLUGIN |
pluginName |
Uses a plugin that is registered before initialization. The top-level |
Configuration parameter examples
Select an authentication method, save the corresponding complete JSON as client-config.json, and replace the placeholder values.
Client secret credential configuration example
The following example uses CLIENT_SECRET_POST. You can change it to CLIENT_SECRET_BASIC or CLIENT_SECRET_JWT based on your actual authentication configuration.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"authnConfiguration": {
"authnMethod": "CLIENT_SECRET_POST",
"identityType": "CLIENT",
"clientSecretEnvVarName": "IDAAS_CLIENT_SECRET"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
Public and private key credential configuration example
The value of IDAAS_PRIVATE_KEY is the unencrypted PEM private key content, not a file path. RSA keys must be at least 2048 bits and use RS256. P-256, P-384, and P-521 use ES256, ES384, and ES512 respectively.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"authnConfiguration": {
"authnMethod": "PRIVATE_KEY_JWT",
"identityType": "CLIENT",
"privateKeyEnvVarName": "IDAAS_PRIVATE_KEY"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
Alibaba Cloud ECS configuration example (PKCS7 federated authentication)
Configure the IDaaS federated trust first. The runtime must be able to access ECS IMDSv2. For AWS EC2, use the AWS_EC2 deployment environment.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"authnConfiguration": {
"authnMethod": "PKCS7",
"identityType": "CLIENT",
"applicationFederatedCredentialName": "your_pkcs7_federated_credential",
"clientDeployEnvironment": "ALIBABA_CLOUD_ECS"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
Kubernetes OIDC configuration example
Configure the IDaaS OIDC federated trust first. The built-in file source supports Kubernetes, ACK, and EKS. When no path is specified, the ServiceAccount token file is read by default. You can also specify a path by using oidcTokenFilePath or oidcTokenFilePathEnvVarName.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"authnConfiguration": {
"authnMethod": "OIDC",
"identityType": "CLIENT",
"applicationFederatedCredentialName": "your_oidc_federated_credential",
"clientDeployEnvironment": "KUBERNETES",
"oidcTokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
PCA certificate credential configuration example
The private key and the leaf certificate must match. x509CertChains is a single string that contains the complete PEM blocks of 1 to 5 intermediate CA certificates. Concatenate the blocks directly or separate them with line breaks. Do not use commas. The certificate bodies in the following example are placeholders and must be replaced with real certificates. The server-side trust chain must also be configured.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"authnConfiguration": {
"authnMethod": "PCA",
"identityType": "CLIENT",
"applicationFederatedCredentialName": "your_pca_federated_credential",
"privateKeyEnvVarName": "IDAAS_PRIVATE_KEY",
"clientX509Certificate": "-----BEGIN CERTIFICATE-----\nREPLACE_WITH_LEAF_BASE64\n-----END CERTIFICATE-----",
"x509CertChains": "-----BEGIN CERTIFICATE-----\nREPLACE_WITH_INTERMEDIATE_CA_BASE64\n-----END CERTIFICATE-----"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
Plugin authentication configuration example (Alibaba Cloud OpenAPI authentication)
To obtain an M2M client token with Alibaba Cloud credentials, install and register the Alibaba Cloud authentication plugin, and grant the runtime identity the eiam:GenerateOauthToken permission on the target client application. For role and policy configuration, see Alibaba Cloud OpenAPI authentication.
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer": "https://your-issuer.example/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://your-issuer.example/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"openApiEndpoint": "eiam.cn-hangzhou.aliyuncs.com",
"authnConfiguration": {
"authnMethod": "PLUGIN",
"identityType": "CLIENT",
"pluginName": "alibabacloudPluginCredentialProvider"
},
"httpConfiguration": {
"connectTimeout": 5000,
"readTimeout": 10000
}
}
A single plugin request can contain only one audience. Separate multiple permissions for the same audience with spaces, and write the complete audience and permission in each entry. After the configuration is complete, register the plugin by following the example in Code integration.