Skip to main content

One organization for each customer

Give each business customer its own members, roles and invitations inside your product.

RecipeReadyView source
Works with
Plain HTTPJavaScript / TypeScriptlumo CLIExpress

What it shows​

  1. A company signs up for your product. Your backend creates a customer organization for it, here Northwind Labs.
  2. The person who signed up has an account already. Your backend adds that account as the owner.
  3. Your backend invites a colleague by email as an administrator.
  4. A member signs in, and the access token names the customer organization and the role of the member.
  5. Your API reads these claims, and serves each customer only its own data.

A customer organization is a group of users inside your LumoAuth organization. All customer organizations share the sign-in methods, the sign-in page and the signing keys of your organization.

Before you start​

  • An API key with the scopes admin:users:read, admin:users:write and admin:roles:read. Create it in the portal under Applications & developer tools, API keys.
  • Check that your plan includes the Admin API in the plans matrix. The plan also sets how many customer organizations you can have.
  • The account of the person who signed up. The setup script creates it.
  • The shell scripts need jq. The script for the CLI needs the lumo CLI.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYAn API key with the scopes admin:users:read, admin:users:write and admin:roles:read.
SIGNUP_USER_IDThe ID of the account that becomes the owner of the customer organization. The setup script prints it.
LUMOAUTH_CLIENT_IDOnly for js/api.mjs: the client ID of the application that your customers sign in to.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node b2b/customer-organizations/setup.mjs
setup.mjs
// Creates what the recipe needs: the account of the person who signs up for
// Northwind Labs. Run it again at any time.
//
// node b2b/customer-organizations/setup.mjs
//
// The API key needs the scope admin:users:write.
import { randomBytes } from 'node:crypto';
import { ensure, printEnv } from '../../_lib/admin.mjs';

const email = 'sam.rivera@northwind.example.com';
const user = await ensure('/admin/users', (u) => u.email === email, {
email,
name: 'Sam Rivera',
// In your product the person chooses the password at sign-up.
password: `${randomBytes(18).toString('base64url')}#Aa1`,
emailVerified: true,
});

console.log(`The account ${email} is ready.`);
printEnv({ SIGNUP_USER_ID: user.id });

Code​

Each program creates the organization, adds the owner and sends the invitation. The JavaScript tab has a second file, js/api.mjs: an API that reads the claims of the token.

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. js/api.mjs was run too: it answered a member of Northwind Labs, and it refused the same token for another customer.

b2b/customer-organizations/http/customer.sh
#!/bin/sh
# Create a customer organization, make the person who signed up its owner,
# and invite a colleague as administrator, with curl and jq.
#
# sh b2b/customer-organizations/http/customer.sh
set -eu

SLUG="${CUSTOMER_SLUG:-northwind-labs}"
COLLEAGUE="${COLLEAGUE_EMAIL:-maya.chen@northwind.example.com}"
ORGANIZATIONS="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1/admin/organizations"

api() {
curl --silent --show-error --request "$1" "$ORGANIZATIONS$2" \
--header "X-API-Key: $LUMOAUTH_API_KEY" \
--header "Content-Type: application/json" \
${3:+--data "$3"}
}

# A second run answers 409, because the slug is taken. The next call reads the
# organization in both cases. The slug works in place of the ID in every path.
api POST "" "{\"name\": \"Northwind Labs\", \"slug\": \"$SLUG\"}" > /dev/null
ID=$(api GET "/$SLUG" | jq -r .data.id)
echo "organization: $SLUG ($ID)"

# Every customer organization starts with the roles owner, admin and member.
ROLES=$(api GET "/$SLUG/roles")
OWNER_ROLE=$(printf '%s\n' "$ROLES" | jq '.data[] | select(.slug == "owner") | .id')
ADMIN_ROLE=$(printf '%s\n' "$ROLES" | jq '.data[] | select(.slug == "admin") | .id')

# The person who signed up has an account already, so no invitation is needed.
# A second run answers 409, because the person is a member.
api POST "/$SLUG/members" "{\"userId\": \"$SIGNUP_USER_ID\", \"roleId\": $OWNER_ROLE}" > /dev/null

# The colleague gets an email with a link that works for 7 days. A second run
# sends no second email: LumoAuth limits how often one address is invited.
PENDING=$(api GET "/$SLUG/invitations?status=pending" | jq --arg email "$COLLEAGUE" '[.data[] | select(.email == $email)] | length')
if [ "$PENDING" = 0 ]; then
api POST "/$SLUG/invitations" "{\"email\": \"$COLLEAGUE\", \"roleId\": $ADMIN_ROLE}" > /dev/null
fi

api GET "/$SLUG/members" | jq -r '.data[] | "member: \(.user.email) as \(.role.slug)"'
api GET "/$SLUG/invitations?status=pending" | jq -r '.data[] | "invited: \(.email) as \(.role.slug)"'
echo "portal: $LUMOAUTH_URL/b2b/$LUMOAUTH_ORG_ID/$SLUG/"
sh b2b/customer-organizations/http/customer.sh

Check the result​

Output
organization: northwind-labs (01a21fb8-7f58-72cf-b24f-d9a8fbc90258)
member: sam.rivera@northwind.example.com as owner
invited: maya.chen@northwind.example.com as admin
portal: https://app.lumoauth.dev/b2b/acme-corp/northwind-labs/
  • In the portal, open Identity, Customer organizations, then Northwind Labs. The Members tab shows the owner, and the Pending invitations tab shows the colleague.
  • Run a program again: the lines are the same, and no second email is sent.

To see the claims, sign in to your application as the owner. A member of one customer organization signs in through that one. A member of several chooses one on a selector. To skip the selector, your application adds the slug to the authorization request:

GET /orgs/acme-corp/api/v1/oauth/authorize?response_type=code&client_id=…&organization=northwind-labs&…

The access token then carries org_id, org_slug, org_name and org_role. Start js/api.mjs and call it with that token:

node b2b/customer-organizations/js/api.mjs
curl http://localhost:3001/customers/northwind-labs/invoices -H "Authorization: Bearer $ACCESS_TOKEN"
curl http://localhost:3001/customers/globex/invoices -H "Authorization: Bearer $ACCESS_TOKEN"

The first call answers with the invoices of Northwind Labs. The second call answers 403 with not_your_organization: a member of customer A cannot read the data of customer B.

To keep your own records in step, subscribe to the webhook events organization.created, organization.member.added and organization.invitation.accepted. See Receive webhooks.

Limits​

  • A customer organization has no SSO connection, SCIM endpoint or sign-in page of its own.
  • An organization role is a name. LumoAuth attaches no permissions to it: your application gives each role its meaning.
  • The organization parameter is a hint. LumoAuth ignores a slug of an organization in which the person is not an active member.
  • An opaque access token carries no claims. An application that uses opaque tokens reads the organization from the ID token.
  • Tokens from the device flow and from CIBA carry no organization claims.
  • LumoAuth limits how often one address is invited in an hour, so the programs send the invitation once.