@celljs/security 4.0.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 +113 -1
- package/cell.yml +1 -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/access-protocol.d.ts +0 -4
- package/esm/node/access/access-protocol.js +0 -1
- package/esm/node/access/access-protocol.js.map +1 -1
- package/esm/node/access/index.d.ts +1 -1
- package/esm/node/access/index.js +1 -1
- 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 +3 -2
- package/esm/node/access/policy-resolver.js +8 -11
- package/esm/node/access/policy-resolver.js.map +1 -1
- package/esm/node/access/resource-name-resolver.d.ts +3 -0
- package/esm/node/access/resource-name-resolver.js +10 -7
- package/esm/node/access/resource-name-resolver.js.map +1 -1
- package/esm/node/authentication/authentication-provider.d.ts +8 -0
- package/esm/node/authentication/authentication-provider.js +29 -2
- package/esm/node/authentication/authentication-provider.js.map +1 -1
- package/esm/node/authentication/authentication-success-handler.js +4 -0
- package/esm/node/authentication/authentication-success-handler.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/crypto/keygen/base64-string-key-generator.d.ts +4 -0
- package/esm/node/crypto/keygen/base64-string-key-generator.js +6 -4
- package/esm/node/crypto/keygen/base64-string-key-generator.js.map +1 -1
- package/esm/node/crypto/password/password-encoder.d.ts +40 -3
- package/esm/node/crypto/password/password-encoder.js +80 -24
- package/esm/node/crypto/password/password-encoder.js.map +1 -1
- package/esm/node/crypto/password/password-protocol.d.ts +6 -0
- package/esm/node/module.d.ts +1 -2
- package/esm/node/module.js +0 -1
- package/esm/node/module.js.map +1 -1
- package/esm/node/user/user-protocol.d.ts +5 -0
- package/esm/node/user/user-service.d.ts +14 -0
- package/esm/node/user/user-service.js +33 -2
- package/esm/node/user/user-service.js.map +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/access-protocol.d.ts +0 -4
- package/lib/node/access/access-protocol.d.ts.map +1 -1
- package/lib/node/access/access-protocol.js +1 -2
- package/lib/node/access/access-protocol.js.map +1 -1
- package/lib/node/access/expression-resolvers.spec.d.ts +2 -0
- package/lib/node/access/expression-resolvers.spec.d.ts.map +1 -0
- package/lib/node/access/expression-resolvers.spec.js +57 -0
- package/lib/node/access/expression-resolvers.spec.js.map +1 -0
- package/lib/node/access/index.d.ts +1 -1
- package/lib/node/access/index.d.ts.map +1 -1
- package/lib/node/access/index.js +1 -1
- 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 +3 -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 -0
- package/lib/node/access/resource-name-resolver.d.ts.map +1 -1
- package/lib/node/access/resource-name-resolver.js +9 -5
- package/lib/node/access/resource-name-resolver.js.map +1 -1
- package/lib/node/authentication/authentication-provider.d.ts +8 -0
- package/lib/node/authentication/authentication-provider.d.ts.map +1 -1
- package/lib/node/authentication/authentication-provider.js +29 -1
- package/lib/node/authentication/authentication-provider.js.map +1 -1
- package/lib/node/authentication/authentication-success-handler.d.ts.map +1 -1
- package/lib/node/authentication/authentication-success-handler.js +4 -0
- package/lib/node/authentication/authentication-success-handler.js.map +1 -1
- package/lib/node/authentication/basic-authentication.spec.d.ts +2 -0
- package/lib/node/authentication/basic-authentication.spec.d.ts.map +1 -0
- package/lib/node/authentication/basic-authentication.spec.js +65 -0
- package/lib/node/authentication/basic-authentication.spec.js.map +1 -0
- 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/crypto/keygen/base64-string-key-generator.d.ts +4 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.d.ts.map +1 -1
- package/lib/node/crypto/keygen/base64-string-key-generator.js +6 -3
- package/lib/node/crypto/keygen/base64-string-key-generator.js.map +1 -1
- package/lib/node/crypto/keygen/base64-string-key-generator.spec.d.ts +2 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.spec.d.ts.map +1 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.spec.js +15 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.spec.js.map +1 -0
- package/lib/node/crypto/password/password-encoder.d.ts +40 -3
- package/lib/node/crypto/password/password-encoder.d.ts.map +1 -1
- package/lib/node/crypto/password/password-encoder.js +80 -23
- package/lib/node/crypto/password/password-encoder.js.map +1 -1
- package/lib/node/crypto/password/password-encoder.spec.d.ts +2 -0
- package/lib/node/crypto/password/password-encoder.spec.d.ts.map +1 -0
- package/lib/node/crypto/password/password-encoder.spec.js +147 -0
- package/lib/node/crypto/password/password-encoder.spec.js.map +1 -0
- package/lib/node/crypto/password/password-protocol.d.ts +6 -0
- package/lib/node/crypto/password/password-protocol.d.ts.map +1 -1
- package/lib/node/module.d.ts +1 -2
- package/lib/node/module.d.ts.map +1 -1
- package/lib/node/module.js +0 -1
- package/lib/node/module.js.map +1 -1
- package/lib/node/user/user-protocol.d.ts +5 -0
- package/lib/node/user/user-protocol.d.ts.map +1 -1
- package/lib/node/user/user-service.d.ts +14 -0
- package/lib/node/user/user-service.d.ts.map +1 -1
- package/lib/node/user/user-service.js +33 -2
- package/lib/node/user/user-service.js.map +1 -1
- package/lib/node/user/user-service.spec.d.ts +2 -0
- package/lib/node/user/user-service.spec.d.ts.map +1 -0
- package/lib/node/user/user-service.spec.js +44 -0
- package/lib/node/user/user-service.spec.js.map +1 -0
- package/package.json +7 -17
- 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/access-protocol.ts +0 -5
- package/src/node/access/expression-resolvers.spec.ts +37 -0
- package/src/node/access/index.ts +1 -1
- 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 +8 -10
- package/src/node/access/resource-name-resolver.ts +11 -7
- package/src/node/authentication/authentication-provider.ts +26 -2
- package/src/node/authentication/authentication-success-handler.ts +4 -0
- package/src/node/authentication/basic-authentication.spec.ts +68 -0
- package/src/node/cache/request-cache.spec.ts +14 -0
- package/src/node/cache/request-cache.ts +8 -3
- package/src/node/crypto/keygen/base64-string-key-generator.spec.ts +13 -0
- package/src/node/crypto/keygen/base64-string-key-generator.ts +6 -3
- package/src/node/crypto/password/password-encoder.spec.ts +162 -0
- package/src/node/crypto/password/password-encoder.ts +99 -21
- package/src/node/crypto/password/password-protocol.ts +6 -0
- package/src/node/module.ts +0 -1
- package/src/node/user/user-protocol.ts +5 -0
- package/src/node/user/user-service.spec.ts +46 -0
- package/src/node/user/user-service.ts +32 -2
- package/esm/node/access/jexl-engine-provider.d.ts +0 -5
- package/esm/node/access/jexl-engine-provider.js +0 -22
- package/esm/node/access/jexl-engine-provider.js.map +0 -1
- package/lib/node/access/jexl-engine-provider.d.ts +0 -6
- package/lib/node/access/jexl-engine-provider.d.ts.map +0 -1
- package/lib/node/access/jexl-engine-provider.js +0 -25
- package/lib/node/access/jexl-engine-provider.js.map +0 -1
- package/src/node/access/jexl-engine-provider.ts +0 -18
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,22 +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.26.1",
|
|
36
|
-
"crypto-js": "^4.0.0",
|
|
37
|
-
"jexl": "^2.2.0",
|
|
38
|
-
"micromatch": "^4.0.2",
|
|
39
|
-
"qs": "^6.9.4",
|
|
40
|
-
"reflect-metadata": "^0.1.13",
|
|
41
|
-
"rxjs": "^6.6.0",
|
|
42
|
-
"tslib": "^2.8.0"
|
|
43
|
-
},
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"@types/crypto-js": "^3.1.47",
|
|
46
|
-
"@types/jexl": "^2.2.0",
|
|
47
|
-
"@types/micromatch": "^4.0.1"
|
|
35
|
+
"@celljs/core": "4.2.0",
|
|
36
|
+
"@celljs/http": "4.2.0",
|
|
37
|
+
"@celljs/web": "4.2.0"
|
|
48
38
|
},
|
|
49
39
|
"publishConfig": {
|
|
50
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
|
+
}
|
|
@@ -5,7 +5,6 @@ export const AccessDecisionVoter = Symbol('AccessDecisionVoter');
|
|
|
5
5
|
export const SecurityMetadataSource = Symbol('SecurityMetadataSource');
|
|
6
6
|
export const PolicyResolver = Symbol('PolicyResolver');
|
|
7
7
|
export const SecurityExpressionContextHandler = Symbol('SecurityExpressionContextHandler');
|
|
8
|
-
export const JexlEngineProvider = Symbol('JexlEngineProvider');
|
|
9
8
|
export const PolicyProvider = Symbol('PolicyProvider');
|
|
10
9
|
export const PolicyFactory = Symbol('PolicyFactory');
|
|
11
10
|
export const ResourceNameResolver = Symbol('ResourceNameResolver');
|
|
@@ -40,10 +39,6 @@ export interface PolicyResolver {
|
|
|
40
39
|
support(policy: Policy, securityMetadata: SecurityMetadata): Promise<boolean>;
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
export interface JexlEngineProvider<T> {
|
|
44
|
-
provide(): T
|
|
45
|
-
}
|
|
46
|
-
|
|
47
42
|
export interface SecurityExpressionContextHandler {
|
|
48
43
|
handle(context: any): Promise<void>
|
|
49
44
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { ExpressionEngine } from '@celljs/core';
|
|
4
|
+
import { Resource } from '../annotation/resource';
|
|
5
|
+
import { ACCESS_DENIED, ACCESS_GRANTED } from './access-protocol';
|
|
6
|
+
import { ElPolicyResolver } from './policy-resolver';
|
|
7
|
+
import { DefaultResourceNameResolver } from './resource-name-resolver';
|
|
8
|
+
import { PolicyType } from '../../common';
|
|
9
|
+
|
|
10
|
+
@Resource('order')
|
|
11
|
+
class OrderService {
|
|
12
|
+
@Resource('args[0].id')
|
|
13
|
+
get(_order: { id: string }) {}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class ItemService {
|
|
17
|
+
@Resource("'item:' + args[0]")
|
|
18
|
+
get(_id: string) {}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe('security expressions (ExpressionEngine of @celljs/core)', () => {
|
|
22
|
+
it('ElPolicyResolver grants when the expression is true', async () => {
|
|
23
|
+
const resolver = new ElPolicyResolver();
|
|
24
|
+
Object.defineProperty(resolver, 'engine', { value: new ExpressionEngine() });
|
|
25
|
+
const policy = (el: string) => ({ type: PolicyType.el, el, context: { user: { name: 'tom', roles: ['admin'] } } }) as any;
|
|
26
|
+
expect(await resolver.resolve(policy("user.name == 'tom' && 'admin' in user.roles"), {} as any)).to.equal(ACCESS_GRANTED);
|
|
27
|
+
expect(await resolver.resolve(policy("user.name == 'bob'"), {} as any)).to.equal(ACCESS_DENIED);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('DefaultResourceNameResolver evaluates method resources against the call', async () => {
|
|
31
|
+
const resolver = new DefaultResourceNameResolver();
|
|
32
|
+
Object.assign(resolver, { tenantEnabled: false, engine: new ExpressionEngine() });
|
|
33
|
+
const target = new OrderService();
|
|
34
|
+
expect(await resolver.resolve({ target, method: 'get', args: [{ id: '42' }] } as any)).to.deep.equal(['order:42']);
|
|
35
|
+
expect(await resolver.resolve({ target: new ItemService(), method: 'get', args: ['7'] } as any)).to.deep.equal(['item:7']);
|
|
36
|
+
});
|
|
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
|
-
import { ACCESS_ABSTAIN, ACCESS_DENIED, ACCESS_GRANTED,
|
|
2
|
-
import { Autowired, Component } from '@celljs/core';
|
|
1
|
+
import { ACCESS_ABSTAIN, ACCESS_DENIED, ACCESS_GRANTED, PolicyResolver, SecurityMetadata, SECURITY_EXPRESSION_CONTEXT_KEY } from './access-protocol';
|
|
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,7 +1,6 @@
|
|
|
1
1
|
import { MethodSecurityMetadataContext, ResourceNameResolver } from './access-protocol';
|
|
2
|
-
import { Component, Value, TENANT_ENABLED, getOwnMetadata } from '@celljs/core';
|
|
2
|
+
import { Component, Value, Autowired, TENANT_ENABLED, getOwnMetadata, ExpressionEngine } from '@celljs/core';
|
|
3
3
|
import { METADATA_KEY } from '../constants';
|
|
4
|
-
import { evalSync } from 'jexl';
|
|
5
4
|
import { Context } from '@celljs/web/lib/node';
|
|
6
5
|
|
|
7
6
|
@Component(ResourceNameResolver)
|
|
@@ -10,18 +9,23 @@ export class DefaultResourceNameResolver implements ResourceNameResolver {
|
|
|
10
9
|
@Value(TENANT_ENABLED)
|
|
11
10
|
protected tenantEnabled: boolean;
|
|
12
11
|
|
|
12
|
+
@Autowired(ExpressionEngine)
|
|
13
|
+
protected readonly engine: ExpressionEngine;
|
|
14
|
+
|
|
15
|
+
protected evaluate(expression: string, ctx: MethodSecurityMetadataContext): any {
|
|
16
|
+
return this.engine.evaluate(expression, ctx);
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
async resolve(ctx: MethodSecurityMetadataContext): Promise<string[]> {
|
|
14
20
|
const classResources: string[] = getOwnMetadata(METADATA_KEY.resource, ctx.target.constructor);
|
|
15
21
|
const methodResources: string[] = getOwnMetadata(METADATA_KEY.resource, ctx.target.constructor, ctx.method);
|
|
16
22
|
let resources: string[] = [];
|
|
17
23
|
if (classResources.length && methodResources.length) {
|
|
18
24
|
for (const resource of methodResources) {
|
|
19
|
-
|
|
20
|
-
resources.push(`${classResources[0]}:${evalSync(resource?.toString(), ctx)}`);
|
|
25
|
+
resources.push(`${classResources[0]}:${this.evaluate(resource?.toString(), ctx)}`);
|
|
21
26
|
}
|
|
22
27
|
} else if (methodResources.length) {
|
|
23
|
-
|
|
24
|
-
resources = methodResources.map(r => evalSync(r?.toString(), ctx)?.toString());
|
|
28
|
+
resources = methodResources.map(r => this.evaluate(r?.toString(), ctx)?.toString());
|
|
25
29
|
} else if (classResources.length) {
|
|
26
30
|
resources = classResources;
|
|
27
31
|
} else {
|
|
@@ -38,4 +42,4 @@ export class DefaultResourceNameResolver implements ResourceNameResolver {
|
|
|
38
42
|
}
|
|
39
43
|
return resources;
|
|
40
44
|
}
|
|
41
|
-
}
|
|
45
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Component, Autowired, Value } from '@celljs/core';
|
|
1
|
+
import { Component, Autowired, Value, Logger } from '@celljs/core';
|
|
2
2
|
import { AuthenticationProvider, Authentication, USERNAME_PASSWORD_AUTHENTICATION_PROVIDER_PRIORITY,
|
|
3
3
|
BASE_AUTHENTICATION_PROVIDER_PRIORITY, AUTHENTICATION_SCHEME_BASIC } from './authentication-protocol';
|
|
4
4
|
import { Context, RequestMatcher, RedirectStrategy } from '@celljs/web/lib/node';
|
|
@@ -10,6 +10,22 @@ import { PathResolver } from '@celljs/web';
|
|
|
10
10
|
|
|
11
11
|
import { User } from '../../common';
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* After a password matched: when its stored hash is in a legacy format and the UserService can store
|
|
15
|
+
* passwords, store a new hash of it. A failure is logged and does not fail the login.
|
|
16
|
+
*/
|
|
17
|
+
export async function upgradePasswordHash(passwordEncoder: PasswordEncoder, userService: UserService<any, User>, user: User,
|
|
18
|
+
rawPassword: string, logger: Logger): Promise<void> {
|
|
19
|
+
if (!userService.updatePassword || !passwordEncoder.needsUpgrade?.(user.password)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
await userService.updatePassword(user, await passwordEncoder.encode(rawPassword));
|
|
24
|
+
} catch (error) {
|
|
25
|
+
logger.warn(`Could not upgrade the password hash of ${user.username}: ${(error as Error)?.message ?? error}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
13
29
|
@Component(AuthenticationProvider)
|
|
14
30
|
export class UsernamePasswordAuthenticationProvider implements AuthenticationProvider {
|
|
15
31
|
|
|
@@ -37,6 +53,9 @@ export class UsernamePasswordAuthenticationProvider implements AuthenticationPro
|
|
|
37
53
|
@Autowired(RedirectStrategy)
|
|
38
54
|
protected readonly redirectStrategy: RedirectStrategy;
|
|
39
55
|
|
|
56
|
+
@Autowired(Logger)
|
|
57
|
+
protected readonly logger: Logger;
|
|
58
|
+
|
|
40
59
|
priority = USERNAME_PASSWORD_AUTHENTICATION_PROVIDER_PRIORITY;
|
|
41
60
|
|
|
42
61
|
async authenticate(): Promise<Authentication | undefined> {
|
|
@@ -50,6 +69,7 @@ export class UsernamePasswordAuthenticationProvider implements AuthenticationPro
|
|
|
50
69
|
if (!await this.passwordEncoder.matches(password, user.password)) {
|
|
51
70
|
throw new BadCredentialsError('Bad credentials');
|
|
52
71
|
}
|
|
72
|
+
await upgradePasswordHash(this.passwordEncoder, this.userService, user, password, this.logger);
|
|
53
73
|
await this.userMapper.map(user);
|
|
54
74
|
return {
|
|
55
75
|
name: user.username,
|
|
@@ -100,6 +120,9 @@ export class BasicAuthenticationProvider implements AuthenticationProvider {
|
|
|
100
120
|
@Autowired(RedirectStrategy)
|
|
101
121
|
protected readonly redirectStrategy: RedirectStrategy;
|
|
102
122
|
|
|
123
|
+
@Autowired(Logger)
|
|
124
|
+
protected readonly logger: Logger;
|
|
125
|
+
|
|
103
126
|
priority = BASE_AUTHENTICATION_PROVIDER_PRIORITY;
|
|
104
127
|
|
|
105
128
|
async authenticate(): Promise<Authentication | undefined> {
|
|
@@ -115,7 +138,7 @@ export class BasicAuthenticationProvider implements AuthenticationProvider {
|
|
|
115
138
|
throw new BadCredentialsError('Invalid basic authentication token');
|
|
116
139
|
}
|
|
117
140
|
const username = token.substring(0, delim);
|
|
118
|
-
const password = token.substring(delim);
|
|
141
|
+
const password = token.substring(delim + 1);
|
|
119
142
|
if (!password || !username) {
|
|
120
143
|
throw new BadCredentialsError('Bad credentials');
|
|
121
144
|
}
|
|
@@ -124,6 +147,7 @@ export class BasicAuthenticationProvider implements AuthenticationProvider {
|
|
|
124
147
|
if (!await this.passwordEncoder.matches(password, user.password)) {
|
|
125
148
|
throw new BadCredentialsError('Bad credentials');
|
|
126
149
|
}
|
|
150
|
+
await upgradePasswordHash(this.passwordEncoder, this.userService, user, password, this.logger);
|
|
127
151
|
|
|
128
152
|
await this.userMapper.map(user);
|
|
129
153
|
|
|
@@ -29,6 +29,10 @@ export class DefaultAuthenticationSuccessHandler implements AuthenticationSucces
|
|
|
29
29
|
protected readonly requestCache: RequestCache;
|
|
30
30
|
|
|
31
31
|
async onAuthenticationSuccess(authentication: Authentication): Promise<void> {
|
|
32
|
+
// Authentication that goes on to the requested resource (Basic): nothing to redirect to.
|
|
33
|
+
if (authentication.next) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
32
36
|
const response = Context.getResponse();
|
|
33
37
|
if (Context.getRequest().get(HttpHeaders.X_REQUESTED_WITH) === XML_HTTP_REQUEST) {
|
|
34
38
|
return;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { Context } from '@celljs/web/lib/node';
|
|
4
|
+
import { BadCredentialsError } from '../error';
|
|
5
|
+
import { BasicAuthenticationProvider } from './authentication-provider';
|
|
6
|
+
import { DefaultAuthenticationSuccessHandler } from './authentication-success-handler';
|
|
7
|
+
|
|
8
|
+
function inRequest<T>(authorization: string | undefined, fn: () => Promise<T>): Promise<T> {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
Context.run(async () => {
|
|
11
|
+
try {
|
|
12
|
+
const headers: Record<string, string> = authorization ? { authorization } : {};
|
|
13
|
+
Context.setCurrent(new Context({ headers, get: (name: string) => headers[name.toLowerCase()] } as any, {} as any));
|
|
14
|
+
resolve(await fn());
|
|
15
|
+
} catch (error) {
|
|
16
|
+
reject(error);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const basic = (user: string, password: string) => `Basic ${Buffer.from(`${user}:${password}`).toString('base64')}`;
|
|
23
|
+
|
|
24
|
+
function provider(): BasicAuthenticationProvider {
|
|
25
|
+
const instance = new BasicAuthenticationProvider();
|
|
26
|
+
Object.assign(instance, {
|
|
27
|
+
passwordEncoder: { matches: async (raw: string, encoded: string) => raw === encoded },
|
|
28
|
+
userService: { load: async (username: string) => ({ username, password: 'pa:ss', policies: [] }) },
|
|
29
|
+
userChecker: { check: async () => undefined },
|
|
30
|
+
userMapper: { map: async () => undefined },
|
|
31
|
+
logger: { warn: () => undefined },
|
|
32
|
+
});
|
|
33
|
+
return instance;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('BasicAuthenticationProvider', () => {
|
|
37
|
+
it('authenticates with the password after the first colon (which may contain colons)', async () => {
|
|
38
|
+
const result = await inRequest(basic('tom', 'pa:ss'), () => provider().authenticate());
|
|
39
|
+
expect(result?.authenticated).to.equal(true);
|
|
40
|
+
expect(result?.name).to.equal('tom');
|
|
41
|
+
// Basic goes on to the requested resource.
|
|
42
|
+
expect(result?.next).to.equal(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('rejects a wrong password', async () => {
|
|
46
|
+
let error: unknown;
|
|
47
|
+
await inRequest(basic('tom', 'wrong'), () => provider().authenticate()).catch(e => {
|
|
48
|
+
error = e;
|
|
49
|
+
});
|
|
50
|
+
expect(error).to.be.instanceOf(BadCredentialsError);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('is only used with a Basic authorization header', async () => {
|
|
54
|
+
expect(await inRequest(basic('tom', 'x'), () => provider().support())).to.equal(true);
|
|
55
|
+
expect(await inRequest('Bearer abc', () => provider().support())).to.equal(false);
|
|
56
|
+
expect(await inRequest(undefined, () => provider().support())).to.equal(false);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('DefaultAuthenticationSuccessHandler', () => {
|
|
61
|
+
it('does not redirect an authentication that goes on to the requested resource', async () => {
|
|
62
|
+
const handler = new DefaultAuthenticationSuccessHandler();
|
|
63
|
+
let redirected = false;
|
|
64
|
+
Object.assign(handler, { loginSuccessUrl: '/', redirectStrategy: { send: async () => { redirected = true; } } });
|
|
65
|
+
await inRequest(basic('tom', 'x'), () => handler.onAuthenticationSuccess({ next: true, authenticated: true } as any));
|
|
66
|
+
expect(redirected).to.equal(false);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { Base64StringKeyGenerator } from './base64-string-key-generator';
|
|
4
|
+
|
|
5
|
+
describe('Base64StringKeyGenerator', () => {
|
|
6
|
+
it('generates URL-safe keys of the requested number of random bytes', async () => {
|
|
7
|
+
const generator = Object.assign(new Base64StringKeyGenerator(), { keyLength: 32 });
|
|
8
|
+
const key = await generator.generateKey();
|
|
9
|
+
expect(key).to.match(/^[A-Za-z0-9_-]{43}$/);
|
|
10
|
+
expect(await generator.generateKey(96)).to.match(/^[A-Za-z0-9_-]{128}$/);
|
|
11
|
+
expect(await generator.generateKey()).to.not.equal(key);
|
|
12
|
+
});
|
|
13
|
+
});
|