Skip to content

Commit

Permalink
Add CustomServiceAccount::with_subject() method
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Apr 9, 2024
1 parent d1e8e4e commit d598f98
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/custom_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct CustomServiceAccount {
credentials: ApplicationCredentials,
signer: Signer,
tokens: RwLock<HashMap<Vec<String>, Token>>,
subject: Option<String>,
}

impl CustomServiceAccount {
Expand Down Expand Up @@ -57,11 +58,18 @@ impl CustomServiceAccount {
}
}

/// Set the `subject` to impersonate a user
pub fn with_subject(mut self, subject: String) -> Self {
self.subject = Some(subject);
self
}

fn new(credentials: ApplicationCredentials) -> Result<Self, Error> {
Ok(Self {
signer: Signer::new(&credentials.private_key)?,
credentials,
tokens: RwLock::new(HashMap::new()),
subject: None,
})
}

Expand Down Expand Up @@ -100,7 +108,8 @@ impl ServiceAccount for CustomServiceAccount {
use hyper::header;
use url::form_urlencoded;

let jwt = Claims::new(&self.credentials, scopes, None).to_jwt(&self.signer)?;
let jwt =
Claims::new(&self.credentials, scopes, self.subject.as_deref()).to_jwt(&self.signer)?;
let rqbody = form_urlencoded::Serializer::new(String::new())
.extend_pairs(&[("grant_type", GRANT_TYPE), ("assertion", jwt.as_str())])
.finish();
Expand Down

0 comments on commit d598f98

Please sign in to comment.