Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]How to add RBAC with domains in loopback4 #412

Open
PoyuLU opened this issue Jan 12, 2023 · 6 comments
Open

[Question]How to add RBAC with domains in loopback4 #412

PoyuLU opened this issue Jan 12, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@PoyuLU
Copy link

PoyuLU commented Jan 12, 2023

export class CasbinAuthorizationProvider implements Provider {
constructor(
@Inject('casbin.enforcer.factory')
private enforcerFactory: (name: string) => Promise<casbin.Enforcer>,
) {}

/**

@returns authenticateFn
*/
value(): Authorizer {
return this.authorize.bind(this);
}
async authorize(
authorizationCtx: AuthorizationContext,
metadata: AuthorizationMetadata,
): Promise {
const subject = this.getUserId(authorizationCtx.principals[0].id);
const resourceId = await authorizationCtx.invocationContext.get(
RESOURCE_ID,
{optional: true},
);
const object = resourceId ?? metadata.resource ?? authorizationCtx.resource;
const request: AuthorizationRequest = {
subject,
object,
action: metadata.scopes?.[0] ?? DEFAULT_SCOPE,
};

const allowedRoles = metadata.allowedRoles;

if (!allowedRoles) return AuthorizationDecision.ALLOW;
if (allowedRoles.length < 1) return AuthorizationDecision.DENY;

let allow = false;

// An optimization for ONLY searching among the allowed roles' policies
for (const role of allowedRoles) {
  const enforcer = await this.enforcerFactory(role);

  const allowedByRole = await enforcer.enforce(
    request.subject,
    request.object,
    request.action,
  );

  debug(`authorizer role: ${role}, result: ${allowedByRole}`);
  if (allowedByRole) {
    allow = true;
    break;
  }
}

debug('final result: ', allow);

if (allow) return AuthorizationDecision.ALLOW;
else if (allow === false) return AuthorizationDecision.DENY;
return AuthorizationDecision.ABSTAIN;
}

how to add domain in casbin.authorizer?

@casbin-bot
Copy link
Member

@PoyuLU
Copy link
Author

PoyuLU commented Jan 13, 2023

@nodece @Zxilly @Shivansh-yadav13

Hi, anyone can give me some tips how to modified domain in node.casbin, thank you.

@Zxilly
Copy link
Contributor

Zxilly commented Jan 13, 2023

Looks like you implement role management without casbin?

 const allowedRoles = metadata.allowedRoles;

 if (!allowedRoles) return AuthorizationDecision.ALLOW;
 if (allowedRoles.length < 1) return AuthorizationDecision.DENY;

@PoyuLU
Copy link
Author

PoyuLU commented Jan 13, 2023

Looks like you implement role management without casbin?

 const allowedRoles = metadata.allowedRoles;

 if (!allowedRoles) return AuthorizationDecision.ALLOW;
 if (allowedRoles.length < 1) return AuthorizationDecision.DENY;

And you create enforcer everytime

const enforcer = await this.enforcerFactory(role);

Seems not a good practice, if you use rbac in casbin, it has to rebuild the whole role graph every time.

I have casbin in different file.
How to access the domain value and compare with RBAC with domain model in the above code?

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act

@PoyuLU
Copy link
Author

PoyuLU commented Jan 15, 2023

I think you use this file and want to add domain support? https://github.com/loopbackio/loopback-next/blob/master/examples/access-control-migration/src/components/casbin-authorization/services/casbin.authorizer.ts

Please ref to https://github.com/casbin/node-casbin/blob/master/test/rbacwDomainAPI.test.ts

What if I have thousands of users, how do I check their roles and domains dynamically?

@casbin casbin deleted a comment from suyash5053 Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

4 participants