Skip to content

Implement the authorization model

This guide shows how to configure Resource-Aware Authorization in Mosaic and how to request it on a token. For how the model works, see How authorization works.

Deprecation notice

If you already use tenant-wide resources, migrate them to the new authorization model. See Migrate from tenant-wide resources.

Applying the model has four stages:

  • Build the model: Create one inventory of resources, scopes, permissions, and roles for each Mosaic application. (Steps 1-3)
  • Assign authorization: Assign resources and scopes to clients, and assign roles to users. (Steps 4-5)
  • Authorize the request: The client requests a resource and scopes. Mosaic returns an access token that contains only the authorization granted for that request. (Step 6)
  • Enforce authorization: Your API validates the token and checks the audience, scope, and permissions before it performs an action. (Step 7)

Default authorization

When you create a Mosaic app, it has no access controls configured. You can sign users in before you create resources, permissions, or custom scopes. Mosaic still returns access tokens and these tokens include basic authorization:

  • aud is userid-api.
  • Token lifetimes come from the client's token timeout settings.
  • Reserved scopes that the client requests, such as openid, are included.

Before you start

If this is your first time integrating with Mosaic, create a Mosaic application in the Admin Portal as described here and add an OIDC-based User authentication client skipping resource and scope selection (see Configure OIDC clients), then implement a login for your app.

Step 1: Plan your strategy

Before you create anything in the Admin Portal, decide what the application needs to protect and who may do what.

  1. List the APIs that need their own token. Each API is a resource.
  2. List the actions a user may perform. An action that belongs to one API is a permission on that resource. An action that applies no matter which API is called is a global permission.
  3. List the privileges a client may request. Each privilege is a scope, either global or owned by one resource.
  4. Group the permissions into user roles.
  5. Decide which clients may request each resource, and which scopes each client may receive.

You can create resources and scopes and leave them unassigned but a client cannot request scopes and resources it doesn't have.

