# Handling roles across multiple customers/hostnames

**URL:** <https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835>\
**Category:** Get Help\
**Tags:** multi-tenant, rules, management-api\
**Created:** [June 1, 2017, 8:29am UTC](https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835 "2017-06-01T08:29:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pacific202](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@pacific202](https://community.auth0.com/u/pacific202)\
**Post date:** [June 1, 2017, 8:29am UTC](https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835/1 "2017-06-01T08:29:43Z")

</div>

We have multiple customers, each of whom could have multiple hostnames or app access points. Each user will have one set of roles for each domain, but could easily have different roles on different domains.

This is an extension of the question asked in [this post](http://community.auth0.com/questions/2712/proper-multi-tenant-set-up?childToView=2714#answer-2714)

Ours is analogous to a school where a user A is both a teacher and a parent and can log in to the app at staff.myschool.edu and access all the teacher functionality then later log in to parent.anotherschool.edu to check their child’s grades. Same login, but different user experience depending on where you access the app.

I’m planning on using the app\_metadata property of the user to store roles for each domain and then processing it inside an Auth0 rule. The choice of root key for the app\_metadata array seems to be critical:

[Link to Auth0 User API](https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id)

- The properties of the new object will replace the old ones.
- The metadata fields are an exception to this rule (user\_metadata and app\_metadata). These properties are merged instead of being replaced but be careful, the merge only occurs on the first level.

I’m hoping to create a rule that will detect which domain has requested access, read that subtree’s values from the app\_metadata, then only send back the roles relevant to that domain in the app\_metadata.

Here’s what the rule would effectively do. If this is the object stored by Auth0:

```
{
  "username": "Michelle Smith",
  "app_metadata": {
    "portal.domaina.com": "Administrator", "Scheduler"],
    "my.domainb.com": "Customer"]
  }
} 

```

When accessed from [portal.domaina.com](http://portal.domaina.com) this will be returned:

```
{
  "username": "Michelle Smith",
  "app_metadata": {
    "roles": "Administrator", "Scheduler"]
  }
}

```

When accessed from [my.domainb.com](http://my.domainb.com) it will look like this:

```
{
  "username": "Michelle Smith",
  "app_metadata": {
    "roles": "Customer"]
  }
} 

```

I have read on this forum that the limit on the size of the user object is 16 MB. That lets you store a massive number of roles per user before you need to consider other strategies.

Managing which roles a user has on a given domain will require calling the Auth0 management api, and because of the section quoted above I know we can just send an update to a single domain without impacting the others that might be in that user’s profile.

I am hoping that someone else with more experience in this can weigh in here and let us know what the best practices are for accomplishing this.

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [June 6, 2017, 6:45pm UTC](https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835/2 "2017-06-06T18:45:12Z")

</div>

In general your approach is suitable although the security aspects of it would always be dependent of the full implementation. For example, would a client application be able to get tricked into simulating that the request is from another application/domain and as such receive roles not meant to itself.

I would advise you also, if you haven’t done so already, to take a look at the [authorization extension](https://auth0.com/docs/extensions/authorization-extension/v2) documentation as I see some overlap between what it offers and what you’re trying to achieve. In particular, when you [create a role in the extension](https://auth0.com/docs/extensions/authorization-extension/v2#roles) it already asks you to which application this role is associated to so you may get a quicker implementation of you requirements by leveraging the extension.

---

<div class="post-metadata">

**Author:** ![pacific202](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@pacific202](https://community.auth0.com/u/pacific202)\
**Post date:** [June 6, 2017, 9:16pm UTC](https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835/3 "2017-06-06T21:16:26Z")

</div>

Thanks, the authorization extension looks very interesting. However, it doesn’t seem to address the multi-tenant requirement. It would just shift the burden on our code from querying the data store to pull in the user’s roles for the given domain to querying the data store to retrieve the Auth0 application settings. I’ve read the docs a few times and can’t see a way to inject a filter into the defined roles based on the incoming hostname.

As for tricking the client, we’re also going to be enforcing these roles on the server which should make it practically impossible to spoof sites.

---

<div class="post-metadata">

**Author:** ![pacific202](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@pacific202](https://community.auth0.com/u/pacific202)\
**Post date:** [July 18, 2017, 3:32am UTC](https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835/4 "2017-07-18T03:32:34Z")

</div>

As a quick follow-up to this, we were able to successfully implement this using Azure Functions on the back-end to populate the roles and this rule to attach the current host’s roles into the token:

```
function (user, context, callback) {
  
  var namespace = 'http://yourdomain.com/';
  var url = require('url');
  var hostname = url.parse(context.request.query.redirect_uri).hostname;
  hostname = hostname.replace(/\./g, '_');
  var roles = ];
  
  if(user.app_metadata === undefined || user.app_metadata[hostname] === undefined || user.app_metadata[hostname].roles === undefined){
    roles = ];
  } else {
    roles = user.app_metadata[hostname].roles;
  }
    
  context.idToken[namespace + 'roles'] = roles;
  
  callback(null, user, context);
}

```

Here’s what my user’s app\_metadata looks like:

```
{
  "localhost": {
    "roles": 
      "Administration",
      "CheckIn"
    ]
  },
  "development_sportsmgmt_net": {
    "roles": 
      "Administration",
      "CheckIn"
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 2, 2018, 5:54am UTC](https://community.auth0.com/t/handling-roles-across-multiple-customers-hostnames/6835/5 "2018-03-02T05:54:16Z")

</div>


