@@ -8,6 +8,7 @@ use std::borrow::Cow;
88
99use anyhow:: { Context , bail} ;
1010use camino:: Utf8PathBuf ;
11+ use futures:: future:: { try_join, try_join_all} ;
1112use mas_jose:: jwk:: { JsonWebKey , JsonWebKeySet } ;
1213use mas_keystore:: { Encrypter , Keystore , PrivateKey } ;
1314use rand:: {
@@ -143,6 +144,22 @@ impl KeyConfig {
143144 Key :: Value ( key) => Cow :: Borrowed ( key) ,
144145 } )
145146 }
147+
148+ /// Returns the JSON Web Key derived from this key config.
149+ ///
150+ /// Password and/or key are read from file if they’re given as path.
151+ async fn json_web_key ( & self ) -> anyhow:: Result < JsonWebKey < mas_keystore:: PrivateKey > > {
152+ let ( key, password) = try_join ( self . key ( ) , self . password ( ) ) . await ?;
153+
154+ let private_key = match password {
155+ Some ( password) => PrivateKey :: load_encrypted ( key. as_bytes ( ) , password. as_bytes ( ) ) ?,
156+ None => PrivateKey :: load ( key. as_bytes ( ) ) ?,
157+ } ;
158+
159+ Ok ( JsonWebKey :: new ( private_key)
160+ . with_kid ( self . kid . clone ( ) )
161+ . with_use ( mas_iana:: jose:: JsonWebKeyUse :: Sig ) )
162+ }
146163}
147164
148165/// Application secrets
@@ -171,24 +188,9 @@ impl SecretsConfig {
171188 /// Returns an error when a key could not be imported
172189 #[ tracing:: instrument( name = "secrets.load" , skip_all) ]
173190 pub async fn key_store ( & self ) -> anyhow:: Result < Keystore > {
174- let mut keys = Vec :: with_capacity ( self . keys . len ( ) ) ;
175- for item in & self . keys {
176- let password = item. password ( ) . await ?;
177-
178- let key = item. key ( ) . await ?;
179- let private_key = match password {
180- Some ( password) => PrivateKey :: load_encrypted ( key. as_bytes ( ) , password. as_bytes ( ) ) ?,
181- None => PrivateKey :: load ( key. as_bytes ( ) ) ?,
182- } ;
183-
184- let key = JsonWebKey :: new ( private_key)
185- . with_kid ( item. kid . clone ( ) )
186- . with_use ( mas_iana:: jose:: JsonWebKeyUse :: Sig ) ;
187- keys. push ( key) ;
188- }
191+ let web_keys = try_join_all ( self . keys . iter ( ) . map ( KeyConfig :: json_web_key) ) . await ?;
189192
190- let keys = JsonWebKeySet :: new ( keys) ;
191- Ok ( Keystore :: new ( keys) )
193+ Ok ( Keystore :: new ( JsonWebKeySet :: new ( web_keys) ) )
192194 }
193195
194196 /// Derive an [`Encrypter`] out of the config
0 commit comments