Example: an online store
ItemWhat you will create
ResourcesShopping cart (https://api.shop.example/cart), Payments (https://api.shop.example/payments), and Inventory (https://api.shop.example/inventory)
Global permissionbrowse-store, so a user can browse the catalog on any token
Global scopestore:browse, so the client can request catalog access with any resource
Shopping cart permissionscreate-cart, update-cart, delete-cart, checkout-cart
Shopping cart scopescart:read, cart:write, cart:checkout
Payments permissionsview-payment, approve-payment
Payments scopespayments:read, payments:approve
RolesShopper and Store manager
Storefront clientShopping cart and Payments, not Inventory. Scopes store:browse, cart:read, cart:write, cart:checkout, and payments:read

In this example, payments:approve stays off the storefront client. Inventory is created so a later request can ask for a resource the storefront is not assigned.

Step 2: Create resources, permissions, and scopes

Admin Portal
  1. In the Admin Portal, navigate to your Mosaic app > Access Control > Resources. Create one resource for each API you identified. When creating a resource, add the permissions and scopes that belong to that resource before you save. A permission or scope created here is owned by the resource. Under Session management, set how long access to this resource lasts–use a shorter access token for a more sensitive resource:

    • Access token expiration
    • Refresh token expiration. Won't apply if the authorization request includes two or more resources. In this case, refresh token expiration time derives from the client settings.
    • Session expiration, the maximum period of time the refresh token can be rotated for this resource. Won't apply if the authorization request includes two or more resources. In this case, maximum rotation time derives from the client settings.
  2. Create global permissions from Access Control > Permissions, and leave them unassigned to a resource. A global permission can appear on a token for any API.

  3. Create global scopes from Access Control > Scopes, and leave them unassigned to a resource. A client can request a global scope with any resource.

Example: an online store

Create the shopping cart resource:

FieldValue
Resource nameShopping cart
Resource URIhttps://api.shop.example/cart
Access token expiration3600 (1 hour)
Refresh token expiration1209600 (14 days)
Session expiration2592000 (30 days)
Permissionscreate-cart, update-cart, delete-cart, checkout-cart
Scopescart:read, cart:write, cart:checkout

Save the resource, then create the payments resource the same way:

FieldValue
Resource namePayments
Resource URIhttps://api.shop.example/payments
Access token expiration900 (15 minutes)
Refresh token expiration86400 (1 day)
Session expiration86400 (1 day)
Permissionsview-payment, approve-payment
Scopespayments:read, payments:approve

Create the inventory resource with no permissions or scopes. It won't be assigned to the storefront client.

FieldValue
Resource nameInventory
Resource URIhttps://api.shop.example/inventory
Access token expiration28800 (8 hours)
Refresh token expiration604800 (7 days)
Session expiration604800 (7 days)

Create browse-store from Access Control > Permissions.

Create store:browse from Access Control > Scopes.

Step 3: Define user roles

Admin Portal

Navigate to Access Control > Roles. Create each role and select permissions from the inventory. The role references permissions that already exist. A role can reference global permissions and resource-specific permissions.

Example: an online store
RolePermissions
Shopperbrowse-store, create-cart, update-cart, checkout-cart, view-payment
Store managerbrowse-store, create-cart, update-cart, delete-cart, checkout-cart, view-payment, approve-payment

Step 4: Assign resources and scopes to the client

Admin Portal
  1. In your Mosaic app settings, proceed to the Clients tab and expand settings for a client that represents your app (OIDC-based user authentication client).
  2. In Access Control, assign the resources that client may request, and assign the scopes it may request. A resource-specific scope can be assigned only when its resource is also assigned to the client. You do not have to assign every resource in the application.
Example: an online store

Open the storefront client. Assign the Shopping cart and Payments resources. Do not assign Inventory. Assign the scopes the client may request: store:browse, cart:read, cart:write, cart:checkout, and payments:read.

Leave payments:approve unassigned.

Step 5: Assign roles to users

Admin Portal

Assign each role to the users who should have it. A user access token includes the permissions from the roles assigned to that user, limited to the resource that was requested. The roles claim lists every role assigned to the user.

Example: an online store

Assign the Shopper role to each shopper, and the Store manager role to each store manager.

Step 6: Send authorization request

client

The authorization request is where the client states the access it wants. The client sends resource and scope to Mosaic together with the other parameters of the flow, completes the flow, and receives tokens. In the authorization code flow, that means exchanging the authorization code for tokens.

You can send resource and scope on these flows:

You can send resource on these flows utilizing Mosaic authentication APIs:

User access token

The example below uses the authorization code flow. SCOPE is the URL-encoded list of scopes, and RESOURCE_URI is the URL-encoded resource URI. Repeat resource to request more than one resource.

Mosaic checks each requested resource before the user logs in. If one is not assigned to the client, Mosaic redirects to the redirect URI with the invalid_target error.

https://api.transmitsecurity.io/cis/oidc/auth?
  client_id=CLIENT_ID&
  scope=openid%20SCOPE&
  response_type=code&
  redirect_uri=REDIRECT_URI&
  resource=RESOURCE_URI

If every requested resource is assigned, the token request to /oidc/token must specify a resource–pick one of the initially resources. If the initial request included one resource, it can be omitted at the token request. The aud always targets a single resource.

curl -i -X POST \
  https://api.transmitsecurity.io/oidc/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=authorization_code \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d code=CODE \
  -d redirect_uri=REDIRECT_URI \
  -d resource=RESOURCE_URI

Upon a code exchange (oidc/token), Mosaic returns a user access token that includes:

  • aud is the one resource picked on the token request.
  • scope lists the requested scopes Mosaic granted: reserved scopes such as openid, global scopes assigned to the client, and resource-specific scopes that belong to the requested resource and are assigned to the client. When the flow includes a consent screen, the user has to consent to granting these scopes as well.
  • permissions lists the global permissions on the user's roles, plus the resource-specific permissions on those roles that belong to the requested resource. Requested scopes do not filter permissions.
  • roles lists every role assigned to the user.

For first-party clients, Mosaic omits a scope that belongs to another resource, a scope that is not assigned to the client, and a permission the user's roles do not include for that resource. In case of third-party clients, Mosaic denies the request entirely if one of the requested scopes cannot be granted in a token or the user didn't consent to requested scopes. Step 7 describes how your API checks these claims.

Exchange the refresh token to obtain an access token for another resource from that authorization request. See Refresh app's access.

curl -i -X POST \
  https://api.transmitsecurity.io/oidc/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=refresh_token \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d refresh_token=REFRESH_TOKEN \
  -d resource=RESOURCE_URI
Example: an online store

A shopper signs in through the storefront. The authorization request asks for the shopping cart and inventory resources. The inventory resource is not assigned to the storefront.

https://api.transmitsecurity.io/cis/oidc/auth?
  client_id=CLIENT_ID&
  scope=openid%20store:browse%20cart:read&
  response_type=code&
  redirect_uri=REDIRECT_URI&
  resource=https%3A%2F%2Fapi.shop.example%2Fcart&
  resource=https%3A%2F%2Fapi.shop.example%2Finventory

Mosaic redirects to the redirect URI with invalid_target before the shopper logs in. No token is issued.

A later authorization request asks for the shopping cart and payments resources. Both are assigned to the storefront. The request includes store:browse, cart:read, cart:write, payments:read, and payments:approve. openid is included because this is a user login. offline_access is included so Mosaic can return a refresh token. It is a reserved scope, so Mosaic grants it without an assignment on the client. Set prompt to consent, which is required to obtain a refresh token.

https://api.transmitsecurity.io/cis/oidc/auth?
  client_id=CLIENT_ID&
  scope=openid%20offline_access%20store:browse%20cart:read%20cart:write%20payments:read%20payments:approve&
  response_type=code&
  prompt=consent&
  redirect_uri=REDIRECT_URI&
  resource=https%3A%2F%2Fapi.shop.example%2Fcart&
  resource=https%3A%2F%2Fapi.shop.example%2Fpayments

The code exchange at /oidc/token must pick one of those resources. This exchange picks the shopping cart resource:

curl -i -X POST \
  https://api.transmitsecurity.io/oidc/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=authorization_code \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d code=CODE \
  -d redirect_uri=REDIRECT_URI \
  -d resource=https://api.shop.example/cart

Mosaic returns a user access token that includes:

ClaimValue
audhttps://api.shop.example/cart
scopeopenid offline_access store:browse cart:read cart:write
permissionsbrowse-store, create-cart, update-cart, checkout-cart
rolesShopper

store:browse is included because it is a global scope assigned to the storefront and the client requested it. browse-store and checkout-cart are included from the Shopper role. checkout-cart was not sent in the request. Requested scopes do not filter permissions.

The token leaves out what this client or this shopper cannot have. payments:read and payments:approve are not in scope, because they belong to the payments resource, not the shopping cart resource that was granted. delete-cart, approve-payment, and view-payment are not in permissions. The cart API accepts this token. The payments API and the inventory API reject it.

The payments resource was also on the authorization request. Exchange the refresh token from this login to obtain an access token for that resource:

curl -i -X POST \
  https://api.transmitsecurity.io/oidc/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=refresh_token \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d resource=https://api.shop.example/payments \
  -d refresh_token=REFRESH_TOKEN

Mosaic returns an access token for the payments resource:

ClaimValue
audhttps://api.shop.example/payments
scopeopenid offline_access store:browse payments:read
permissionsbrowse-store, view-payment
rolesShopper

payments:read is included because it belongs to the payments resource, it is assigned to the storefront, and the authorization request included it. payments:approve is not in scope, because it is not assigned to the storefront client. The cart scopes and cart permissions are absent because they belong to the shopping cart resource. The payments API accepts this token. The cart API and the inventory API reject it.

The same shopper can also start a new authorization request that asks only for the payments resource, with the scopes store:browse, payments:read, cart:read, and payments:approve:

https://api.transmitsecurity.io/cis/oidc/auth?
  client_id=CLIENT_ID&
  scope=openid%20store:browse%20payments:read%20cart:read%20payments:approve&
  response_type=code&
  redirect_uri=REDIRECT_URI&
  resource=https%3A%2F%2Fapi.shop.example%2Fpayments

Mosaic returns:

ClaimValue
audhttps://api.shop.example/payments
scopeopenid store:browse payments:read
permissionsbrowse-store, view-payment
rolesShopper

cart:read is not in scope, because it belongs to the shopping cart resource. payments:approve is not in scope, because it is not assigned the storefront client. The cart permissions are absent because they belong to the shopping cart resource. The payments API accepts this token. The cart API and the inventory API reject it.

Client access token

Client access tokens, such as tokens from the client credentials grant, follow the same audience and scope rules but exclude permissions or roles, because those come from roles assigned to a user.

A client credentials request sends resource and scope to /oidc/token. SCOPE is the space-separated list of scopes, and RESOURCE_URI is the resource URI. A request to /oidc/token specifies one resource value. There is no authorization code to exchange.

curl -i -X POST \
  https://api.transmitsecurity.io/oidc/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=client_credentials \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d resource=RESOURCE_URI \
  -d scope=SCOPE
Example: an online store

A client credentials request for the storefront asks for the shopping cart resource and the scopes store:browse, cart:read, and payments:approve:

curl -i -X POST \
  https://api.transmitsecurity.io/oidc/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=client_credentials \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d resource=https://api.shop.example/cart \
  -d scope='store:browse cart:read payments:approve'

Mosaic returns a client access token that includes:

ClaimValue
audhttps://api.shop.example/cart
scopestore:browse cart:read

payments:approve is not in scope, because it belongs to the payments resource. The token does not include permissions or roles. The cart API accepts this token. The payments API and the inventory API reject it.

Step 7: Enforce authorization

Backend

Validate the access token before you use it. After validation succeeds, your API decides whether this call is allowed.

On each call, check all of the following. Reject the call if any check fails:

  • aud is this API's resource URI. Reject a token issued for another resource.
  • scope includes the scope for this operation. A scope says the client may call the operation.
  • permissions includes the permission for this action. A permission says this user may perform the action. A requested scope does not replace this check.

Do not authorize from roles alone. That claim lists every role assigned to the user, including roles that reference other resources. Use permissions for the decision.

A client access token has no user. Check aud and scope only.

Example: an online store

Starting checkout on the cart API requires:

  • aud of https://api.shop.example/cart
  • scope includes cart:checkout
  • permissions includes checkout-cart

A cart token is rejected by the payments API and the inventory API, because aud is not their resource URI. A payments token that includes view-payment can view a payment. It cannot approve one unless permissions also includes approve-payment.

Migrate from tenant-wide resources

Deprecation notice

Tenant-wide resources and the APIs that manage them are being deprecated. Create resources in the Mosaic applications, and verify your integrations use a new model.

For your convenience, Mosaic has automatically migrated tenant-wide resources to the application level, with the resource naming preserved. In the Admin Portal, navigate to the Mosaic apps > Access Control > Resources to confirm required resources are present. If an app-level resource is missing:

  1. Create an application resource for each tenant-wide resource your integration still uses. See Step 2.
  2. Assign each new resource to the clients that should receive tokens for it. See Step 4.
  3. Update authorization requests to send the new resource URI as resource. See Step 6.
  4. Update Mosaic API calls that pass resource IDs. Send the ID of the application resource, not the tenant-wide resource. For example, Create client takes those IDs in resources.