@celljs/security 4.1.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/esm/browser/module.d.ts +1 -1
- package/esm/browser/user/behavior-subject.d.ts +16 -0
- package/esm/browser/user/behavior-subject.js +28 -0
- package/esm/browser/user/behavior-subject.js.map +1 -0
- package/esm/browser/user/index.d.ts +1 -0
- package/esm/browser/user/index.js +1 -0
- package/esm/browser/user/index.js.map +1 -1
- package/esm/browser/user/user-manager.d.ts +1 -1
- package/esm/browser/user/user-manager.js +2 -3
- package/esm/browser/user/user-manager.js.map +1 -1
- package/esm/browser/user/user-protocol.d.ts +1 -1
- package/esm/node/access/index.d.ts +1 -0
- package/esm/node/access/index.js +1 -0
- package/esm/node/access/index.js.map +1 -1
- package/esm/node/access/method-advice.js +3 -7
- package/esm/node/access/method-advice.js.map +1 -1
- package/esm/node/access/pattern-util.d.ts +8 -0
- package/esm/node/access/pattern-util.js +78 -0
- package/esm/node/access/pattern-util.js.map +1 -0
- package/esm/node/access/policy-provider.js +2 -3
- package/esm/node/access/policy-provider.js.map +1 -1
- package/esm/node/access/policy-resolver.d.ts +2 -2
- package/esm/node/access/policy-resolver.js +7 -10
- package/esm/node/access/policy-resolver.js.map +1 -1
- package/esm/node/access/resource-name-resolver.d.ts +3 -3
- package/esm/node/access/resource-name-resolver.js +8 -10
- package/esm/node/access/resource-name-resolver.js.map +1 -1
- package/esm/node/cache/request-cache.d.ts +2 -0
- package/esm/node/cache/request-cache.js +7 -2
- package/esm/node/cache/request-cache.js.map +1 -1
- package/esm/node/module.d.ts +1 -1
- package/lib/browser/module.d.ts +1 -1
- package/lib/browser/user/behavior-subject.d.ts +17 -0
- package/lib/browser/user/behavior-subject.d.ts.map +1 -0
- package/lib/browser/user/behavior-subject.js +32 -0
- package/lib/browser/user/behavior-subject.js.map +1 -0
- package/lib/browser/user/behavior-subject.spec.d.ts +2 -0
- package/lib/browser/user/behavior-subject.spec.d.ts.map +1 -0
- package/lib/browser/user/behavior-subject.spec.js +20 -0
- package/lib/browser/user/behavior-subject.spec.js.map +1 -0
- package/lib/browser/user/index.d.ts +1 -0
- package/lib/browser/user/index.d.ts.map +1 -1
- package/lib/browser/user/index.js +1 -0
- package/lib/browser/user/index.js.map +1 -1
- package/lib/browser/user/user-manager.d.ts +1 -1
- package/lib/browser/user/user-manager.d.ts.map +1 -1
- package/lib/browser/user/user-manager.js +3 -4
- package/lib/browser/user/user-manager.js.map +1 -1
- package/lib/browser/user/user-protocol.d.ts +1 -1
- package/lib/browser/user/user-protocol.d.ts.map +1 -1
- package/lib/node/access/expression-resolvers.spec.js +3 -3
- package/lib/node/access/expression-resolvers.spec.js.map +1 -1
- package/lib/node/access/index.d.ts +1 -0
- package/lib/node/access/index.d.ts.map +1 -1
- package/lib/node/access/index.js +1 -0
- package/lib/node/access/index.js.map +1 -1
- package/lib/node/access/method-advice.d.ts.map +1 -1
- package/lib/node/access/method-advice.js +2 -5
- package/lib/node/access/method-advice.js.map +1 -1
- package/lib/node/access/pattern-util.d.ts +9 -0
- package/lib/node/access/pattern-util.d.ts.map +1 -0
- package/lib/node/access/pattern-util.js +81 -0
- package/lib/node/access/pattern-util.js.map +1 -0
- package/lib/node/access/pattern-util.spec.d.ts +2 -0
- package/lib/node/access/pattern-util.spec.d.ts.map +1 -0
- package/lib/node/access/pattern-util.spec.js +35 -0
- package/lib/node/access/pattern-util.spec.js.map +1 -0
- package/lib/node/access/policy-provider.js +2 -2
- package/lib/node/access/policy-provider.js.map +1 -1
- package/lib/node/access/policy-resolver.d.ts +2 -2
- package/lib/node/access/policy-resolver.d.ts.map +1 -1
- package/lib/node/access/policy-resolver.js +6 -8
- package/lib/node/access/policy-resolver.js.map +1 -1
- package/lib/node/access/resource-name-resolver.d.ts +3 -3
- package/lib/node/access/resource-name-resolver.d.ts.map +1 -1
- package/lib/node/access/resource-name-resolver.js +7 -9
- package/lib/node/access/resource-name-resolver.js.map +1 -1
- package/lib/node/cache/request-cache.d.ts +2 -0
- package/lib/node/cache/request-cache.d.ts.map +1 -1
- package/lib/node/cache/request-cache.js +7 -35
- package/lib/node/cache/request-cache.js.map +1 -1
- package/lib/node/cache/request-cache.spec.d.ts +2 -0
- package/lib/node/cache/request-cache.spec.d.ts.map +1 -0
- package/lib/node/cache/request-cache.spec.js +16 -0
- package/lib/node/cache/request-cache.spec.js.map +1 -0
- package/lib/node/module.d.ts +1 -1
- package/package.json +7 -11
- package/src/browser/user/behavior-subject.spec.ts +18 -0
- package/src/browser/user/behavior-subject.ts +36 -0
- package/src/browser/user/index.ts +2 -1
- package/src/browser/user/user-manager.ts +3 -5
- package/src/browser/user/user-protocol.ts +2 -2
- package/src/node/access/expression-resolvers.spec.ts +5 -5
- package/src/node/access/index.ts +1 -0
- package/src/node/access/method-advice.ts +1 -2
- package/src/node/access/pattern-util.spec.ts +33 -0
- package/src/node/access/pattern-util.ts +71 -0
- package/src/node/access/policy-provider.ts +3 -3
- package/src/node/access/policy-resolver.ts +7 -9
- package/src/node/access/resource-name-resolver.ts +8 -10
- package/src/node/cache/request-cache.spec.ts +14 -0
- package/src/node/cache/request-cache.ts +8 -3
|
@@ -1,43 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
2
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
3
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
4
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
7
|
};
|
|
24
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
-
var ownKeys = function(o) {
|
|
26
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
-
var ar = [];
|
|
28
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
-
return ar;
|
|
30
|
-
};
|
|
31
|
-
return ownKeys(o);
|
|
32
|
-
};
|
|
33
|
-
return function (mod) {
|
|
34
|
-
if (mod && mod.__esModule) return mod;
|
|
35
|
-
var result = {};
|
|
36
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
-
__setModuleDefault(result, mod);
|
|
38
|
-
return result;
|
|
39
|
-
};
|
|
40
|
-
})();
|
|
41
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
10
|
};
|
|
@@ -47,18 +14,23 @@ const cache_protocol_1 = require("./cache-protocol");
|
|
|
47
14
|
const node_1 = require("@celljs/web/lib/node");
|
|
48
15
|
const core_1 = require("@celljs/core");
|
|
49
16
|
const web_1 = require("@celljs/web");
|
|
50
|
-
const qs = __importStar(require("qs"));
|
|
51
17
|
let HttpSessionRequestCache = class HttpSessionRequestCache {
|
|
52
18
|
async save(savedRequest) {
|
|
19
|
+
var _a;
|
|
53
20
|
if (node_1.Context.getSession()) {
|
|
54
21
|
const request = node_1.Context.getRequest();
|
|
55
22
|
node_1.Context.setAttr(cache_protocol_1.SAVED_REQUEST, savedRequest || {
|
|
56
|
-
redirectUrl: await this.pathResolver.resolve(this.endpoint, request.path,
|
|
23
|
+
redirectUrl: await this.pathResolver.resolve(this.endpoint, request.path, this.queryString((_a = request.originalUrl) !== null && _a !== void 0 ? _a : request.url)),
|
|
57
24
|
method: request.method.toUpperCase(),
|
|
58
25
|
query: { ...request.query }
|
|
59
26
|
}, node_1.AttributeScope.Session);
|
|
60
27
|
}
|
|
61
28
|
}
|
|
29
|
+
/** `?a=1` of the URL as the client sent it, or `''`. */
|
|
30
|
+
queryString(url = '') {
|
|
31
|
+
const index = url.indexOf('?');
|
|
32
|
+
return index < 0 || index === url.length - 1 ? '' : url.slice(index);
|
|
33
|
+
}
|
|
62
34
|
async get() {
|
|
63
35
|
if (node_1.Context.getSession()) {
|
|
64
36
|
return node_1.Context.getAttr(cache_protocol_1.SAVED_REQUEST, node_1.AttributeScope.Session);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-cache.js","sourceRoot":"","sources":["../../../src/node/cache/request-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-cache.js","sourceRoot":"","sources":["../../../src/node/cache/request-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAA6E;AAC7E,+CAA+D;AAC/D,uCAA2D;AAC3D,qCAAqD;AAG9C,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;IAQhC,KAAK,CAAC,IAAI,CAAC,YAA2B;;QAClC,IAAI,cAAO,CAAC,UAAU,EAAE,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,cAAO,CAAC,UAAU,EAAE,CAAC;YACrC,cAAO,CAAC,OAAO,CAAC,8BAAa,EAAE,YAAY,IAAI;gBAC3C,WAAW,EAAE,MAAM,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,OAAC,OAAO,CAAC,WAAW,mCAAI,OAAO,CAAC,GAAG,CAAC,CAAC;gBAC/H,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE;gBACpC,KAAK,EAAE,EAAE,GAAG,OAAO,CAAC,KAAK,EAAE;aAC9B,EAAE,qBAAc,CAAC,OAAO,CAAC,CAAC;QAC/B,CAAC;IACL,CAAC;IACD,wDAAwD;IAC9C,WAAW,CAAC,GAAG,GAAG,EAAE;QAC1B,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC/B,OAAO,KAAK,GAAG,CAAC,IAAI,KAAK,KAAK,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACzE,CAAC;IAED,KAAK,CAAC,GAAG;QACL,IAAI,cAAO,CAAC,UAAU,EAAE,EAAE,CAAC;YACvB,OAAO,cAAO,CAAC,OAAO,CAAe,8BAAa,EAAE,qBAAc,CAAC,OAAO,CAAC,CAAC;QAChF,CAAC;IACL,CAAC;IAED,KAAK,CAAC,MAAM;QACR,IAAI,cAAO,CAAC,UAAU,EAAE,EAAE,CAAC;YACvB,cAAO,CAAC,OAAO,CAAC,8BAAa,EAAE,SAAS,EAAE,qBAAc,CAAC,OAAO,CAAC,CAAC;QACtE,CAAC;IACL,CAAC;CAEJ,CAAA;;AAjCsB;IADlB,IAAA,YAAK,EAAC,cAAQ,CAAC;;yDACoB;AAGjB;IADlB,IAAA,gBAAS,EAAC,kBAAY,CAAC;;6DACsB;kCANrC,uBAAuB;IADnC,IAAA,gBAAS,EAAC,6BAAY,CAAC;GACX,uBAAuB,CAoCnC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-cache.spec.d.ts","sourceRoot":"","sources":["../../../src/node/cache/request-cache.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bun_test_1 = require("bun:test");
|
|
4
|
+
const chai_1 = require("chai");
|
|
5
|
+
const core_1 = require("@celljs/core");
|
|
6
|
+
const request_cache_1 = require("./request-cache");
|
|
7
|
+
(0, bun_test_1.describe)('HttpSessionRequestCache', () => {
|
|
8
|
+
(0, bun_test_1.it)('keeps the query of the saved request as the client sent it', () => {
|
|
9
|
+
const cache = new request_cache_1.HttpSessionRequestCache();
|
|
10
|
+
(0, chai_1.expect)(cache.queryString('/orders?id=1&tag=a&tag=b')).to.equal('?id=1&tag=a&tag=b');
|
|
11
|
+
(0, chai_1.expect)(cache.queryString('/orders?')).to.equal('');
|
|
12
|
+
(0, chai_1.expect)(cache.queryString('/orders')).to.equal('');
|
|
13
|
+
(0, chai_1.expect)(core_1.UrlUtil.join('http://localhost:3000', '/orders', cache.queryString('/orders?id=1'))).to.equal('http://localhost:3000/orders?id=1');
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=request-cache.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-cache.spec.js","sourceRoot":"","sources":["../../../src/node/cache/request-cache.spec.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AACxC,+BAA8B;AAC9B,uCAAuC;AACvC,mDAA0D;AAE1D,IAAA,mBAAQ,EAAC,yBAAyB,EAAE,GAAG,EAAE;IACrC,IAAA,aAAE,EAAC,4DAA4D,EAAE,GAAG,EAAE;QAClE,MAAM,KAAK,GAAG,IAAI,uCAAuB,EAAsD,CAAC;QAChG,IAAA,aAAM,EAAC,KAAK,CAAC,WAAW,CAAC,0BAA0B,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACpF,IAAA,aAAM,EAAC,KAAK,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACnD,IAAA,aAAM,EAAC,KAAK,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAClD,IAAA,aAAM,EAAC,cAAO,CAAC,IAAI,CAAC,uBAAuB,EAAE,SAAS,EAAE,KAAK,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAC9I,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/lib/node/module.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/security",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
"url": "https://github.com/cellbang/cell.git"
|
|
15
15
|
},
|
|
16
16
|
"license": "MIT",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18.0.0"
|
|
19
|
+
},
|
|
17
20
|
"main": "lib/common/index.js",
|
|
18
21
|
"typings": "lib/common/index.d.ts",
|
|
19
22
|
"files": [
|
|
@@ -29,16 +32,9 @@
|
|
|
29
32
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
30
33
|
},
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"@celljs/core": "4.
|
|
33
|
-
"@celljs/http": "4.
|
|
34
|
-
"@celljs/web": "4.
|
|
35
|
-
"axios": "^0.35.0",
|
|
36
|
-
"micromatch": "^4.0.2",
|
|
37
|
-
"qs": "^6.17.1",
|
|
38
|
-
"rxjs": "^6.6.0"
|
|
39
|
-
},
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"@types/micromatch": "^4.0.1"
|
|
35
|
+
"@celljs/core": "4.2.0",
|
|
36
|
+
"@celljs/http": "4.2.0",
|
|
37
|
+
"@celljs/web": "4.2.0"
|
|
42
38
|
},
|
|
43
39
|
"publishConfig": {
|
|
44
40
|
"access": "public"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { BehaviorSubject } from './behavior-subject';
|
|
4
|
+
|
|
5
|
+
describe('BehaviorSubject', () => {
|
|
6
|
+
it('gives the current value to a new subscriber and every next value after', () => {
|
|
7
|
+
const subject = new BehaviorSubject<string | undefined>(undefined);
|
|
8
|
+
const seen: (string | undefined)[] = [];
|
|
9
|
+
const subscription = subject.subscribe(v => seen.push(v));
|
|
10
|
+
subject.next('a');
|
|
11
|
+
subject.next('b');
|
|
12
|
+
subscription.unsubscribe();
|
|
13
|
+
subject.next('c');
|
|
14
|
+
expect(seen).to.deep.equal([undefined, 'a', 'b']);
|
|
15
|
+
expect(subject.value).to.equal('c');
|
|
16
|
+
expect(subject.getValue()).to.equal('c');
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface Subscription {
|
|
2
|
+
unsubscribe(): void;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A value that tells its subscribers when it changes, and tells a new subscriber the current value at once
|
|
7
|
+
* (the part of the rxjs `BehaviorSubject` that Cell uses).
|
|
8
|
+
*/
|
|
9
|
+
export class BehaviorSubject<T> {
|
|
10
|
+
|
|
11
|
+
protected readonly listeners = new Set<(value: T) => void>();
|
|
12
|
+
|
|
13
|
+
constructor(protected current: T) {
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get value(): T {
|
|
17
|
+
return this.current;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getValue(): T {
|
|
21
|
+
return this.current;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
next(value: T): void {
|
|
25
|
+
this.current = value;
|
|
26
|
+
for (const listener of [...this.listeners]) {
|
|
27
|
+
listener(value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
subscribe(listener: (value: T) => void): Subscription {
|
|
32
|
+
this.listeners.add(listener);
|
|
33
|
+
listener(this.current);
|
|
34
|
+
return { unsubscribe: () => this.listeners.delete(listener) };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -2,8 +2,7 @@ import { UserManager } from './user-protocol';
|
|
|
2
2
|
import { Autowired, Component, Value, PostConstruct } from '@celljs/core';
|
|
3
3
|
import { RestOperations, HttpMethod, HttpStatus, HttpHeaders } from '@celljs/http';
|
|
4
4
|
import { PathResolver } from '@celljs/web';
|
|
5
|
-
import {
|
|
6
|
-
import { BehaviorSubject } from 'rxjs';
|
|
5
|
+
import { BehaviorSubject } from './behavior-subject';
|
|
7
6
|
import { User } from '../../common';
|
|
8
7
|
|
|
9
8
|
@Component(UserManager)
|
|
@@ -88,8 +87,7 @@ export class UserManagerImpl implements UserManager {
|
|
|
88
87
|
|
|
89
88
|
async logout(): Promise<void> {
|
|
90
89
|
this.userInfoSubject.next(undefined);
|
|
91
|
-
|
|
92
|
-
await this.restOperations.request({ url: this.logoutUrl, method });
|
|
90
|
+
await this.restOperations.request({ url: this.logoutUrl, method: this.logoutMethod || HttpMethod.POST });
|
|
93
91
|
window.location.href = await this.pathResolver.resolve(this.logoutSuccessUrl.replace(/{redirect}/g, encodeURI(window.location.href)));
|
|
94
92
|
}
|
|
95
93
|
|
|
@@ -100,4 +98,4 @@ export class UserManagerImpl implements UserManager {
|
|
|
100
98
|
return data;
|
|
101
99
|
}
|
|
102
100
|
|
|
103
|
-
}
|
|
101
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { User } from '../../common';
|
|
2
|
-
import { BehaviorSubject } from '
|
|
2
|
+
import { BehaviorSubject } from './behavior-subject';
|
|
3
3
|
|
|
4
4
|
export const UserManager = Symbol('UserManager');
|
|
5
5
|
|
|
@@ -8,4 +8,4 @@ export interface UserManager {
|
|
|
8
8
|
logout(): Promise<void>;
|
|
9
9
|
getUserInfo(): Promise<User | undefined>;
|
|
10
10
|
userInfoSubject: BehaviorSubject<User | undefined>;
|
|
11
|
-
}
|
|
11
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it } from 'bun:test';
|
|
2
2
|
import { expect } from 'chai';
|
|
3
|
-
import {
|
|
3
|
+
import { ExpressionEngine } from '@celljs/core';
|
|
4
4
|
import { Resource } from '../annotation/resource';
|
|
5
5
|
import { ACCESS_DENIED, ACCESS_GRANTED } from './access-protocol';
|
|
6
6
|
import { ElPolicyResolver } from './policy-resolver';
|
|
@@ -18,10 +18,10 @@ class ItemService {
|
|
|
18
18
|
get(_id: string) {}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
describe('security expressions (
|
|
21
|
+
describe('security expressions (ExpressionEngine of @celljs/core)', () => {
|
|
22
22
|
it('ElPolicyResolver grants when the expression is true', async () => {
|
|
23
23
|
const resolver = new ElPolicyResolver();
|
|
24
|
-
Object.defineProperty(resolver, '
|
|
24
|
+
Object.defineProperty(resolver, 'engine', { value: new ExpressionEngine() });
|
|
25
25
|
const policy = (el: string) => ({ type: PolicyType.el, el, context: { user: { name: 'tom', roles: ['admin'] } } }) as any;
|
|
26
26
|
expect(await resolver.resolve(policy("user.name == 'tom' && 'admin' in user.roles"), {} as any)).to.equal(ACCESS_GRANTED);
|
|
27
27
|
expect(await resolver.resolve(policy("user.name == 'bob'"), {} as any)).to.equal(ACCESS_DENIED);
|
|
@@ -29,9 +29,9 @@ describe('security expressions (jexl of @celljs/core)', () => {
|
|
|
29
29
|
|
|
30
30
|
it('DefaultResourceNameResolver evaluates method resources against the call', async () => {
|
|
31
31
|
const resolver = new DefaultResourceNameResolver();
|
|
32
|
-
Object.assign(resolver, { tenantEnabled: false,
|
|
32
|
+
Object.assign(resolver, { tenantEnabled: false, engine: new ExpressionEngine() });
|
|
33
33
|
const target = new OrderService();
|
|
34
34
|
expect(await resolver.resolve({ target, method: 'get', args: [{ id: '42' }] } as any)).to.deep.equal(['order:42']);
|
|
35
35
|
expect(await resolver.resolve({ target: new ItemService(), method: 'get', args: ['7'] } as any)).to.deep.equal(['item:7']);
|
|
36
36
|
});
|
|
37
|
-
});
|
|
37
|
+
});
|
package/src/node/access/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MethodBeforeAdvice, Autowired, Aspect, AfterReturningAdvice, Value, ConfigUtil
|
|
1
|
+
import { MethodBeforeAdvice, Autowired, Aspect, AfterReturningAdvice, Value, ConfigUtil } from '@celljs/core';
|
|
2
2
|
import { AccessDecisionManager, MethodSecurityMetadataContext, ResourceNameResolver, SecurityMetadataSource, SECURITY_EXPRESSION_CONTEXT_KEY } from './access-protocol';
|
|
3
3
|
import { AOP_POINTCUT } from '@celljs/web';
|
|
4
4
|
import { SecurityContext } from '../context';
|
|
@@ -7,7 +7,6 @@ import { Context } from '@celljs/web/lib/node';
|
|
|
7
7
|
|
|
8
8
|
const pointcut = ConfigUtil.getRaw().cell.security.aop?.pointcut || AOP_POINTCUT;
|
|
9
9
|
|
|
10
|
-
@Injectable()
|
|
11
10
|
export abstract class AbstractSecurityMethodAdivice {
|
|
12
11
|
|
|
13
12
|
@Autowired(AccessDecisionManager)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { PatternUtil } from './pattern-util';
|
|
4
|
+
|
|
5
|
+
describe('PatternUtil.contains', () => {
|
|
6
|
+
// What micromatch.contains(text, pattern) returned.
|
|
7
|
+
const cases: [string, string, boolean][] = [
|
|
8
|
+
['order', 'order', true],
|
|
9
|
+
['order:1', 'order:*', true],
|
|
10
|
+
['tenant:order:1', 'order:*', true],
|
|
11
|
+
['order:1', '*', true],
|
|
12
|
+
['a/b/c', 'a/*', true],
|
|
13
|
+
['a/b/c', 'a/**', true],
|
|
14
|
+
['abc', 'a?c', true],
|
|
15
|
+
['abc', 'b', true],
|
|
16
|
+
['ord', 'order', false],
|
|
17
|
+
['Order', 'order', false],
|
|
18
|
+
['order:1', 'order:[0-9]', true],
|
|
19
|
+
['order:x', 'order:{x,y}', true],
|
|
20
|
+
['', '*', false],
|
|
21
|
+
['x', '', false],
|
|
22
|
+
['user:read', 'order:*', false],
|
|
23
|
+
['a/b', 'a*b', false],
|
|
24
|
+
['ab', 'a**b', true],
|
|
25
|
+
['a.b', 'a?b', true],
|
|
26
|
+
['axb', 'a.b', false]
|
|
27
|
+
];
|
|
28
|
+
for (const [text, pattern, expected] of cases) {
|
|
29
|
+
it(`${pattern} in ${JSON.stringify(text)}`, () => {
|
|
30
|
+
expect(PatternUtil.contains(text, pattern)).to.equal(expected);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
function escapeRegExp(text: string): string {
|
|
2
|
+
return text.replace(/[-/\\^$+.()|{}[\]]/g, '\\$&');
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function compile(pattern: string): string {
|
|
6
|
+
let source = '';
|
|
7
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
8
|
+
const c = pattern[i];
|
|
9
|
+
if (c === '\\' && i + 1 < pattern.length) {
|
|
10
|
+
source += escapeRegExp(pattern[++i]);
|
|
11
|
+
} else if (c === '*') {
|
|
12
|
+
if (pattern[i + 1] === '*') {
|
|
13
|
+
source += '.*';
|
|
14
|
+
i++;
|
|
15
|
+
} else {
|
|
16
|
+
source += '[^/]*';
|
|
17
|
+
}
|
|
18
|
+
} else if (c === '?') {
|
|
19
|
+
source += '[^/]';
|
|
20
|
+
} else if (c === '[') {
|
|
21
|
+
const end = pattern.indexOf(']', i + 2);
|
|
22
|
+
if (end < 0) {
|
|
23
|
+
source += '\\[';
|
|
24
|
+
} else {
|
|
25
|
+
const set = pattern.slice(i + 1, end).replace(/\\/g, '\\\\');
|
|
26
|
+
source += set[0] === '!' ? `[^${set.slice(1)}]` : `[${set}]`;
|
|
27
|
+
i = end;
|
|
28
|
+
}
|
|
29
|
+
} else if (c === '{') {
|
|
30
|
+
const end = pattern.indexOf('}', i + 1);
|
|
31
|
+
const alternatives = end < 0 ? undefined : pattern.slice(i + 1, end).split(',');
|
|
32
|
+
if (!alternatives || alternatives.length < 2) {
|
|
33
|
+
source += '\\{';
|
|
34
|
+
} else {
|
|
35
|
+
source += `(?:${alternatives.map(compile).join('|')})`;
|
|
36
|
+
i = end;
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
source += escapeRegExp(c);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return source;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Compiled patterns; they come from the policies of the application, so there are few. */
|
|
46
|
+
const CACHE = new Map<string, RegExp>();
|
|
47
|
+
|
|
48
|
+
export namespace PatternUtil {
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whether a glob pattern matches a part of `text`, as `micromatch.contains`: `order:*` matches `order:1` and
|
|
52
|
+
* `tenant:order:1`; `*` matches any text that is not empty. `*` stops at `/`, `**` does not; `?` is one
|
|
53
|
+
* character; `[0-9]` and `{a,b}` work as in globs. Matching is case-sensitive.
|
|
54
|
+
*/
|
|
55
|
+
export function contains(text: string, pattern: string): boolean {
|
|
56
|
+
if (!text || !pattern) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (text.includes(pattern)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
let regex = CACHE.get(pattern);
|
|
63
|
+
if (!regex) {
|
|
64
|
+
regex = new RegExp(compile(pattern));
|
|
65
|
+
if (CACHE.size < 1000) {
|
|
66
|
+
CACHE.set(pattern, regex);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return regex.test(text);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PolicyContext, PolicyProvider } from './access-protocol';
|
|
2
2
|
import { Component, Value, PostConstruct } from '@celljs/core';
|
|
3
3
|
import { AuthorizeType, Policy } from '../../common';
|
|
4
|
-
import {
|
|
4
|
+
import { PatternUtil } from './pattern-util';
|
|
5
5
|
|
|
6
6
|
@Component(PolicyProvider)
|
|
7
7
|
export class PrincipalPolicyProvider implements PolicyProvider {
|
|
@@ -55,10 +55,10 @@ export class ResourcePolicyProvider implements PolicyProvider {
|
|
|
55
55
|
const map = this.metadata[type];
|
|
56
56
|
const policies: Policy[] = [];
|
|
57
57
|
for (const pattarn of map.keys()) {
|
|
58
|
-
if (contains(resource, pattarn)) {
|
|
58
|
+
if (PatternUtil.contains(resource, pattarn)) {
|
|
59
59
|
policies.push(...map.get(pattarn)!);
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
return policies;
|
|
63
63
|
}
|
|
64
|
-
}
|
|
64
|
+
}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { ACCESS_ABSTAIN, ACCESS_DENIED, ACCESS_GRANTED, PolicyResolver, SecurityMetadata, SECURITY_EXPRESSION_CONTEXT_KEY } from './access-protocol';
|
|
2
|
-
import { Autowired, Component,
|
|
2
|
+
import { Autowired, Component, ExpressionEngine } from '@celljs/core';
|
|
3
3
|
import { Context } from '@celljs/web/lib/node';
|
|
4
4
|
import { AclPolicy, Effect, ElPolicy, Policy, PolicyType, Statement } from '../../common';
|
|
5
|
-
import {
|
|
5
|
+
import { PatternUtil } from './pattern-util';
|
|
6
6
|
|
|
7
7
|
@Component(PolicyResolver)
|
|
8
8
|
export class ElPolicyResolver implements PolicyResolver {
|
|
9
9
|
|
|
10
|
-
@Autowired(
|
|
11
|
-
protected readonly
|
|
10
|
+
@Autowired(ExpressionEngine)
|
|
11
|
+
protected readonly engine: ExpressionEngine;
|
|
12
12
|
|
|
13
13
|
async resolve(policy: ElPolicy, securityMetadata: SecurityMetadata): Promise<number> {
|
|
14
|
-
const
|
|
15
|
-
// eslint-disable-next-line no-eval
|
|
16
|
-
const result = await expression.eval(policy.context || Context.getAttr(SECURITY_EXPRESSION_CONTEXT_KEY));
|
|
14
|
+
const result = this.engine.evaluate(policy.el, policy.context || Context.getAttr(SECURITY_EXPRESSION_CONTEXT_KEY));
|
|
17
15
|
if (result === true) {
|
|
18
16
|
return ACCESS_GRANTED;
|
|
19
17
|
}
|
|
@@ -29,7 +27,7 @@ export class ElPolicyResolver implements PolicyResolver {
|
|
|
29
27
|
export class AclPolicyResolver implements PolicyResolver {
|
|
30
28
|
|
|
31
29
|
protected match(pattern: string, resource: string) {
|
|
32
|
-
return contains(resource, pattern);
|
|
30
|
+
return PatternUtil.contains(resource, pattern);
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
protected toArray(value: string | string[]) {
|
|
@@ -76,4 +74,4 @@ export class AclPolicyResolver implements PolicyResolver {
|
|
|
76
74
|
async support(policy: Policy, securityMetadata: SecurityMetadata): Promise<boolean> {
|
|
77
75
|
return policy.type === PolicyType.acl;
|
|
78
76
|
}
|
|
79
|
-
}
|
|
77
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MethodSecurityMetadataContext, ResourceNameResolver } from './access-protocol';
|
|
2
|
-
import { Component, Value, Autowired, TENANT_ENABLED, getOwnMetadata,
|
|
2
|
+
import { Component, Value, Autowired, TENANT_ENABLED, getOwnMetadata, ExpressionEngine } from '@celljs/core';
|
|
3
3
|
import { METADATA_KEY } from '../constants';
|
|
4
4
|
import { Context } from '@celljs/web/lib/node';
|
|
5
5
|
|
|
@@ -9,11 +9,11 @@ export class DefaultResourceNameResolver implements ResourceNameResolver {
|
|
|
9
9
|
@Value(TENANT_ENABLED)
|
|
10
10
|
protected tenantEnabled: boolean;
|
|
11
11
|
|
|
12
|
-
@Autowired(
|
|
13
|
-
protected readonly
|
|
12
|
+
@Autowired(ExpressionEngine)
|
|
13
|
+
protected readonly engine: ExpressionEngine;
|
|
14
14
|
|
|
15
|
-
protected
|
|
16
|
-
return this.
|
|
15
|
+
protected evaluate(expression: string, ctx: MethodSecurityMetadataContext): any {
|
|
16
|
+
return this.engine.evaluate(expression, ctx);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
async resolve(ctx: MethodSecurityMetadataContext): Promise<string[]> {
|
|
@@ -22,12 +22,10 @@ export class DefaultResourceNameResolver implements ResourceNameResolver {
|
|
|
22
22
|
let resources: string[] = [];
|
|
23
23
|
if (classResources.length && methodResources.length) {
|
|
24
24
|
for (const resource of methodResources) {
|
|
25
|
-
|
|
26
|
-
resources.push(`${classResources[0]}:${this.evalSync(resource?.toString(), ctx)}`);
|
|
25
|
+
resources.push(`${classResources[0]}:${this.evaluate(resource?.toString(), ctx)}`);
|
|
27
26
|
}
|
|
28
27
|
} else if (methodResources.length) {
|
|
29
|
-
|
|
30
|
-
resources = methodResources.map(r => this.evalSync(r?.toString(), ctx)?.toString());
|
|
28
|
+
resources = methodResources.map(r => this.evaluate(r?.toString(), ctx)?.toString());
|
|
31
29
|
} else if (classResources.length) {
|
|
32
30
|
resources = classResources;
|
|
33
31
|
} else {
|
|
@@ -44,4 +42,4 @@ export class DefaultResourceNameResolver implements ResourceNameResolver {
|
|
|
44
42
|
}
|
|
45
43
|
return resources;
|
|
46
44
|
}
|
|
47
|
-
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { UrlUtil } from '@celljs/core';
|
|
4
|
+
import { HttpSessionRequestCache } from './request-cache';
|
|
5
|
+
|
|
6
|
+
describe('HttpSessionRequestCache', () => {
|
|
7
|
+
it('keeps the query of the saved request as the client sent it', () => {
|
|
8
|
+
const cache = new HttpSessionRequestCache() as unknown as { queryString(url?: string): string };
|
|
9
|
+
expect(cache.queryString('/orders?id=1&tag=a&tag=b')).to.equal('?id=1&tag=a&tag=b');
|
|
10
|
+
expect(cache.queryString('/orders?')).to.equal('');
|
|
11
|
+
expect(cache.queryString('/orders')).to.equal('');
|
|
12
|
+
expect(UrlUtil.join('http://localhost:3000', '/orders', cache.queryString('/orders?id=1'))).to.equal('http://localhost:3000/orders?id=1');
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -2,7 +2,6 @@ import { RequestCache, SavedRequest, SAVED_REQUEST } from './cache-protocol';
|
|
|
2
2
|
import { AttributeScope, Context } from '@celljs/web/lib/node';
|
|
3
3
|
import { Component, Value, Autowired } from '@celljs/core';
|
|
4
4
|
import { ENDPOINT, PathResolver } from '@celljs/web';
|
|
5
|
-
import * as qs from 'qs';
|
|
6
5
|
|
|
7
6
|
@Component(RequestCache)
|
|
8
7
|
export class HttpSessionRequestCache implements RequestCache {
|
|
@@ -17,12 +16,18 @@ export class HttpSessionRequestCache implements RequestCache {
|
|
|
17
16
|
if (Context.getSession()) {
|
|
18
17
|
const request = Context.getRequest();
|
|
19
18
|
Context.setAttr(SAVED_REQUEST, savedRequest || {
|
|
20
|
-
redirectUrl: await this.pathResolver.resolve(this.endpoint, request.path,
|
|
19
|
+
redirectUrl: await this.pathResolver.resolve(this.endpoint, request.path, this.queryString(request.originalUrl ?? request.url)),
|
|
21
20
|
method: request.method.toUpperCase(),
|
|
22
21
|
query: { ...request.query }
|
|
23
22
|
}, AttributeScope.Session);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
25
|
+
/** `?a=1` of the URL as the client sent it, or `''`. */
|
|
26
|
+
protected queryString(url = ''): string {
|
|
27
|
+
const index = url.indexOf('?');
|
|
28
|
+
return index < 0 || index === url.length - 1 ? '' : url.slice(index);
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
async get(): Promise<SavedRequest | undefined> {
|
|
27
32
|
if (Context.getSession()) {
|
|
28
33
|
return Context.getAttr<SavedRequest>(SAVED_REQUEST, AttributeScope.Session);
|
|
@@ -35,4 +40,4 @@ export class HttpSessionRequestCache implements RequestCache {
|
|
|
35
40
|
}
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
}
|
|
43
|
+
}
|