@celljs/security 3.0.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/LICENSE +21 -0
- package/README.md +1 -0
- package/cell.yml +36 -0
- package/lib/browser/index.d.ts +2 -0
- package/lib/browser/index.d.ts.map +1 -0
- package/lib/browser/index.js +18 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/module.d.ts +4 -0
- package/lib/browser/module.d.ts.map +1 -0
- package/lib/browser/module.js +6 -0
- package/lib/browser/module.js.map +1 -0
- package/lib/browser/user/index.d.ts +3 -0
- package/lib/browser/user/index.d.ts.map +1 -0
- package/lib/browser/user/index.js +19 -0
- package/lib/browser/user/index.js.map +1 -0
- package/lib/browser/user/user-manager.d.ts +23 -0
- package/lib/browser/user/user-manager.d.ts.map +1 -0
- package/lib/browser/user/user-manager.js +125 -0
- package/lib/browser/user/user-manager.js.map +1 -0
- package/lib/browser/user/user-protocol.d.ts +10 -0
- package/lib/browser/user/user-protocol.d.ts.map +1 -0
- package/lib/browser/user/user-protocol.js +5 -0
- package/lib/browser/user/user-protocol.js.map +1 -0
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +18 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/security-protocol.d.ts +60 -0
- package/lib/common/security-protocol.d.ts.map +1 -0
- package/lib/common/security-protocol.js +33 -0
- package/lib/common/security-protocol.js.map +1 -0
- package/lib/node/access/access-decision-manager.d.ts +9 -0
- package/lib/node/access/access-decision-manager.d.ts.map +1 -0
- package/lib/node/access/access-decision-manager.js +56 -0
- package/lib/node/access/access-decision-manager.js.map +1 -0
- package/lib/node/access/access-decision-utils.d.ts +5 -0
- package/lib/node/access/access-decision-utils.d.ts.map +1 -0
- package/lib/node/access/access-decision-utils.js +23 -0
- package/lib/node/access/access-decision-utils.js.map +1 -0
- package/lib/node/access/access-decision-voter.d.ts +14 -0
- package/lib/node/access/access-decision-voter.d.ts.map +1 -0
- package/lib/node/access/access-decision-voter.js +86 -0
- package/lib/node/access/access-decision-voter.js.map +1 -0
- package/lib/node/access/access-protocol.d.ts +75 -0
- package/lib/node/access/access-protocol.d.ts.map +1 -0
- package/lib/node/access/access-protocol.js +20 -0
- package/lib/node/access/access-protocol.js.map +1 -0
- package/lib/node/access/action-name-resolver.d.ts +5 -0
- package/lib/node/access/action-name-resolver.d.ts.map +1 -0
- package/lib/node/access/action-name-resolver.js +30 -0
- package/lib/node/access/action-name-resolver.js.map +1 -0
- package/lib/node/access/index.d.ts +12 -0
- package/lib/node/access/index.d.ts.map +1 -0
- package/lib/node/access/index.js +28 -0
- package/lib/node/access/index.js.map +1 -0
- package/lib/node/access/jexl-engine-provider.d.ts +6 -0
- package/lib/node/access/jexl-engine-provider.d.ts.map +1 -0
- package/lib/node/access/jexl-engine-provider.js +25 -0
- package/lib/node/access/jexl-engine-provider.js.map +1 -0
- package/lib/node/access/method-advice.d.ts +17 -0
- package/lib/node/access/method-advice.d.ts.map +1 -0
- package/lib/node/access/method-advice.js +81 -0
- package/lib/node/access/method-advice.js.map +1 -0
- package/lib/node/access/policy-provider.d.ts +17 -0
- package/lib/node/access/policy-provider.d.ts.map +1 -0
- package/lib/node/access/policy-provider.js +84 -0
- package/lib/node/access/policy-provider.js.map +1 -0
- package/lib/node/access/policy-resolver.d.ts +15 -0
- package/lib/node/access/policy-resolver.d.ts.map +1 -0
- package/lib/node/access/policy-resolver.js +91 -0
- package/lib/node/access/policy-resolver.js.map +1 -0
- package/lib/node/access/resource-name-resolver.d.ts +7 -0
- package/lib/node/access/resource-name-resolver.d.ts.map +1 -0
- package/lib/node/access/resource-name-resolver.js +57 -0
- package/lib/node/access/resource-name-resolver.js.map +1 -0
- package/lib/node/access/security-metadata-source.d.ts +7 -0
- package/lib/node/access/security-metadata-source.d.ts.map +1 -0
- package/lib/node/access/security-metadata-source.js +58 -0
- package/lib/node/access/security-metadata-source.js.map +1 -0
- package/lib/node/annotation/action.d.ts +2 -0
- package/lib/node/annotation/action.d.ts.map +1 -0
- package/lib/node/annotation/action.js +11 -0
- package/lib/node/annotation/action.js.map +1 -0
- package/lib/node/annotation/anonymous.d.ts +2 -0
- package/lib/node/annotation/anonymous.d.ts.map +1 -0
- package/lib/node/annotation/anonymous.js +10 -0
- package/lib/node/annotation/anonymous.js.map +1 -0
- package/lib/node/annotation/authenticated.d.ts +2 -0
- package/lib/node/annotation/authenticated.d.ts.map +1 -0
- package/lib/node/annotation/authenticated.js +10 -0
- package/lib/node/annotation/authenticated.js.map +1 -0
- package/lib/node/annotation/authorize.d.ts +11 -0
- package/lib/node/annotation/authorize.d.ts.map +1 -0
- package/lib/node/annotation/authorize.js +40 -0
- package/lib/node/annotation/authorize.js.map +1 -0
- package/lib/node/annotation/index.d.ts +8 -0
- package/lib/node/annotation/index.d.ts.map +1 -0
- package/lib/node/annotation/index.js +24 -0
- package/lib/node/annotation/index.js.map +1 -0
- package/lib/node/annotation/post-authorize.d.ts +2 -0
- package/lib/node/annotation/post-authorize.d.ts.map +1 -0
- package/lib/node/annotation/post-authorize.js +10 -0
- package/lib/node/annotation/post-authorize.js.map +1 -0
- package/lib/node/annotation/pre-authorize.d.ts +2 -0
- package/lib/node/annotation/pre-authorize.d.ts.map +1 -0
- package/lib/node/annotation/pre-authorize.js +10 -0
- package/lib/node/annotation/pre-authorize.js.map +1 -0
- package/lib/node/annotation/resource.d.ts +2 -0
- package/lib/node/annotation/resource.d.ts.map +1 -0
- package/lib/node/annotation/resource.js +16 -0
- package/lib/node/annotation/resource.js.map +1 -0
- package/lib/node/authentication/authentication-manager.d.ts +10 -0
- package/lib/node/authentication/authentication-manager.d.ts.map +1 -0
- package/lib/node/authentication/authentication-manager.js +81 -0
- package/lib/node/authentication/authentication-manager.js.map +1 -0
- package/lib/node/authentication/authentication-middleware.d.ts +8 -0
- package/lib/node/authentication/authentication-middleware.d.ts.map +1 -0
- package/lib/node/authentication/authentication-middleware.js +36 -0
- package/lib/node/authentication/authentication-middleware.js.map +1 -0
- package/lib/node/authentication/authentication-protocol.d.ts +30 -0
- package/lib/node/authentication/authentication-protocol.d.ts.map +1 -0
- package/lib/node/authentication/authentication-protocol.js +12 -0
- package/lib/node/authentication/authentication-protocol.js.map +1 -0
- package/lib/node/authentication/authentication-provider.d.ts +33 -0
- package/lib/node/authentication/authentication-provider.d.ts.map +1 -0
- package/lib/node/authentication/authentication-provider.js +174 -0
- package/lib/node/authentication/authentication-provider.js.map +1 -0
- package/lib/node/authentication/authentication-success-handler.d.ts +16 -0
- package/lib/node/authentication/authentication-success-handler.d.ts.map +1 -0
- package/lib/node/authentication/authentication-success-handler.js +97 -0
- package/lib/node/authentication/authentication-success-handler.js.map +1 -0
- package/lib/node/authentication/index.d.ts +6 -0
- package/lib/node/authentication/index.d.ts.map +1 -0
- package/lib/node/authentication/index.js +22 -0
- package/lib/node/authentication/index.js.map +1 -0
- package/lib/node/cache/cache-protocol.d.ts +16 -0
- package/lib/node/cache/cache-protocol.d.ts.map +1 -0
- package/lib/node/cache/cache-protocol.js +6 -0
- package/lib/node/cache/cache-protocol.js.map +1 -0
- package/lib/node/cache/index.d.ts +3 -0
- package/lib/node/cache/index.d.ts.map +1 -0
- package/lib/node/cache/index.js +19 -0
- package/lib/node/cache/index.js.map +1 -0
- package/lib/node/cache/request-cache.d.ts +10 -0
- package/lib/node/cache/request-cache.d.ts.map +1 -0
- package/lib/node/cache/request-cache.js +52 -0
- package/lib/node/cache/request-cache.js.map +1 -0
- package/lib/node/constants.d.ts +6 -0
- package/lib/node/constants.d.ts.map +1 -0
- package/lib/node/constants.js +9 -0
- package/lib/node/constants.js.map +1 -0
- package/lib/node/context/context-protocol.d.ts +22 -0
- package/lib/node/context/context-protocol.d.ts.map +1 -0
- package/lib/node/context/context-protocol.js +30 -0
- package/lib/node/context/context-protocol.js.map +1 -0
- package/lib/node/context/index.d.ts +5 -0
- package/lib/node/context/index.d.ts.map +1 -0
- package/lib/node/context/index.js +21 -0
- package/lib/node/context/index.js.map +1 -0
- package/lib/node/context/security-context-middleware.d.ts +9 -0
- package/lib/node/context/security-context-middleware.d.ts.map +1 -0
- package/lib/node/context/security-context-middleware.js +44 -0
- package/lib/node/context/security-context-middleware.js.map +1 -0
- package/lib/node/context/security-context-store.d.ts +8 -0
- package/lib/node/context/security-context-store.d.ts.map +1 -0
- package/lib/node/context/security-context-store.js +40 -0
- package/lib/node/context/security-context-store.js.map +1 -0
- package/lib/node/context/security-context-strategy.d.ts +9 -0
- package/lib/node/context/security-context-strategy.d.ts.map +1 -0
- package/lib/node/context/security-context-strategy.js +32 -0
- package/lib/node/context/security-context-strategy.js.map +1 -0
- package/lib/node/crypto/index.d.ts +3 -0
- package/lib/node/crypto/index.d.ts.map +1 -0
- package/lib/node/crypto/index.js +19 -0
- package/lib/node/crypto/index.js.map +1 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.d.ts +6 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.d.ts.map +1 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.js +30 -0
- package/lib/node/crypto/keygen/base64-string-key-generator.js.map +1 -0
- package/lib/node/crypto/keygen/index.d.ts +3 -0
- package/lib/node/crypto/keygen/index.d.ts.map +1 -0
- package/lib/node/crypto/keygen/index.js +19 -0
- package/lib/node/crypto/keygen/index.js.map +1 -0
- package/lib/node/crypto/keygen/keygen-protocol.d.ts +5 -0
- package/lib/node/crypto/keygen/keygen-protocol.d.ts.map +1 -0
- package/lib/node/crypto/keygen/keygen-protocol.js +5 -0
- package/lib/node/crypto/keygen/keygen-protocol.js.map +1 -0
- package/lib/node/crypto/password/index.d.ts +3 -0
- package/lib/node/crypto/password/index.d.ts.map +1 -0
- package/lib/node/crypto/password/index.js +19 -0
- package/lib/node/crypto/password/index.js.map +1 -0
- package/lib/node/crypto/password/password-encoder.d.ts +9 -0
- package/lib/node/crypto/password/password-encoder.d.ts.map +1 -0
- package/lib/node/crypto/password/password-encoder.js +51 -0
- package/lib/node/crypto/password/password-encoder.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 -0
- package/lib/node/crypto/password/password-protocol.js +5 -0
- package/lib/node/crypto/password/password-protocol.js.map +1 -0
- package/lib/node/error/error-handler.d.ts +19 -0
- package/lib/node/error/error-handler.d.ts.map +1 -0
- package/lib/node/error/error-handler.js +81 -0
- package/lib/node/error/error-handler.js.map +1 -0
- package/lib/node/error/error-protocol.d.ts +3 -0
- package/lib/node/error/error-protocol.d.ts.map +1 -0
- package/lib/node/error/error-protocol.js +7 -0
- package/lib/node/error/error-protocol.js.map +1 -0
- package/lib/node/error/error.d.ts +21 -0
- package/lib/node/error/error.d.ts.map +1 -0
- package/lib/node/error/error.js +35 -0
- package/lib/node/error/error.js.map +1 -0
- package/lib/node/error/index.d.ts +4 -0
- package/lib/node/error/index.d.ts.map +1 -0
- package/lib/node/error/index.js +20 -0
- package/lib/node/error/index.js.map +1 -0
- package/lib/node/index.d.ts +11 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +27 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/logout/index.d.ts +5 -0
- package/lib/node/logout/index.d.ts.map +1 -0
- package/lib/node/logout/index.js +21 -0
- package/lib/node/logout/index.js.map +1 -0
- package/lib/node/logout/logout-handler.d.ts +5 -0
- package/lib/node/logout/logout-handler.d.ts.map +1 -0
- package/lib/node/logout/logout-handler.js +22 -0
- package/lib/node/logout/logout-handler.js.map +1 -0
- package/lib/node/logout/logout-middleware.d.ts +16 -0
- package/lib/node/logout/logout-middleware.d.ts.map +1 -0
- package/lib/node/logout/logout-middleware.js +66 -0
- package/lib/node/logout/logout-middleware.js.map +1 -0
- package/lib/node/logout/logout-protocol.d.ts +12 -0
- package/lib/node/logout/logout-protocol.d.ts.map +1 -0
- package/lib/node/logout/logout-protocol.js +9 -0
- package/lib/node/logout/logout-protocol.js.map +1 -0
- package/lib/node/logout/logout-success-handler-provider.d.ts +8 -0
- package/lib/node/logout/logout-success-handler-provider.d.ts.map +1 -0
- package/lib/node/logout/logout-success-handler-provider.js +35 -0
- package/lib/node/logout/logout-success-handler-provider.js.map +1 -0
- package/lib/node/logout/logout-success-handler.d.ts +9 -0
- package/lib/node/logout/logout-success-handler.d.ts.map +1 -0
- package/lib/node/logout/logout-success-handler.js +39 -0
- package/lib/node/logout/logout-success-handler.js.map +1 -0
- package/lib/node/module.d.ts +5 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +7 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/user/index.d.ts +6 -0
- package/lib/node/user/index.d.ts.map +1 -0
- package/lib/node/user/index.js +22 -0
- package/lib/node/user/index.js.map +1 -0
- package/lib/node/user/user-checker.d.ts +8 -0
- package/lib/node/user/user-checker.d.ts.map +1 -0
- package/lib/node/user/user-checker.js +44 -0
- package/lib/node/user/user-checker.js.map +1 -0
- package/lib/node/user/user-mapper.d.ts +9 -0
- package/lib/node/user/user-mapper.d.ts.map +1 -0
- package/lib/node/user/user-mapper.js +40 -0
- package/lib/node/user/user-mapper.js.map +1 -0
- package/lib/node/user/user-middleware.d.ts +11 -0
- package/lib/node/user/user-middleware.d.ts.map +1 -0
- package/lib/node/user/user-middleware.js +55 -0
- package/lib/node/user/user-middleware.js.map +1 -0
- package/lib/node/user/user-protocol.d.ts +15 -0
- package/lib/node/user/user-protocol.d.ts.map +1 -0
- package/lib/node/user/user-protocol.js +9 -0
- package/lib/node/user/user-protocol.js.map +1 -0
- package/lib/node/user/user-service.d.ts +7 -0
- package/lib/node/user/user-service.d.ts.map +1 -0
- package/lib/node/user/user-service.js +42 -0
- package/lib/node/user/user-service.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +4 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +53 -0
- package/src/browser/index.ts +1 -0
- package/src/browser/module.ts +4 -0
- package/src/browser/user/index.ts +2 -0
- package/src/browser/user/user-manager.ts +103 -0
- package/src/browser/user/user-protocol.ts +11 -0
- package/src/common/index.ts +1 -0
- package/src/common/security-protocol.ts +65 -0
- package/src/node/access/access-decision-manager.ts +43 -0
- package/src/node/access/access-decision-utils.ts +18 -0
- package/src/node/access/access-decision-voter.ts +73 -0
- package/src/node/access/access-protocol.ts +93 -0
- package/src/node/access/action-name-resolver.ts +19 -0
- package/src/node/access/index.ts +11 -0
- package/src/node/access/jexl-engine-provider.ts +18 -0
- package/src/node/access/method-advice.ts +64 -0
- package/src/node/access/policy-provider.ts +64 -0
- package/src/node/access/policy-resolver.ts +79 -0
- package/src/node/access/resource-name-resolver.ts +41 -0
- package/src/node/access/security-metadata-source.ts +47 -0
- package/src/node/annotation/action.ts +8 -0
- package/src/node/annotation/anonymous.ts +6 -0
- package/src/node/annotation/authenticated.ts +6 -0
- package/src/node/annotation/authorize.ts +39 -0
- package/src/node/annotation/index.ts +7 -0
- package/src/node/annotation/post-authorize.ts +6 -0
- package/src/node/annotation/pre-authorize.ts +6 -0
- package/src/node/annotation/resource.ts +12 -0
- package/src/node/authentication/authentication-manager.ts +61 -0
- package/src/node/authentication/authentication-middleware.ts +21 -0
- package/src/node/authentication/authentication-protocol.ts +37 -0
- package/src/node/authentication/authentication-provider.ts +155 -0
- package/src/node/authentication/authentication-success-handler.ts +83 -0
- package/src/node/authentication/index.ts +5 -0
- package/src/node/cache/cache-protocol.ts +17 -0
- package/src/node/cache/index.ts +2 -0
- package/src/node/cache/request-cache.ts +38 -0
- package/src/node/constants.ts +5 -0
- package/src/node/context/context-protocol.ts +43 -0
- package/src/node/context/index.ts +4 -0
- package/src/node/context/security-context-middleware.ts +28 -0
- package/src/node/context/security-context-store.ts +25 -0
- package/src/node/context/security-context-strategy.ts +24 -0
- package/src/node/crypto/index.ts +2 -0
- package/src/node/crypto/keygen/base64-string-key-generator.ts +15 -0
- package/src/node/crypto/keygen/index.ts +2 -0
- package/src/node/crypto/keygen/keygen-protocol.ts +7 -0
- package/src/node/crypto/password/index.ts +2 -0
- package/src/node/crypto/password/password-encoder.ts +39 -0
- package/src/node/crypto/password/password-protocol.ts +6 -0
- package/src/node/error/error-handler.ts +58 -0
- package/src/node/error/error-protocol.ts +5 -0
- package/src/node/error/error.ts +41 -0
- package/src/node/error/index.ts +3 -0
- package/src/node/index.ts +10 -0
- package/src/node/logout/index.ts +4 -0
- package/src/node/logout/logout-handler.ts +12 -0
- package/src/node/logout/logout-middleware.ts +49 -0
- package/src/node/logout/logout-protocol.ts +17 -0
- package/src/node/logout/logout-success-handler-provider.ts +21 -0
- package/src/node/logout/logout-success-handler.ts +22 -0
- package/src/node/module.ts +5 -0
- package/src/node/user/index.ts +6 -0
- package/src/node/user/user-checker.ts +37 -0
- package/src/node/user/user-mapper.ts +26 -0
- package/src/node/user/user-middleware.ts +40 -0
- package/src/node/user/user-protocol.ts +20 -0
- package/src/node/user/user-service.ts +28 -0
- package/src/package.spec.ts +4 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Component, Autowired, Prioritizeable, PostConstruct } from '@celljs/core';
|
|
2
|
+
import { AuthenticationProvider, AuthenticationManager, Authentication, AuthenticationSuccessHandler } from './authentication-protocol';
|
|
3
|
+
import { AccountStatusError, AuthenticationError } from '../error';
|
|
4
|
+
import { SecurityContext } from '../context';
|
|
5
|
+
|
|
6
|
+
@Component(AuthenticationManager)
|
|
7
|
+
export class AuthenticationManagerImpl implements AuthenticationManager {
|
|
8
|
+
|
|
9
|
+
protected prioritized: AuthenticationProvider[];
|
|
10
|
+
|
|
11
|
+
@Autowired(AuthenticationProvider)
|
|
12
|
+
protected readonly authenticationProviders: AuthenticationProvider[];
|
|
13
|
+
|
|
14
|
+
@Autowired(AuthenticationSuccessHandler)
|
|
15
|
+
protected readonly authenticationSuccessHandler: AuthenticationSuccessHandler;
|
|
16
|
+
|
|
17
|
+
@PostConstruct()
|
|
18
|
+
init() {
|
|
19
|
+
this.prioritized = Prioritizeable.prioritizeAllSync(this.authenticationProviders).map(c => c.value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async authenticate(next: () => Promise<void>): Promise<void> {
|
|
23
|
+
let result: Authentication | undefined;
|
|
24
|
+
let lastError: any;
|
|
25
|
+
for (const authenticationProvider of this.prioritized) {
|
|
26
|
+
try {
|
|
27
|
+
if (await authenticationProvider.support()) {
|
|
28
|
+
result = await authenticationProvider.authenticate();
|
|
29
|
+
if (result) {
|
|
30
|
+
await this.authenticationSuccessHandler.onAuthenticationSuccess(result);
|
|
31
|
+
SecurityContext.setAuthentication(result);
|
|
32
|
+
if (result.next) {
|
|
33
|
+
await next();
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof AccountStatusError) {
|
|
40
|
+
throw error;
|
|
41
|
+
} else if (error instanceof AuthenticationError) {
|
|
42
|
+
lastError = error;
|
|
43
|
+
} else {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (lastError) {
|
|
49
|
+
throw lastError;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async support(): Promise<boolean> {
|
|
54
|
+
for (const authenticationProvider of this.prioritized) {
|
|
55
|
+
if (await authenticationProvider.support()) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Middleware, Context } from '@celljs/web/lib/node';
|
|
2
|
+
import { Component, Autowired } from '@celljs/core';
|
|
3
|
+
import { AuthenticationManager, AUTHENTICATION_MIDDLE_PRIORITY } from './authentication-protocol';
|
|
4
|
+
|
|
5
|
+
@Component(Middleware)
|
|
6
|
+
export class AuthenticationMiddleware implements Middleware {
|
|
7
|
+
|
|
8
|
+
@Autowired(AuthenticationManager)
|
|
9
|
+
protected readonly authenticationManager: AuthenticationManager;
|
|
10
|
+
|
|
11
|
+
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
|
|
12
|
+
if (await this.authenticationManager.support()) {
|
|
13
|
+
await this.authenticationManager.authenticate(next);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
await next();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
readonly priority: number = AUTHENTICATION_MIDDLE_PRIORITY;
|
|
20
|
+
|
|
21
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Policy } from '../../common';
|
|
2
|
+
import { SECURITY_CONTEXT_MIDDLEWARE_PRIORITY } from '../context';
|
|
3
|
+
|
|
4
|
+
export const AuthenticationProvider = Symbol('AuthenticationProvider');
|
|
5
|
+
export const AuthenticationManager = Symbol('AuthenticationManager');
|
|
6
|
+
export const AuthenticationSuccessHandler = Symbol('AuthenticationSuccessHandler');
|
|
7
|
+
export const AUTHENTICATION_SCHEME_BASIC = 'Basic';
|
|
8
|
+
|
|
9
|
+
export const AUTHENTICATION_MIDDLE_PRIORITY = SECURITY_CONTEXT_MIDDLEWARE_PRIORITY - 100;
|
|
10
|
+
|
|
11
|
+
export const USERNAME_PASSWORD_AUTHENTICATION_PROVIDER_PRIORITY = 2000;
|
|
12
|
+
export const BASE_AUTHENTICATION_PROVIDER_PRIORITY = USERNAME_PASSWORD_AUTHENTICATION_PROVIDER_PRIORITY - 100;
|
|
13
|
+
|
|
14
|
+
export interface AuthenticationManager {
|
|
15
|
+
authenticate(next: () => Promise<void>): Promise<void>;
|
|
16
|
+
support(): Promise<boolean>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface AuthenticationProvider {
|
|
20
|
+
readonly priority: number;
|
|
21
|
+
authenticate(): Promise<Authentication | undefined>;
|
|
22
|
+
support(): Promise<boolean>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Authentication {
|
|
26
|
+
name: string;
|
|
27
|
+
policies: Policy[];
|
|
28
|
+
credentials: any;
|
|
29
|
+
details?: any;
|
|
30
|
+
principal: any;
|
|
31
|
+
next?: boolean;
|
|
32
|
+
authenticated: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface AuthenticationSuccessHandler {
|
|
36
|
+
onAuthenticationSuccess(authentication: Authentication): Promise<void>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Component, Autowired, Value } from '@celljs/core';
|
|
2
|
+
import { AuthenticationProvider, Authentication, USERNAME_PASSWORD_AUTHENTICATION_PROVIDER_PRIORITY,
|
|
3
|
+
BASE_AUTHENTICATION_PROVIDER_PRIORITY, AUTHENTICATION_SCHEME_BASIC } from './authentication-protocol';
|
|
4
|
+
import { Context, RequestMatcher, RedirectStrategy } from '@celljs/web/lib/node';
|
|
5
|
+
import { PasswordEncoder } from '../crypto';
|
|
6
|
+
import { UserService, UserChecker, UserMapper } from '../user';
|
|
7
|
+
import { BadCredentialsError } from '../error';
|
|
8
|
+
import { HttpHeaders } from '@celljs/http';
|
|
9
|
+
import { PathResolver } from '@celljs/web';
|
|
10
|
+
|
|
11
|
+
import { User } from '../../common';
|
|
12
|
+
|
|
13
|
+
@Component(AuthenticationProvider)
|
|
14
|
+
export class UsernamePasswordAuthenticationProvider implements AuthenticationProvider {
|
|
15
|
+
|
|
16
|
+
@Value('cell.security')
|
|
17
|
+
protected readonly options: any;
|
|
18
|
+
|
|
19
|
+
@Autowired(PasswordEncoder)
|
|
20
|
+
protected readonly passwordEncoder: PasswordEncoder;
|
|
21
|
+
|
|
22
|
+
@Autowired(UserService)
|
|
23
|
+
protected readonly userService: UserService<string, User>;
|
|
24
|
+
|
|
25
|
+
@Autowired(UserChecker)
|
|
26
|
+
protected readonly userChecker: UserChecker;
|
|
27
|
+
|
|
28
|
+
@Autowired(UserMapper)
|
|
29
|
+
protected readonly userMapper: UserMapper;
|
|
30
|
+
|
|
31
|
+
@Autowired(RequestMatcher)
|
|
32
|
+
protected readonly requestMatcher: RequestMatcher;
|
|
33
|
+
|
|
34
|
+
@Autowired(PathResolver)
|
|
35
|
+
protected readonly pathResolver: PathResolver;
|
|
36
|
+
|
|
37
|
+
@Autowired(RedirectStrategy)
|
|
38
|
+
protected readonly redirectStrategy: RedirectStrategy;
|
|
39
|
+
|
|
40
|
+
priority = USERNAME_PASSWORD_AUTHENTICATION_PROVIDER_PRIORITY;
|
|
41
|
+
|
|
42
|
+
async authenticate(): Promise<Authentication | undefined> {
|
|
43
|
+
const username = this.doGetValue(this.options.usernameKey);
|
|
44
|
+
const password = this.doGetValue(this.options.passwordKey);
|
|
45
|
+
if (!password || !username) {
|
|
46
|
+
throw new BadCredentialsError('Bad credentials');
|
|
47
|
+
}
|
|
48
|
+
const user = await this.userService.load(username);
|
|
49
|
+
await this.userChecker.check(user);
|
|
50
|
+
if (!await this.passwordEncoder.matches(password, user.password)) {
|
|
51
|
+
throw new BadCredentialsError('Bad credentials');
|
|
52
|
+
}
|
|
53
|
+
await this.userMapper.map(user);
|
|
54
|
+
return {
|
|
55
|
+
name: user.username,
|
|
56
|
+
principal: user,
|
|
57
|
+
credentials: '',
|
|
58
|
+
policies: user.policies,
|
|
59
|
+
authenticated: true
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected doGetValue(key: string): string {
|
|
65
|
+
const request = Context.getRequest();
|
|
66
|
+
if (request.body) {
|
|
67
|
+
return request.body[key];
|
|
68
|
+
} else {
|
|
69
|
+
return <string>request.query[key];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async support(): Promise<boolean> {
|
|
74
|
+
return !!await this.requestMatcher.match(await this.pathResolver.resolve(this.options.loginUrl), this.options.loginMethod);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Component(AuthenticationProvider)
|
|
80
|
+
export class BasicAuthenticationProvider implements AuthenticationProvider {
|
|
81
|
+
|
|
82
|
+
@Autowired(PasswordEncoder)
|
|
83
|
+
protected readonly passwordEncoder: PasswordEncoder;
|
|
84
|
+
|
|
85
|
+
@Autowired(UserService)
|
|
86
|
+
protected readonly userService: UserService<string, User>;
|
|
87
|
+
|
|
88
|
+
@Autowired(UserChecker)
|
|
89
|
+
protected readonly userChecker: UserChecker;
|
|
90
|
+
|
|
91
|
+
@Autowired(UserMapper)
|
|
92
|
+
protected readonly userMapper: UserMapper;
|
|
93
|
+
|
|
94
|
+
@Autowired(RequestMatcher)
|
|
95
|
+
protected readonly requestMatcher: RequestMatcher;
|
|
96
|
+
|
|
97
|
+
@Autowired(PathResolver)
|
|
98
|
+
protected readonly pathResolver: PathResolver;
|
|
99
|
+
|
|
100
|
+
@Autowired(RedirectStrategy)
|
|
101
|
+
protected readonly redirectStrategy: RedirectStrategy;
|
|
102
|
+
|
|
103
|
+
priority = BASE_AUTHENTICATION_PROVIDER_PRIORITY;
|
|
104
|
+
|
|
105
|
+
async authenticate(): Promise<Authentication | undefined> {
|
|
106
|
+
const request = Context.getRequest();
|
|
107
|
+
const header = request.get(HttpHeaders.AUTHORIZATION)!.trim();
|
|
108
|
+
|
|
109
|
+
const token = Buffer.from(header.substring(6), 'base64').toString('utf8');
|
|
110
|
+
if (!token) {
|
|
111
|
+
throw new BadCredentialsError('Empty basic authentication token');
|
|
112
|
+
}
|
|
113
|
+
const delim = token.indexOf(':');
|
|
114
|
+
if (delim === -1) {
|
|
115
|
+
throw new BadCredentialsError('Invalid basic authentication token');
|
|
116
|
+
}
|
|
117
|
+
const username = token.substring(0, delim);
|
|
118
|
+
const password = token.substring(delim);
|
|
119
|
+
if (!password || !username) {
|
|
120
|
+
throw new BadCredentialsError('Bad credentials');
|
|
121
|
+
}
|
|
122
|
+
const user = await this.userService.load(username);
|
|
123
|
+
await this.userChecker.check(user);
|
|
124
|
+
if (!await this.passwordEncoder.matches(password, user.password)) {
|
|
125
|
+
throw new BadCredentialsError('Bad credentials');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await this.userMapper.map(user);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
name: user.username,
|
|
132
|
+
principal: user,
|
|
133
|
+
credentials: '',
|
|
134
|
+
policies: user.policies,
|
|
135
|
+
authenticated: true,
|
|
136
|
+
next: true
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async support(): Promise<boolean> {
|
|
142
|
+
const request = Context.getRequest();
|
|
143
|
+
let header = request.get(HttpHeaders.AUTHORIZATION);
|
|
144
|
+
if (!header) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
header = header.trim();
|
|
148
|
+
if (header.toLowerCase().startsWith(AUTHENTICATION_SCHEME_BASIC.toLowerCase())) {
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { AuthenticationSuccessHandler, Authentication } from './authentication-protocol';
|
|
2
|
+
import { Component, Value, Autowired, Logger } from '@celljs/core';
|
|
3
|
+
import { Context, RedirectStrategy } from '@celljs/web/lib/node';
|
|
4
|
+
import { HttpHeaders, XML_HTTP_REQUEST } from '@celljs/http';
|
|
5
|
+
import { RequestCache } from '../cache';
|
|
6
|
+
|
|
7
|
+
@Component(AuthenticationSuccessHandler)
|
|
8
|
+
export class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
|
9
|
+
|
|
10
|
+
@Value('cell.security.targetUrlParameter')
|
|
11
|
+
protected readonly targetUrlParameter: string;
|
|
12
|
+
|
|
13
|
+
@Value('cell.security.loginSuccessUrl')
|
|
14
|
+
protected readonly loginSuccessUrl: string;
|
|
15
|
+
|
|
16
|
+
@Value('cell.security.alwaysUseLoginSuccessUrl')
|
|
17
|
+
protected readonly alwaysUseLoginSuccessUrl: boolean;
|
|
18
|
+
|
|
19
|
+
@Value('cell.security.useReferer')
|
|
20
|
+
protected readonly useReferer: boolean;
|
|
21
|
+
|
|
22
|
+
@Autowired(Logger)
|
|
23
|
+
protected readonly logger: Logger;
|
|
24
|
+
|
|
25
|
+
@Autowired(RedirectStrategy)
|
|
26
|
+
protected readonly redirectStrategy: RedirectStrategy;
|
|
27
|
+
|
|
28
|
+
@Autowired(RequestCache)
|
|
29
|
+
protected readonly requestCache: RequestCache;
|
|
30
|
+
|
|
31
|
+
async onAuthenticationSuccess(authentication: Authentication): Promise<void> {
|
|
32
|
+
const response = Context.getResponse();
|
|
33
|
+
if (Context.getRequest().get(HttpHeaders.X_REQUESTED_WITH) === XML_HTTP_REQUEST) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (!this.loginSuccessUrl) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const targetUrl = await this.determineTargetUrl(authentication);
|
|
40
|
+
if (response.writableEnded) {
|
|
41
|
+
this.logger.debug(`Response has already been committed. Unable to redirect to ${targetUrl}`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
await this.redirectStrategy.send(targetUrl);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected async determineTargetUrl(authentication: Authentication) {
|
|
48
|
+
if (this.alwaysUseLoginSuccessUrl) {
|
|
49
|
+
return this.loginSuccessUrl;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let targetUrl: string | undefined;
|
|
53
|
+
const request = Context.getRequest();
|
|
54
|
+
if (this.targetUrlParameter) {
|
|
55
|
+
targetUrl = <string | undefined>request.query[this.targetUrlParameter];
|
|
56
|
+
if (targetUrl) {
|
|
57
|
+
this.logger.debug(`Found targetUrlParameter in request: ${targetUrl}`);
|
|
58
|
+
return targetUrl;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (this.useReferer) {
|
|
63
|
+
const referer = request.get(HttpHeaders.REFERER);
|
|
64
|
+
targetUrl = referer?.length ? referer[0] : '';
|
|
65
|
+
this.logger.debug(`Using Referer header: ${targetUrl}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const savedRequest = await this.requestCache.get();
|
|
69
|
+
if (savedRequest) {
|
|
70
|
+
await this.requestCache.remove();
|
|
71
|
+
targetUrl = savedRequest.redirectUrl;
|
|
72
|
+
this.logger.debug(`Redirecting to SavedRequest Url: ${targetUrl}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!targetUrl) {
|
|
76
|
+
targetUrl = this.loginSuccessUrl;
|
|
77
|
+
this.logger.debug(`Using default Url: ${targetUrl}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return targetUrl;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { HttpMethod } from '@celljs/http';
|
|
2
|
+
|
|
3
|
+
export const RequestCache = Symbol('RequestCache');
|
|
4
|
+
export const SAVED_REQUEST = 'MALAGU_SECURITY_SAVED_REQUEST';
|
|
5
|
+
|
|
6
|
+
export interface SavedRequest {
|
|
7
|
+
redirectUrl: string;
|
|
8
|
+
method: HttpMethod;
|
|
9
|
+
query: { [key: string]: string };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RequestCache {
|
|
13
|
+
save(savedRequest?: SavedRequest): Promise<void>;
|
|
14
|
+
get(): Promise<SavedRequest | undefined>;
|
|
15
|
+
remove(): Promise<void>;
|
|
16
|
+
|
|
17
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RequestCache, SavedRequest, SAVED_REQUEST } from './cache-protocol';
|
|
2
|
+
import { AttributeScope, Context } from '@celljs/web/lib/node';
|
|
3
|
+
import { Component, Value, Autowired } from '@celljs/core';
|
|
4
|
+
import { ENDPOINT, PathResolver } from '@celljs/web';
|
|
5
|
+
import * as qs from 'qs';
|
|
6
|
+
|
|
7
|
+
@Component(RequestCache)
|
|
8
|
+
export class HttpSessionRequestCache implements RequestCache {
|
|
9
|
+
|
|
10
|
+
@Value(ENDPOINT)
|
|
11
|
+
protected readonly endpoint: string;
|
|
12
|
+
|
|
13
|
+
@Autowired(PathResolver)
|
|
14
|
+
protected readonly pathResolver: PathResolver;
|
|
15
|
+
|
|
16
|
+
async save(savedRequest?: SavedRequest): Promise<void> {
|
|
17
|
+
if (Context.getSession()) {
|
|
18
|
+
const request = Context.getRequest();
|
|
19
|
+
Context.setAttr(SAVED_REQUEST, savedRequest || {
|
|
20
|
+
redirectUrl: await this.pathResolver.resolve(this.endpoint, request.path, qs.stringify(request.query)),
|
|
21
|
+
method: request.method.toUpperCase(),
|
|
22
|
+
query: { ...request.query }
|
|
23
|
+
}, AttributeScope.Session);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async get(): Promise<SavedRequest | undefined> {
|
|
27
|
+
if (Context.getSession()) {
|
|
28
|
+
return Context.getAttr<SavedRequest>(SAVED_REQUEST, AttributeScope.Session);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async remove(): Promise<void> {
|
|
33
|
+
if (Context.getSession()) {
|
|
34
|
+
Context.setAttr(SAVED_REQUEST, undefined, AttributeScope.Session);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Context } from '@celljs/web/lib/node';
|
|
2
|
+
import { Authentication } from '../authentication';
|
|
3
|
+
import { SESSION_MIDDLEWARE_PRIORITY } from '@celljs/web/lib/node/session/session-protocol';
|
|
4
|
+
import { AttributeScope } from '@celljs/core/lib/node';
|
|
5
|
+
|
|
6
|
+
export const SECURITY_CONTEXT_MIDDLEWARE_PRIORITY = SESSION_MIDDLEWARE_PRIORITY - 100;
|
|
7
|
+
export const CURRENT_SECURITY_CONTEXT_REQUEST_KEY = 'CurrentSecurityContextRequest';
|
|
8
|
+
|
|
9
|
+
export const SecurityContextStore = Symbol('SecurityContextStore');
|
|
10
|
+
export const SecurityContextStrategy = Symbol('SecurityContextStrategy');
|
|
11
|
+
|
|
12
|
+
export interface SecurityContext {
|
|
13
|
+
authentication: Authentication;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface SecurityContextStore {
|
|
17
|
+
load(): Promise<SecurityContext>;
|
|
18
|
+
save(context: SecurityContext): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SecurityContextStrategy {
|
|
22
|
+
create(): Promise<SecurityContext>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export namespace SecurityContext {
|
|
26
|
+
|
|
27
|
+
export function setCurrent(context: SecurityContext | undefined ) {
|
|
28
|
+
Context.setAttr(CURRENT_SECURITY_CONTEXT_REQUEST_KEY, context);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getCurrent<T extends SecurityContext>(): T {
|
|
32
|
+
return Context.getAttr(CURRENT_SECURITY_CONTEXT_REQUEST_KEY, AttributeScope.Request);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getAuthentication(): Authentication {
|
|
36
|
+
return getCurrent().authentication;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function setAuthentication(authentication: Authentication): void {
|
|
40
|
+
getCurrent().authentication = authentication;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Middleware, Context } from '@celljs/web/lib/node';
|
|
2
|
+
import { Component, Value, Autowired } from '@celljs/core';
|
|
3
|
+
import { SecurityContextStore, SecurityContext, SECURITY_CONTEXT_MIDDLEWARE_PRIORITY } from './context-protocol';
|
|
4
|
+
|
|
5
|
+
@Component(Middleware)
|
|
6
|
+
export class SecurityContextMiddleWare implements Middleware {
|
|
7
|
+
|
|
8
|
+
@Value('cell.security')
|
|
9
|
+
protected readonly options: any;
|
|
10
|
+
|
|
11
|
+
@Autowired(SecurityContextStore)
|
|
12
|
+
protected readonly securityContextStore: SecurityContextStore;
|
|
13
|
+
|
|
14
|
+
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
|
|
15
|
+
|
|
16
|
+
const context = await this.securityContextStore.load();
|
|
17
|
+
try {
|
|
18
|
+
SecurityContext.setCurrent(context);
|
|
19
|
+
Context.setTenant(context.authentication.name);
|
|
20
|
+
await next();
|
|
21
|
+
} finally {
|
|
22
|
+
await this.securityContextStore.save(SecurityContext.getCurrent());
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
readonly priority: number = SECURITY_CONTEXT_MIDDLEWARE_PRIORITY;
|
|
27
|
+
|
|
28
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Component, Value, Autowired } from '@celljs/core';
|
|
2
|
+
import { SecurityContextStore, SecurityContext, SecurityContextStrategy } from './context-protocol';
|
|
3
|
+
import { Context } from '@celljs/web/lib/node';
|
|
4
|
+
|
|
5
|
+
@Component(SecurityContextStore)
|
|
6
|
+
export class SessionSecurityContextStore implements SecurityContextStore {
|
|
7
|
+
|
|
8
|
+
@Value('cell.security')
|
|
9
|
+
protected readonly options: any;
|
|
10
|
+
|
|
11
|
+
@Autowired(SecurityContextStrategy)
|
|
12
|
+
protected readonly securityContextStrategy: SecurityContextStrategy;
|
|
13
|
+
|
|
14
|
+
async load(): Promise<SecurityContext> {
|
|
15
|
+
const context = Context.getSession()[this.options.contextKey];
|
|
16
|
+
if (!context) {
|
|
17
|
+
return this.securityContextStrategy.create();
|
|
18
|
+
}
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
async save(context: SecurityContext): Promise<void> {
|
|
22
|
+
Context.getSession()[this.options.contextKey] = context;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Component } from '@celljs/core';
|
|
2
|
+
import { SecurityContext, SecurityContextStrategy } from './context-protocol';
|
|
3
|
+
import { Authentication } from '../authentication';
|
|
4
|
+
|
|
5
|
+
export class SecurityContextImpl implements SecurityContext {
|
|
6
|
+
authentication: Authentication;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
@Component(SecurityContextStrategy)
|
|
10
|
+
export class SessionSecurityContextStrategy implements SecurityContextStrategy {
|
|
11
|
+
|
|
12
|
+
create(): Promise<SecurityContext> {
|
|
13
|
+
const securityContext = new SecurityContextImpl();
|
|
14
|
+
securityContext.authentication = {
|
|
15
|
+
name: 'anonymousUser',
|
|
16
|
+
principal: 'anonymousUser',
|
|
17
|
+
policies: [],
|
|
18
|
+
credentials: '',
|
|
19
|
+
authenticated: false
|
|
20
|
+
};
|
|
21
|
+
return Promise.resolve(securityContext);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StringKeyGenerator } from './keygen-protocol';
|
|
2
|
+
import { enc, lib } from 'crypto-js';
|
|
3
|
+
import { Value, Component } from '@celljs/core';
|
|
4
|
+
|
|
5
|
+
@Component(StringKeyGenerator)
|
|
6
|
+
export class Base64StringKeyGenerator implements StringKeyGenerator {
|
|
7
|
+
|
|
8
|
+
@Value('cell.security.base64StringKeyLength')
|
|
9
|
+
protected readonly keyLength: number;
|
|
10
|
+
|
|
11
|
+
async generateKey(keyLength?: number) {
|
|
12
|
+
const key = lib.WordArray.random(keyLength || this.keyLength);
|
|
13
|
+
return enc.Base64.stringify(key);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { PasswordEncoder } from './password-protocol';
|
|
2
|
+
import { PBKDF2, lib, enc} from 'crypto-js';
|
|
3
|
+
import { Value, Component } from '@celljs/core';
|
|
4
|
+
|
|
5
|
+
@Component(PasswordEncoder)
|
|
6
|
+
export class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
|
7
|
+
|
|
8
|
+
@Value('cell.security.passwordEncoder')
|
|
9
|
+
protected readonly options: any;
|
|
10
|
+
|
|
11
|
+
async encode(rawPassword: string): Promise<string> {
|
|
12
|
+
const { encodeHashAsBase64 } = this.options;
|
|
13
|
+
const salt = lib.WordArray.random(8);
|
|
14
|
+
const encoded = this.doEncode(rawPassword, salt.toString());
|
|
15
|
+
if (encodeHashAsBase64) {
|
|
16
|
+
return enc.Base64.stringify(enc.Utf8.parse(encoded));
|
|
17
|
+
}
|
|
18
|
+
return encoded;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected doEncode(rawPassword: string, salt: string): string {
|
|
22
|
+
const { secret } = this.options;
|
|
23
|
+
return salt + PBKDF2(rawPassword, salt + secret, this.options).toString();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async matches(rawPassword: string, encodedPassword: string): Promise<boolean> {
|
|
27
|
+
const digested = this.doDecode(encodedPassword);
|
|
28
|
+
const salt = digested.substring(0, 16);
|
|
29
|
+
return digested === this.doEncode(rawPassword, salt);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected doDecode(encoded: string): string {
|
|
33
|
+
const { encodeHashAsBase64 } = this.options;
|
|
34
|
+
if (encodeHashAsBase64) {
|
|
35
|
+
return enc.Base64.parse(encoded).toString(enc.Utf8);
|
|
36
|
+
}
|
|
37
|
+
return encoded;
|
|
38
|
+
}
|
|
39
|
+
}
|