1
- import { Provider , ProviderConfig , ProviderImplConfig , SecurityLevel } from "@nmshd/rs-crypto-types" ;
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+ import {
3
+ Provider ,
4
+ ProviderConfig ,
5
+ ProviderFactoryFunctions ,
6
+ ProviderImplConfig ,
7
+ SecurityLevel
8
+ } from "@nmshd/rs-crypto-types" ;
2
9
3
10
import { defaults } from "lodash" ;
4
11
import { CryptoError } from "./CryptoError" ;
5
12
import { CryptoErrorCode } from "./CryptoErrorCode" ;
6
- import { CryptoLayerConfig } from "./CryptoLayerConfig" ;
13
+ import { CryptoLayerConfig , CryptoLayerProviderFilter } from "./CryptoLayerConfig" ;
7
14
8
15
let PROVIDERS_BY_SECURITY : Map < SecurityLevel , Provider [ ] > | undefined = undefined ;
9
16
let PROVIDERS_BY_NAME : Map < string , Provider > | undefined = undefined ;
@@ -16,6 +23,56 @@ const DEFAULT_PROVIDER_CONFIG: ProviderConfig = {
16
23
supported_hashes : [ "Sha2_256" , "Sha2_512" ]
17
24
} ;
18
25
26
+ async function providerBySecurityMapFromProviderByNameMap (
27
+ providersByName : Map < string , Provider >
28
+ ) : Promise < Map < SecurityLevel , Provider [ ] > > {
29
+ const providersBySecurity = new Map ( ) ;
30
+ for ( const [ _key , value ] of providersByName ) {
31
+ const caps = await value . getCapabilities ( ) ;
32
+ if ( ! caps ?. min_security_level ) {
33
+ continue ;
34
+ }
35
+ const securityLevel = caps . min_security_level ;
36
+
37
+ if ( ! providersBySecurity . has ( securityLevel ) ) {
38
+ providersBySecurity . set ( securityLevel , [ ] ) ;
39
+ }
40
+
41
+ providersBySecurity . get ( securityLevel ) ! . push ( value ) ;
42
+ }
43
+ return providersBySecurity ;
44
+ }
45
+
46
+ /**
47
+ * Creates a provider if possible with the given provider filter. This means, that the provider created must adhere to the filter.
48
+ *
49
+ * If a `SecurityLevel` is given, the default provider config (`DEFAULT_PROVIDER_CONFIG`) will be used to fill in the rest for the selection.
50
+ */
51
+ async function createProviderFromProviderFilter (
52
+ providerToBeInitialized : CryptoLayerProviderFilter ,
53
+ factoryFunctions : ProviderFactoryFunctions ,
54
+ providerImplConfig : ProviderImplConfig
55
+ ) : Promise < Provider | undefined > {
56
+ if ( "providerName" in providerToBeInitialized ) {
57
+ return await factoryFunctions . createProviderFromName ( providerToBeInitialized . providerName , providerImplConfig ) ;
58
+ }
59
+ if ( "securityLevel" in providerToBeInitialized ) {
60
+ const providerConfig : ProviderConfig = defaults (
61
+ {
62
+ max_security_level : providerToBeInitialized . securityLevel ,
63
+ min_security_level : providerToBeInitialized . securityLevel
64
+ } ,
65
+ DEFAULT_PROVIDER_CONFIG
66
+ ) ;
67
+ return await factoryFunctions . createProvider ( providerConfig , providerImplConfig ) ;
68
+ }
69
+ if ( "providerConfig" in providerToBeInitialized ) {
70
+ return await factoryFunctions . createProvider ( providerToBeInitialized . providerConfig , providerImplConfig ) ;
71
+ }
72
+
73
+ throw new CryptoError ( CryptoErrorCode . WrongParameters ) ;
74
+ }
75
+
19
76
/**
20
77
* Intializes global providers with the given configuration.
21
78
*
@@ -28,37 +85,19 @@ export async function initCryptoLayerProviders(config: CryptoLayerConfig): Promi
28
85
return ;
29
86
}
30
87
31
- let providerImplConfig : ProviderImplConfig = { additional_config : [ config . keyMetadataStoreConfig ] } ;
88
+ const providerImplConfig : ProviderImplConfig = { additional_config : [ config . keyMetadataStoreConfig ] } ;
32
89
if ( config . keyMetadataStoreAuth ) {
33
90
providerImplConfig . additional_config . push ( config . keyMetadataStoreAuth ) ;
34
91
}
35
92
36
- let providers : Map < string , Provider > = new Map ( ) ;
37
-
38
- for ( const providerInitalizationConfig of config . providers ) {
39
- let provider : Provider | undefined ;
40
- if ( "providerName" in providerInitalizationConfig ) {
41
- provider = await config . factoryFunctions . createProviderFromName (
42
- providerInitalizationConfig . providerName ,
43
- providerImplConfig
44
- ) ;
45
- } else if ( "securityLevel" in providerInitalizationConfig ) {
46
- let providerConfig : ProviderConfig = defaults (
47
- {
48
- max_security_level : providerInitalizationConfig . securityLevel ,
49
- min_security_level : providerInitalizationConfig . securityLevel
50
- } ,
51
- DEFAULT_PROVIDER_CONFIG
52
- ) ;
53
- provider = await config . factoryFunctions . createProvider ( providerConfig , providerImplConfig ) ;
54
- } else if ( "providerConfig" in providerInitalizationConfig ) {
55
- provider = await config . factoryFunctions . createProvider (
56
- providerInitalizationConfig . providerConfig ,
57
- providerImplConfig
58
- ) ;
59
- } else {
60
- throw new CryptoError ( CryptoErrorCode . WrongParameters ) ;
61
- }
93
+ const providers : Map < string , Provider > = new Map ( ) ;
94
+
95
+ for ( const providerFilter of config . providersToBeInitialized ) {
96
+ const provider = await createProviderFromProviderFilter (
97
+ providerFilter ,
98
+ config . factoryFunctions ,
99
+ providerImplConfig
100
+ ) ;
62
101
63
102
if ( ! provider ) {
64
103
throw new CryptoError ( CryptoErrorCode . CalFailedLoadingProvider , `Failed loading provider.` ) ;
@@ -68,23 +107,7 @@ export async function initCryptoLayerProviders(config: CryptoLayerConfig): Promi
68
107
}
69
108
70
109
PROVIDERS_BY_NAME = providers ;
71
-
72
- let providers_by_security = new Map ( ) ;
73
- for ( const [ key , value ] of providers ) {
74
- let caps = await value . getCapabilities ( ) ;
75
- if ( ! caps ?. min_security_level ) {
76
- continue ;
77
- }
78
- let securityLevel = caps . min_security_level ;
79
-
80
- if ( ! providers_by_security . has ( securityLevel ) ) {
81
- providers_by_security . set ( securityLevel , [ ] ) ;
82
- }
83
-
84
- providers_by_security . get ( securityLevel ) ! . push ( value ) ;
85
- }
86
-
87
- PROVIDERS_BY_SECURITY = providers_by_security ;
110
+ PROVIDERS_BY_SECURITY = await providerBySecurityMapFromProviderByNameMap ( PROVIDERS_BY_NAME ) ;
88
111
}
89
112
90
113
function isSecurityLevel ( value : string ) : value is SecurityLevel {
@@ -113,7 +136,7 @@ export function getProvider(key: string | SecurityLevel | undefined): Provider |
113
136
return undefined ;
114
137
}
115
138
116
- let provider = isSecurityLevel ( key ) ? PROVIDERS_BY_SECURITY . get ( key ) ?. [ 0 ] : PROVIDERS_BY_NAME . get ( key ) ;
139
+ const provider = isSecurityLevel ( key ) ? PROVIDERS_BY_SECURITY . get ( key ) ?. [ 0 ] : PROVIDERS_BY_NAME . get ( key ) ;
117
140
118
141
if ( ! provider ) {
119
142
throw new CryptoError ( CryptoErrorCode . WrongParameters , `No such provider with name or security level: ${ key } ` ) ;
0 commit comments