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" ;
29
310import { defaults } from "lodash" ;
411import { CryptoError } from "./CryptoError" ;
512import { CryptoErrorCode } from "./CryptoErrorCode" ;
6- import { CryptoLayerConfig } from "./CryptoLayerConfig" ;
13+ import { CryptoLayerConfig , CryptoLayerProviderFilter } from "./CryptoLayerConfig" ;
714
815let PROVIDERS_BY_SECURITY : Map < SecurityLevel , Provider [ ] > | undefined = undefined ;
916let PROVIDERS_BY_NAME : Map < string , Provider > | undefined = undefined ;
@@ -16,6 +23,56 @@ const DEFAULT_PROVIDER_CONFIG: ProviderConfig = {
1623 supported_hashes : [ "Sha2_256" , "Sha2_512" ]
1724} ;
1825
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+
1976/**
2077 * Intializes global providers with the given configuration.
2178 *
@@ -28,37 +85,19 @@ export async function initCryptoLayerProviders(config: CryptoLayerConfig): Promi
2885 return ;
2986 }
3087
31- let providerImplConfig : ProviderImplConfig = { additional_config : [ config . keyMetadataStoreConfig ] } ;
88+ const providerImplConfig : ProviderImplConfig = { additional_config : [ config . keyMetadataStoreConfig ] } ;
3289 if ( config . keyMetadataStoreAuth ) {
3390 providerImplConfig . additional_config . push ( config . keyMetadataStoreAuth ) ;
3491 }
3592
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+ ) ;
62101
63102 if ( ! provider ) {
64103 throw new CryptoError ( CryptoErrorCode . CalFailedLoadingProvider , `Failed loading provider.` ) ;
@@ -68,23 +107,7 @@ export async function initCryptoLayerProviders(config: CryptoLayerConfig): Promi
68107 }
69108
70109 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 ) ;
88111}
89112
90113function isSecurityLevel ( value : string ) : value is SecurityLevel {
@@ -113,7 +136,7 @@ export function getProvider(key: string | SecurityLevel | undefined): Provider |
113136 return undefined ;
114137 }
115138
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 ) ;
117140
118141 if ( ! provider ) {
119142 throw new CryptoError ( CryptoErrorCode . WrongParameters , `No such provider with name or security level: ${ key } ` ) ;
0 commit comments