M2M OAuth injection

Updated at:

M2M (Machine to Machine) OAuth lets a backend access an OAuth service with the application's own identity, with no end user involved. The platform obtains and refreshes the real token automatically, and the gateway injects it when requests go out, so you send requests in the sandbox as usual.

Use cases

M2M OAuth applies to scenarios where no end user is involved and a backend accesses a downstream service with the application's own identity. The following are typical examples:

  • Access the DingTalk Open Platform with the identity of an enterprise internal application.

  • Access Microsoft Graph with the identity of a Microsoft Entra ID application.

  • Access enterprise APIs with an Auth0 M2M application.

  • Access internal services with the OAuth 2.0 client_credentials flow.

Read Credential injection overview first to understand the overall framework.

M2M OAuth does not involve user logon, authorization pages, or callbacks. It differs from an API key in that an M2M token has an expiration time and is refreshed by the platform automatically. Other than that, it is used in the same way as an API key: send requests in the sandbox as usual, and the gateway injects the real token when the requests go out. The only difference is the reference expression, and you might need to configure scopes or an audience.

If you complete the client_credentials flow on your own to obtain a token, use Inject your own credentials to inject the token directly. You must still manage token expiration and refresh on your own.

Prerequisites

Complete the following preparations in the Agent Identity console:

  1. Create a Workload Identity and associate it with an available Resource Access Management (RAM) role.

  2. Grant the agentidentitydata:GetResourceOAuth2Token permission to the RAM role.

  3. Create an OAuth credential provider, and configure the token endpoint, client ID, and client secret of the target service.

  4. Allow the Workload Identity to use the credential provider.

  5. Confirm the domain name and the header name of the target service, and check whether scopes or audience are required.

M2M cannot be used to access private user data, such as personal documents and mailboxes. Use User-delegated OAuth injection instead.

Configure the sandbox

The following DingTalk example assumes that the sandbox identity is named my-agent (the name must be the same as the Workload Identity that you created in Prerequisites), and injects the token of the credential provider named dingtalk-provider in Agent Identity into requests sent to api.dingtalk.com:

Node.js

import { Sandbox } from "e2b";

const sandbox = await Sandbox.create("code-interpreter-v1", {
  metadata: {
    "fc.sandbox.identity.name": "my-agent",
  },
  network: {
    allowOut: ["api.dingtalk.com"],
    denyOut: ["0.0.0.0/0"],
    rules: {
      "api.dingtalk.com": [{
        transform: {
          headers: {
            "x-acs-dingtalk-access-token":
              "${fc.sandbox.identity.oauth.m2m:dingtalk-provider}",
          },
        },
      }],
    },
  },
});

Python

from e2b import Sandbox

sandbox = Sandbox.create(
    template="code-interpreter-v1",
    metadata={"fc.sandbox.identity.name": "my-agent"},
    network={
        "allow_out": ["api.dingtalk.com"],
        "deny_out": ["0.0.0.0/0"],
        "rules": {
            "api.dingtalk.com": [{
                "transform": {
                    "headers": {
                        "x-acs-dingtalk-access-token":
                            "${fc.sandbox.identity.oauth.m2m:dingtalk-provider}"
                    }
                }
            }]
        },
    },
)

The reference expression is in the following format: ${fc.sandbox.identity.oauth.m2m:<PROVIDER-NAME>}. This expression explicitly selects the M2M flow and does not automatically switch to user-delegated OAuth injection. DingTalk enterprise internal applications typically do not require additional scopes.

Verify credential injection

Request the target service from inside the sandbox without carrying the authentication header:

curl -i https://api.dingtalk.com/v1.0/contact/departments/1

When the request goes out, the gateway injects x-acs-dingtalk-access-token: <REAL-TOKEN>. If the SDK requires you to pass a token value, use a placeholder value. The gateway overwrites the corresponding header.

Configure scopes or audience

Some credential providers require the token request to carry scopes or audience. Submit these parameters by using the reserved key @fc.sandbox.identity, which is not a real domain name and is not sent as a header. It only carries the parameters used to obtain the token.

Microsoft Graph (requires scopes):

Node.js

const network = {
  allowOut: ["graph.microsoft.com"],
  denyOut: ["0.0.0.0/0"],
  rules: {
    "@fc.sandbox.identity": [{
      transform: { headers: {
        "fc.sandbox.identity.oauth.m2m.options": JSON.stringify({
          "microsoft-graph": { scopes: ["https://graph.microsoft.com/.default"] },
        }),
      } },
    }],
    "graph.microsoft.com": [{
      transform: { headers: {
        Authorization: "Bearer ${fc.sandbox.identity.oauth.m2m:microsoft-graph}",
      } },
    }],
  },
};

Python

import json

network = {
    "allow_out": ["graph.microsoft.com"],
    "deny_out": ["0.0.0.0/0"],
    "rules": {
        "@fc.sandbox.identity": [{
            "transform": {"headers": {
                "fc.sandbox.identity.oauth.m2m.options": json.dumps({
                    "microsoft-graph": {
                        "scopes": ["https://graph.microsoft.com/.default"]
                    }
                })
            }}
        }],
        "graph.microsoft.com": [{
            "transform": {"headers": {
                "Authorization":
                    "Bearer ${fc.sandbox.identity.oauth.m2m:microsoft-graph}"
            }}
        }]
    },
}

Auth0 (requires audience): replace the options value above with the following code, and replace the domain name in rules and the provider name in the reference expression accordingly:

Node.js

const m2mOptions = JSON.stringify({
  "company-auth0": { customParameters: { audience: "https://api.example.com" } },
});

Python

import json

m2m_options = json.dumps({
    "company-auth0": {
        "customParameters": {"audience": "https://api.example.com"}
    }
})

The options key is the provider name, which must exactly match the name in the reference expression (case-sensitive).

Update the configuration

While the sandbox is running, use updateNetwork to change the provider, the injection rules, or the scopes or audience. For the common operations, see Update network configuration.

M2M options are processed on a per-field basis: if you omit the field, the existing configuration is retained. If you submit the field, the entire M2M provider table is replaced; if you submit an empty object {}, all configurations are cleared. Therefore, to retain other providers, submit them together.

Limits

For the common limits and security requirements, see Credential usage limits.