-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcasbin.authorizer.ts
More file actions
87 lines (73 loc) · 2.49 KB
/
casbin.authorizer.ts
File metadata and controls
87 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
// Node module: @loopback/example-access-control-migration
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
AuthorizationContext,
AuthorizationDecision,
AuthorizationMetadata,
AuthorizationRequest,
Authorizer,
} from '@loopback/authorization';
import {inject, Provider} from '@loopback/core';
import * as casbin from 'casbin';
import {RESOURCE_ID} from '../../../keys';
const debug = require('debug')('loopback:example:acl');
const DEFAULT_SCOPE = 'execute';
// Class level authorizer
export class CasbinAuthorizationProvider implements Provider<Authorizer> {
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<AuthorizationDecision> {
const subject = this.getUserName(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;
}
// Generate the user name according to the naming convention
// in casbin policy
// A user's name would be `u${id}`
getUserName(id: number): string {
return `u${id}`;
}
}