"""Logic for Principals """ from ..models import roles from cerbos.sdk.model import Principal def build_principal(orchardIdentityId, jwt_data): apps=[] tenants={} for each in jwt_data: # where each is a profile/role/label combination # Add list of apps user has access to app = each['profileType'][0:-7] # Remove 'Profile' suffix apps.append(app) # Run through each profile/role pair and add an attachment within that label tenant vendor_id = each['vendorId'] subaccount_id = each['subaccountId'] print('vendor_id', vendor_id, 'subaccount_id', subaccount_id) if vendor_id: if not tenants.get(vendor_id): attachments = {} if each['roles']: print('vendor roles', each['roles']) for role in each['roles']: role_type = f'{app}_{role}' attachments[role_type] = {'role': role_type} tenants[vendor_id] = { "account_id": vendor_id, "attachments": attachments } else: if each['roles']: for role in each['roles']: role_type = f'{app}_{role}' tenants[vendor_id]['attachments'][role_type] = {'role': role_type} if subaccount_id: if not tenants.get(subaccount_id): attachments = {} if each['roles']: for role in each['roles']: role_type = f'{app}_{role}' attachments[role_type] = {'role': role_type} tenants[subaccount_id] = { "subaccount_id": subaccount_id, "attachments": attachments } # TODO: Get account_id corresponding with the subaccount else: if each['roles']: for role in each['roles']: role_type = f'{app}_{role}' tenants[subaccount_id]['attachments'][role_type] = {'role': role_type} principal= { "id": orchardIdentityId, "roles": ["user"], "attr": { "type": "human", "apps": list(set(apps)), "tenants": tenants } } return principal def create_cerbos_principal(principal): return Principal( id=principal['id'], roles=principal['roles'], attr=principal['attr'] ) def hydrate_JWT(orchardIdentityId): data = roles.hydrate_roles(orchardIdentityId) return build_principal(orchardIdentityId, data)