Skip to content

Commit

Permalink
refactor: cache static parameters in initialization
Browse files Browse the repository at this point in the history
Co-authored-by: Tommy Trøen <[email protected]>
Co-authored-by: Kim Tore Jensen <[email protected]>
  • Loading branch information
3 people committed Oct 30, 2024
1 parent b271ebe commit 7cb380f
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/identity_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ pub trait Provider<T: Serialize> {
fn introspect(&mut self, token: String) -> impl std::future::Future<Output=HashMap<String, Value>> + Send;
}

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Maskinporten {
pub cfg: Config,
private_jwk: jwk::JsonWebKey,
private_jwk: jwt::EncodingKey,
client_assertion_header: jwt::Header,
upstream_jwks: jwks::Jwks,
}


#[derive(Clone, Debug)]
pub struct EntraID(pub Config);

Expand Down Expand Up @@ -48,9 +48,7 @@ pub struct TokenXTokenRequest {}

impl Provider<TokenXTokenRequest> for TokenX {
fn token_request(&self, _target: String) -> TokenXTokenRequest {
TokenXTokenRequest {

}
TokenXTokenRequest {}
}

fn token_endpoint(&self) -> String {
Expand Down Expand Up @@ -82,16 +80,10 @@ impl Provider<MaskinportenTokenRequest> for Maskinporten {
aud: self.cfg.maskinporten_issuer.to_string(),
};

let encoding_key: jwt::EncodingKey = self.private_jwk.key.to_encoding_key();
let alg: jwt::Algorithm = self.private_jwk.algorithm.unwrap().into();
let kid: String = self.private_jwk.key_id.clone().unwrap();
let mut header = jwt::Header::new(alg);
header.kid = Some(kid);

let token = jwt::encode(
&header,
&self.client_assertion_header,
&claims,
&encoding_key,
&self.private_jwk,
).unwrap();

MaskinportenTokenRequest {
Expand Down Expand Up @@ -121,11 +113,18 @@ impl Provider<MaskinportenTokenRequest> for Maskinporten {

impl Maskinporten {
pub fn new(cfg: Config, upstream_jwks: jwks::Jwks) -> Self {
let the_jwk: jwk::JsonWebKey = cfg.maskinporten_client_jwk.parse().unwrap();
let client_private_jwk: jwk::JsonWebKey = cfg.maskinporten_client_jwk.parse().unwrap();
let alg: jwt::Algorithm = client_private_jwk.algorithm.unwrap().into();
let kid: String = client_private_jwk.key_id.clone().unwrap();

let mut header = jwt::Header::new(alg);
header.kid = Some(kid);

Self {
cfg,
upstream_jwks,
private_jwk: the_jwk,
private_jwk: client_private_jwk.key.to_encoding_key(),
client_assertion_header: header,
}
}
}
Expand Down

0 comments on commit 7cb380f

Please sign in to comment.