@@ -36,7 +36,10 @@ fn path_to_file_uri(path: &str) -> Result<String> {
3636}
3737
3838fn normalize_jwk_set_source ( source : & str ) -> Result < String > {
39- if source. starts_with ( "https://" ) || source. starts_with ( "file://" ) {
39+ if source. starts_with ( "https://" )
40+ || source. starts_with ( "http://" )
41+ || source. starts_with ( "file://" )
42+ {
4043 return Ok ( source. to_string ( ) ) ;
4144 }
4245
@@ -47,12 +50,21 @@ fn normalize_jwk_set_source(source: &str) -> Result<String> {
4750 path_to_file_uri ( source)
4851}
4952
50- /// Read a PEM public key from a URI (`https://`, `file://`, or local path).
51- pub ( crate ) async fn read_pem_public_key_from_uri ( uri : & str ) -> Result < DecodingKey > {
53+ /// Read a PEM public key from a URI.
54+ ///
55+ /// # Arguments
56+ ///
57+ /// * `uri` - The URI of the PEM public key.
58+ /// * `allow_insecure_http` - Whether to allow HTTP address as uri.
59+ pub ( crate ) async fn read_pem_public_key_from_uri (
60+ uri : & str ,
61+ allow_insecure_http : bool ,
62+ ) -> Result < DecodingKey > {
5263 let maybe_url = Url :: parse ( uri) ;
5364 let data = if let Ok ( url) = maybe_url {
5465 match url. scheme ( ) {
5566 "https" => reqwest:: get ( uri) . await ?. bytes ( ) . await ?. to_vec ( ) ,
67+ "http" if allow_insecure_http => reqwest:: get ( uri) . await ?. bytes ( ) . await ?. to_vec ( ) ,
5668 "file" => std:: fs:: read ( url. path ( ) ) ?,
5769 _ => {
5870 bail ! ( "unsupported scheme in {uri}" ) ;
@@ -127,16 +139,18 @@ impl JwtVerifier {
127139 /// * `trusted_cert_paths` - The paths of the trusted certificates.
128140 /// * `trusted_pem_public_key_uris` - The URIs of the trusted PEM public keys.
129141 /// * `insecure_public_key_from_jwt` - Whether to verify the endorsement of the public key from JWT header.
142+ /// * `insecure_public_key_from_uri` - Whether to allow insecure HTTP address in trusted_pem_public_key_uris.
130143 pub async fn new (
131144 trusted_jwk_set_uris : & [ String ] ,
132145 trusted_cert_paths : & [ String ] ,
133146 trusted_pem_public_key_uris : & [ String ] ,
134147 insecure_public_key_from_jwt : bool ,
148+ insecure_public_key_from_uri : bool ,
135149 ) -> Result < Self > {
136150 let mut trusted_jwk_sets = JwkSet { keys : Vec :: new ( ) } ;
137151 for uri in trusted_jwk_set_uris {
138152 let uri = normalize_jwk_set_source ( & uri[ ..] ) ?;
139- let mut jwk_set = read_jwk_from_uri ( & uri[ ..] ) . await ?;
153+ let mut jwk_set = read_jwk_from_uri ( & uri[ ..] , insecure_public_key_from_uri ) . await ?;
140154 trusted_jwk_sets. keys . append ( & mut jwk_set. keys ) ;
141155 }
142156
@@ -156,7 +170,8 @@ impl JwtVerifier {
156170
157171 let mut trusted_pem_public_keys = Vec :: new ( ) ;
158172 for uri in trusted_pem_public_key_uris {
159- let public_key = read_pem_public_key_from_uri ( uri) . await ?;
173+ let public_key =
174+ read_pem_public_key_from_uri ( uri, insecure_public_key_from_uri) . await ?;
160175 trusted_pem_public_keys. push ( public_key) ;
161176 }
162177
@@ -356,7 +371,7 @@ mod tests {
356371
357372 use crate :: crypto:: jwt:: JwtVerifier ;
358373
359- let verifier = JwtVerifier :: new ( & [ ] , & [ trusted_pem_path. to_string ( ) ] , & [ ] , false )
374+ let verifier = JwtVerifier :: new ( & [ ] , & [ trusted_pem_path. to_string ( ) ] , & [ ] , false , false )
360375 . await
361376 . expect ( "verifier init" ) ;
362377 let jwk_json = std:: fs:: read_to_string ( jwk_json_path) . expect ( "read jwk json" ) ;
0 commit comments