Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 34
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2e0df4

Browse files
committedJul 26, 2020
add .npmignore; refactor Authorizer.ts and test suites
Signed-off-by: kingiw <kingiw@hotmail.com>
1 parent d150b0a commit a2e0df4

File tree

7 files changed

+139
-118
lines changed

7 files changed

+139
-118
lines changed
 

‎.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
22

33
# dependencies
4-
/node_modules
4+
**/node_modules/
55
/.pnp
66
.pnp.js
77

@@ -29,4 +29,6 @@ yarn.lock
2929
.idea/
3030
*.iml
3131

32-
dist/
32+
dist/
33+
34+
test/

‎.npmignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# dependencies
2+
node_modules/
3+
4+
# Src and test suites
5+
src/
6+
7+
# Other extensions
8+
extensions/
9+
10+
.idea/
11+
package-lock.json
12+
yarn.lock
13+
14+
# webpack configurations
15+
webpack.*
16+
17+
npm-debug.log

‎src/Authorizer.ts

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import axios from 'axios';
2+
import Cookies from 'js-cookie';
3+
import Permission from './Permission';
4+
import { StringKV } from './types';
5+
6+
interface BaseResponse {
7+
message: string;
8+
data: any;
9+
}
10+
11+
type Mode = "auto" | "cookies" | "manual"
12+
13+
export class Authorizer {
14+
private mode!: Mode;
15+
private endpoint: string | undefined = undefined;
16+
private user : string | undefined;
17+
private permission = new Permission();
18+
private cookieKey : string | undefined = undefined;
19+
20+
/**
21+
*
22+
* @param mode "auto", "cookies" or "manual"
23+
* "auto": Specify the casbin server endpoint, and Casbin.js will load permission from it when the identity changes
24+
* "cookies": Casbin.js load the permission data from the cookie "casbin_permission" or the specified cookie key.
25+
* "manual": Load the permission mannually with "setPermission"
26+
* @param args.endpoint Casbin service endpoint, required when mode == "auto"
27+
* @param args.cookieKey The cookie key when loading permission, activated when mode == "cookies"
28+
*/
29+
constructor(mode: Mode, args: {endpoint?: string, cookieKey?: string} = {}) {
30+
if (mode == 'auto') {
31+
if (!args.endpoint) {
32+
throw new Error("Specify the endpoint when initializing casbin.js with mode == 'auto'");
33+
return;
34+
} else {
35+
this.mode = mode;
36+
this.endpoint = args.endpoint;
37+
}
38+
} else if (mode == 'cookies') {
39+
this.mode = mode;
40+
let permission = Cookies.get(args.cookieKey ? args.cookieKey : "casbin_perm");
41+
if (permission) {
42+
this.setPermission(permission);
43+
} else {
44+
console.log("WARNING: No specified cookies");
45+
}
46+
} else if (mode == 'manual') {
47+
this.mode = mode;
48+
} else {
49+
throw new Error("Casbin.js mode can only be one of the 'auto', 'cookies' and 'manual'");
50+
}
51+
}
52+
53+
/**
54+
* Get the permission.
55+
*/
56+
public getPermission() : StringKV {
57+
return this.permission.getPermissionJson();
58+
}
59+
60+
public setPermission(permission : Record<string, unknown> | string) : void{
61+
this.permission.load(permission);
62+
}
63+
64+
/**
65+
* Get the authority of a given user from Casbin core
66+
*/
67+
private async syncUserPermission(): Promise<void> {
68+
if (this.endpoint !== undefined) {
69+
const resp = await axios.get<BaseResponse>(`${this.endpoint}?casbin_subject=${this.user}`);
70+
this.permission.load(resp.data.data);
71+
console.log("syncUserPermission is called")
72+
}
73+
}
74+
75+
/**
76+
* Set the user subject for the authroizer
77+
* @param user The current user
78+
*/
79+
public async setUser(user : string) : Promise<void> {
80+
// Sync with the server and fetch the latest permission of the new user
81+
if (this.mode == 'auto' && user != this.user) {
82+
this.user = user;
83+
await this.syncUserPermission()
84+
}
85+
}
86+
87+
public can(action: string, object: string): boolean {
88+
return this.permission.check(action, object);
89+
}
90+
91+
public cannot(action: string, object: string): boolean {
92+
return !this.permission.check(action, object);
93+
}
94+
95+
public canAll(action: string, objects: Array<string>) : boolean {
96+
for (let i = 0; i < objects.length; ++i) {
97+
if (!this.permission.check(action, objects[i])) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
104+
public canAny(action: string, objects: Array<string>) : boolean {
105+
for (let i = 0; i < objects.length; ++i) {
106+
if (this.permission.check(action, objects[i])) {
107+
return true;
108+
}
109+
}
110+
return false;
111+
}
112+
113+
}
File renamed without changes.

‎src/__test__/permission.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Permission from '../permission';
1+
import Permission from '../Permission';
2+
23
describe('Permission unit test', () => {
34
const policyExample = {
45
'read': ['data1', 'data2'],

‎src/__test__/server.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { Authorizer } from '../index';
66
import TestServer from './server';
77
import Axios from 'axios';
88

9-
describe('Communication with server', () => {
9+
describe('Auto mode', () => {
1010
let server: TestServer;
1111
beforeAll(async () => {
1212
server = new TestServer();
1313
await server.start();
1414
});
1515

1616
test('Request for /api/casbin', async () => {
17-
const authorizer = new Authorizer('http://localhost:4000/api/casbin');
17+
const authorizer = new Authorizer('auto', { endpoint: 'http://localhost:4000/api/casbin'});
1818
await authorizer.setUser('alice');
1919
expect(authorizer.getPermission()).toMatchObject({
2020
read: ['data1', 'data2'],

‎src/index.ts

+1-113
Original file line numberDiff line numberDiff line change
@@ -1,113 +1 @@
1-
import axios from 'axios';
2-
import Cookies from 'js-cookie';
3-
import Permission from './permission';
4-
import { StringKV } from './types';
5-
6-
interface BaseResponse {
7-
message: string;
8-
data: any;
9-
}
10-
11-
type Mode = "auto" | "cookies" | "manual"
12-
13-
export class Authorizer {
14-
private mode!: Mode;
15-
private endpoint: string | undefined = undefined;
16-
private user : string | undefined;
17-
private permission = new Permission();
18-
private cookieKey : string | undefined = undefined;
19-
20-
/**
21-
*
22-
* @param mode "auto", "cookies" or "manual"
23-
* "auto": Specify the casbin server endpoint, and Casbin.js will load permission from it when the identity changes
24-
* "cookies": Casbin.js load the permission data from the cookie "casbin_permission" or the specified cookie key.
25-
* "manual": Load the permission mannually with "setPermission"
26-
* @param args.endpoint Casbin service endpoint, required when mode == "auto"
27-
* @param args.cookieKey The cookie key when loading permission, activated when mode == "cookies"
28-
*/
29-
constructor(mode: Mode, args: {endpoint?: string, cookieKey?: string} = {}) {
30-
if (mode == 'auto') {
31-
if (!args.endpoint) {
32-
throw new Error("Specify the endpoint when initializing casbin.js with mode == 'auto'");
33-
return;
34-
} else {
35-
this.mode = mode;
36-
this.endpoint = args.endpoint;
37-
}
38-
} else if (mode == 'cookies') {
39-
this.mode = mode;
40-
let permission = Cookies.get(args.cookieKey ? args.cookieKey : "casbin_perm");
41-
if (permission) {
42-
this.setPermission(permission);
43-
} else {
44-
console.log("WARNING: No specified cookies");
45-
}
46-
} else if (mode == 'manual') {
47-
this.mode = mode;
48-
} else {
49-
throw new Error("Casbin.js mode can only be one of the 'auto', 'cookies' and 'manual'");
50-
}
51-
}
52-
53-
/**
54-
* Get the permission.
55-
*/
56-
public getPermission() : StringKV {
57-
return this.permission.getPermissionJson();
58-
}
59-
60-
public setPermission(permission : Record<string, unknown> | string) : void{
61-
this.permission.load(permission);
62-
}
63-
64-
/**
65-
* Get the authority of a given user from Casbin core
66-
*/
67-
private async syncUserPermission(): Promise<void> {
68-
if (this.endpoint !== undefined) {
69-
const resp = await axios.get<BaseResponse>(`${this.endpoint}?casbin_subject=${this.user}`);
70-
this.permission.load(resp.data.data);
71-
console.log("syncUserPermission is called")
72-
}
73-
}
74-
75-
/**
76-
* Set the user subject for the authroizer
77-
* @param user The current user
78-
*/
79-
public async setUser(user : string) : Promise<void> {
80-
// Sync with the server and fetch the latest permission of the new user
81-
if (this.mode == 'auto' && user != this.user) {
82-
this.user = user;
83-
await this.syncUserPermission()
84-
}
85-
}
86-
87-
public can(action: string, object: string): boolean {
88-
return this.permission.check(action, object);
89-
}
90-
91-
public cannot(action: string, object: string): boolean {
92-
return !this.permission.check(action, object);
93-
}
94-
95-
public canAll(action: string, objects: Array<string>) : boolean {
96-
for (let i = 0; i < objects.length; ++i) {
97-
if (!this.permission.check(action, objects[i])) {
98-
return false;
99-
}
100-
}
101-
return true;
102-
}
103-
104-
public canAny(action: string, objects: Array<string>) : boolean {
105-
for (let i = 0; i < objects.length; ++i) {
106-
if (this.permission.check(action, objects[i])) {
107-
return true;
108-
}
109-
}
110-
return false;
111-
}
112-
113-
}
1+
export * from './Authorizer';

0 commit comments

Comments
 (0)