1414use api:: auth:: { AuthResponse , Authorizer } ;
1515use api:: error:: VssError ;
1616use async_trait:: async_trait;
17- use jsonwebtoken:: { decode, Algorithm , Validation } ;
17+ use jsonwebtoken:: { decode, Algorithm , DecodingKey , Validation } ;
1818use serde:: { Deserialize , Serialize } ;
1919use std:: collections:: HashMap ;
2020
21- pub use jsonwebtoken:: DecodingKey ;
22-
2321/// A JWT based authorizer, only allows requests with verified 'JsonWebToken' signed by the given
2422/// issuer key.
2523///
@@ -43,9 +41,11 @@ pub(crate) struct Claims {
4341const BEARER_PREFIX : & str = "Bearer " ;
4442
4543impl JWTAuthorizer {
46- /// Create new instance of [`JWTAuthorizer`]
47- pub async fn new ( jwt_issuer_key : DecodingKey ) -> Self {
48- Self { jwt_issuer_key }
44+ /// Creates a new instance of [`JWTAuthorizer`], fails on failure to parse the PEM formatted RSA public key
45+ pub async fn new ( rsa_pem : & str ) -> Result < Self , String > {
46+ let jwt_issuer_key =
47+ DecodingKey :: from_rsa_pem ( rsa_pem. as_bytes ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
48+ Ok ( Self { jwt_issuer_key } )
4949 }
5050}
5151
@@ -76,7 +76,7 @@ mod tests {
7676 use crate :: JWTAuthorizer ;
7777 use api:: auth:: Authorizer ;
7878 use api:: error:: VssError ;
79- use jsonwebtoken:: { encode, Algorithm , DecodingKey , EncodingKey , Header } ;
79+ use jsonwebtoken:: { encode, Algorithm , EncodingKey , Header } ;
8080 use serde:: { Deserialize , Serialize } ;
8181 use std:: collections:: HashMap ;
8282 use std:: time:: SystemTime ;
@@ -134,7 +134,7 @@ mod tests {
134134 )
135135 . expect ( "Failed to create Encoding Key." ) ;
136136
137- let decoding_key = DecodingKey :: from_rsa_pem (
137+ let decoding_key = String :: from (
138138 "-----BEGIN PUBLIC KEY-----\
139139 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAysGpKU+I9i9b+QZSANu/\
140140 ExaA6w4qiQdFZaXeReiz49r1oDfABwKIFW9gK/kNnrnL9H8P+pYfj7jqUJ/glmgq\
@@ -143,12 +143,10 @@ mod tests {
143143 8YsTa5piV8KgJpG/rwYTGXuu3lcCmnWwjmbeDq1zFFrCDDVkaIHkGJgRuFIDPXaH\
144144 yUw5H2HvKlP94ySbvTDLXWZj6TyzHEHDbstqs4DgvurB/bIhi/dQ7zK3EIXL8KRB\
145145 hwIDAQAB\
146- -----END PUBLIC KEY-----"
147- . as_bytes ( ) ,
148- )
149- . expect ( "Failed to create Decoding Key." ) ;
146+ -----END PUBLIC KEY-----",
147+ ) ;
150148
151- let jwt_authorizer = JWTAuthorizer :: new ( decoding_key) . await ;
149+ let jwt_authorizer = JWTAuthorizer :: new ( & decoding_key) . await . unwrap ( ) ;
152150
153151 let valid_jwt_token =
154152 encode ( & Header :: new ( Algorithm :: RS256 ) , & claims, & valid_encoding_key) . unwrap ( ) ;
0 commit comments