> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fga.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# B2B via Connections

> Sync external B2B customers into FGA using enterprise connections and SCIM groups, without Auth0 Organizations.

Use this when each external customer maps to its own enterprise connection (a SAML or OIDC connection to their IdP, for example) instead of an Auth0 Organization. This example grants membership through SCIM groups belonging to that connection. It does **not** grant a user membership merely because they authenticated through the connection. This is an alternative to [B2B via Organizations](https://docs.fga.dev/relationship-sync/use-cases/b2b-organizations.md): no Auth0 Organization is involved anywhere in this model.

A connection is the organization boundary here: a user reaches it through a SCIM group that belongs to it. See [Direct connection memberships](#direct-connection-memberships) before relying on this model for users with no SCIM group.

## Model

FGA model · DSL

## Mapping

```yaml theme={null}
version: "1"

rules:
  - name: "Link group to connection on creation"
    when: input.type == "group.created"
    variables:
      group_id: input.data.object.id
      connection_id: input.data.object.connection_id
    tuples:
      - user: "connection:{{ variables.connection_id }}"
        relation: "connection"
        object: "group:{{ variables.group_id }}"
      - user: "group:{{ variables.group_id }}#member"
        relation: "member"
        object: "connection:{{ variables.connection_id }}"

  - name: "Add user member to group"
    when: >-
      input.type == "group.member.added" &&
      input.data.object.member.member_type == "user"
    variables:
      group_id: input.data.object?.group?.id
      member_id: input.data.object?.member?.id
    tuples:
      - user: "user:{{ variables.member_id }}"
        relation: "member"
        object: "group:{{ variables.group_id }}"

  - name: "Remove user member from group"
    when: >-
      input.type == "group.member.deleted" &&
      input.data.object.member.member_type == "user"
    variables:
      group_id: input.data.object?.group?.id
      member_id: input.data.object?.member?.id
    tuples:
      - action: delete
        user: "user:{{ variables.member_id }}"
        relation: "member"
        object: "group:{{ variables.group_id }}"

  - name: "Revoke all group tuples on deletion"
    when: >-
      input.type == "group.deleted" &&
      input.data.object?.id != nil
    variables:
      group_id: input.data.object.id
    tuple_filters:
      - object: "group:{{ variables.group_id }}"
        action: delete
      - object: "group:"
        user: "group:{{ variables.group_id }}#member"
        action: delete
      - object: "connection:"
        user: "group:{{ variables.group_id }}#member"
        action: delete

tests:
  - name: "group.created links group to connection and grants member access"
    input:
      type: "group.created"
      data:
        object:
          id: "grp_acme_eng"
          connection_id: "con_acme_saml"
    expect_tuples:
      - user: "connection:con_acme_saml"
        relation: "connection"
        object: "group:grp_acme_eng"
      - user: "group:grp_acme_eng#member"
        relation: "member"
        object: "connection:con_acme_saml"

  - name: "group.member.added adds a user member"
    input:
      type: "group.member.added"
      data:
        object:
          group:
            id: "grp_acme_eng"
          member:
            member_type: "user"
            id: "u_bob"
    expect_tuples:
      - user: "user:u_bob"
        relation: "member"
        object: "group:grp_acme_eng"

  - name: "group.member.deleted removes a user member"
    input:
      type: "group.member.deleted"
      data:
        object:
          group:
            id: "grp_acme_eng"
          member:
            member_type: "user"
            id: "u_bob"
    expect_tuples:
      - user: "user:u_bob"
        relation: "member"
        object: "group:grp_acme_eng"
        action: delete

  - name: "group.deleted revokes all group tuples"
    input:
      type: "group.deleted"
      data:
        object:
          id: "grp_acme_eng"
    expect_tuples: []
    expect_tuple_filters:
      - object: "group:grp_acme_eng"
        action: delete
      - object: "group:"
        user: "group:grp_acme_eng#member"
        action: delete
      - object: "connection:"
        user: "group:grp_acme_eng#member"
        action: delete
```

"Link group to connection on creation" writes both the bookkeeping `group --connection--> connection` tuple (which connection owns this group) and the `group#member --member--> connection` tuple that grants every group member connection membership. A `Check` for a group member on the connection can resolve `true`; a user with no synced group membership has no connection membership from this mapping.

## Direct connection memberships

Auth0's `user.created` and `user.updated` event schema does not require `context.connection`, so a rule that depends on it can miss direct grants. A direct grant must also be removed when a user is deleted or their identities change. This example intentionally does not emit direct-user connection tuples.

If your application needs to grant access to users without SCIM groups, give those memberships their own relation, `direct_member`, and include it in `member`. The mapping below reconciles and deletes only `direct_member`, so it never touches the group-derived membership:

FGA model · DSL

`data.object.identities[].connection` holds the connection's *name*, not the connection ID that `group.created` uses, so map each name to its ID explicitly and ignore identities on connections you do not list. The mapping reconciles a user's direct memberships on every `user.created` and `user.updated`, so a removed identity also loses its membership, and clears them on `user.deleted`:

> [!NOTE]
>
> The connection names appear in several places: both `when` expressions, the iterator `source`, and the name-to-ID map. When you add or rename a connection, change every one of them. If they disagree, the event fails: a name missing from the second rule's list raises a write/delete conflict, and a name missing from the map fails to evaluate.

```yaml theme={null}
version: "1"
rules:
  - name: "Sync direct connection memberships"
    when: >-
      (input.type == "user.created" || input.type == "user.updated") &&
      len(filter(input.data.object.identities, .connection in ["acme-saml", "globex-oidc"])) > 0
    variables:
      user_id: input.data.object.user_id
    tuple_filters:
      - user: "user:{{ variables.user_id }}"
        relation: "direct_member"
        object: "connection:"
    iterator:
      source: filter(input.data.object.identities, .connection in ["acme-saml", "globex-oidc"])
      as: identity
      tuples:
        - user: "user:{{ variables.user_id }}"
          relation: "direct_member"
          object: "connection:{{ ({'acme-saml': 'con_acme_saml', 'globex-oidc': 'con_globex_oidc'})[identity.connection] }}"

  - name: "Remove direct connection memberships when no listed identity remains"
    when: >-
      (input.type == "user.created" || input.type == "user.updated") &&
      len(filter(input.data.object.identities, .connection in ["acme-saml", "globex-oidc"])) == 0
    variables:
      user_id: input.data.object.user_id
    tuple_filters:
      - user: "user:{{ variables.user_id }}"
        relation: "direct_member"
        object: "connection:"
        action: delete

  - name: "Remove direct connection memberships on user deletion"
    when: input.type == "user.deleted"
    variables:
      user_id: input.data.object.user_id
    tuple_filters:
      - user: "user:{{ variables.user_id }}"
        relation: "direct_member"
        object: "connection:"
        action: delete

tests:
  - name: "user.updated writes a direct membership for each listed connection"
    input:
      type: "user.updated"
      data:
        object:
          user_id: "u_alice"
          identities:
            - connection: "acme-saml"
            - connection: "globex-oidc"
    expect_tuples:
      - user: "user:u_alice"
        relation: "direct_member"
        object: "connection:con_acme_saml"
      - user: "user:u_alice"
        relation: "direct_member"
        object: "connection:con_globex_oidc"
    expect_tuple_filters:
      - user: "user:u_alice"
        relation: "direct_member"
        object: "connection:"

  - name: "identities on unlisted connections are skipped"
    input:
      type: "user.updated"
      data:
        object:
          user_id: "u_bob"
          identities:
            - connection: "Username-Password-Authentication"
            - connection: "acme-saml"
    expect_tuples:
      - user: "user:u_bob"
        relation: "direct_member"
        object: "connection:con_acme_saml"
    expect_tuple_filters:
      - user: "user:u_bob"
        relation: "direct_member"
        object: "connection:"

  - name: "a user with no listed identity clears direct memberships"
    input:
      type: "user.updated"
      data:
        object:
          user_id: "u_carol"
          identities:
            - connection: "Username-Password-Authentication"
    expect_tuples: []
    expect_tuple_filters:
      - user: "user:u_carol"
        relation: "direct_member"
        object: "connection:"
        action: delete

  - name: "user.deleted clears every direct membership"
    input:
      type: "user.deleted"
      data:
        object:
          user_id: "u_alice"
    expect_tuples: []
    expect_tuple_filters:
      - user: "user:u_alice"
        relation: "direct_member"
        object: "connection:"
        action: delete
```

Replace the connection names and IDs with your own. Take them from your tenant's Management API connection list, not from the optional `context.connection`. Reserve `direct_member` for this mapping: the filters match every `user --direct_member--> connection` tuple for the user, so any other writer to that relation would have its tuples removed. See [Backfilling and reconciling](https://docs.fga.dev/relationship-sync/managing.md#reconciling) for loading existing users.

## Known limitations

* **Connection changes and deletion.** This group mapping handles `group.created` and `group.deleted`, but does not reconcile a group moved between connections or clean up all group grants on `connection.deleted`. Reconcile mapping-owned group tuples if either occurs; do not delete unrelated memberships.
* **Roles.** There's no per-user role assignment tied to a plain connection. If you need roles here, assign them to the SCIM group itself; see [Roles: a group, at the tenant level](https://docs.fga.dev/relationship-sync/use-cases/roles.md#a-group-at-the-tenant-level).
