Skip to main content

Machine-to-Machine (M2M) Access for Organization Units

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

If one service works for several customer or partner organizations, each request still needs to access data for the correct organization unit.

You could register a separate copy of the service in every organization unit. That works, but each copy comes with its own client ID and secret. As the number of organization units grows, so does the number of credentials you need to manage and rotate.

Instead, register the service once in the organization unit that owns it, then specify which other organization units it can act for. The service keeps one client ID and secret and requests a token for the organization unit it is acting for.

The resource server uses the organization unit in that access token to scope the request. If the token does not contain the expected organization unit, the service cannot access that organization unit's data.

Prerequisites​

  • Choreo Identity & Access Management running locally. See Get Choreo Identity & Access Management.
  • An application that uses the client_credentials grant. See Client Credentials.
  • The organization units the application will act for. See Manage Organization Units.

How It Works​

The application belongs to one organization unit. A sharing policy names the others it may act for, and the token endpoint takes the chosen one in its path.

note

When the application asks for the organization claims, ouId, ouName, and ouHandle, they come from the organization unit the request named rather than the one that owns the application. An application that asks for none receives a token carrying none. See Emit the Organization Unit in the Token.

Grant an Application Access to Organization Units​

Declare a sharing policy in one of two places:

  • The application's resource file, shown below.
  • Console, on the application's Organization Access tab. The Console cannot create a policy that reaches every organization unit in the deployment or the root of every other tree, because only a resource file or an import can set those. A policy that the application's resource file declares is read-only in the Console.

Add a sharingPolicies list to the application document:

applications/billing-service.yaml
resource_type: application
id: billing-service
name: Billing Service
ouId: acme-root
type: m2m
sharingPolicies:
- id: billing-service-all-customers
childUnits:
mode: all

This policy lets the application act for every organization unit beneath acme-root, at any depth, including organization units created later.

A policy can also reach every organization unit in the deployment, named organization units only, or the root organization units of other trees, and it can withhold organization units from a broad grant. See Resource Sharing Across Organization Units for every section, mode, and exclusion rule.

The owning organization unit needs no sharing policy. A policy only extends access to the others.

Emit the Organization Unit in the Token​

By default, an access token does not name the organization unit it was issued for. Add the organization claims to the application's access token configuration, so the resource server receiving the token knows which organization unit the service is acting for:

applications/billing-service.yaml
inboundAuthConfig:
- type: oauth2
config:
clientId: billing-service-client
grantTypes:
- client_credentials
token:
accessToken:
clientConfig:
attributes:
- ouId
- ouName
- ouHandle
Deploy your changes

Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.

Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.

Request a Token for an Organization Unit​

Name the organization unit by its id in the token endpoint's path. The path does not resolve handles:

curl -X POST https://<DATA_PLANE_URL>/ou/019f8c21-4b7e-7a39-b5d0-6e2c91f4a8b7/oauth2/token \
-u 'billing-service-client:<client-secret>' \
-d 'grant_type=client_credentials'

The bare /oauth2/token request without the ou prefix issues a token for the application's own organization unit.

The organization claims resolve to the organization unit named in the path:

{
"sub": "billing-service",
"ouId": "019f8c21-4b7e-7a39-b5d0-6e2c91f4a8b7",
"ouName": "Acme Customer A",
"ouHandle": "acme-customer-a",
"aud": "https://api.example.com"
}

Refusals​

ResponseCause
401 invalid_clientThe client credentials are wrong. Credentials are checked before anything about the organization unit, so this answer says nothing about which organization units exist.
400 unauthorized_clientThe organization unit does not exist, or no policy lets this application act for it. The two answer identically, so a caller cannot use the endpoint to discover which organization units exist.
400 invalid_requestA grant other than client_credentials was sent to the /ou/{ouId} path. Only client_credentials acts for an organization unit, apart from the device secret exchange in Sign-In With a Device Secret.

Was this page helpful?