Skip to content

Commit d598f98

Browse files
committed
Add CustomServiceAccount::with_subject() method
1 parent d1e8e4e commit d598f98

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/custom_service_account.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct CustomServiceAccount {
2525
credentials: ApplicationCredentials,
2626
signer: Signer,
2727
tokens: RwLock<HashMap<Vec<String>, Token>>,
28+
subject: Option<String>,
2829
}
2930

3031
impl CustomServiceAccount {
@@ -57,11 +58,18 @@ impl CustomServiceAccount {
5758
}
5859
}
5960

61+
/// Set the `subject` to impersonate a user
62+
pub fn with_subject(mut self, subject: String) -> Self {
63+
self.subject = Some(subject);
64+
self
65+
}
66+
6067
fn new(credentials: ApplicationCredentials) -> Result<Self, Error> {
6168
Ok(Self {
6269
signer: Signer::new(&credentials.private_key)?,
6370
credentials,
6471
tokens: RwLock::new(HashMap::new()),
72+
subject: None,
6573
})
6674
}
6775

@@ -100,7 +108,8 @@ impl ServiceAccount for CustomServiceAccount {
100108
use hyper::header;
101109
use url::form_urlencoded;
102110

103-
let jwt = Claims::new(&self.credentials, scopes, None).to_jwt(&self.signer)?;
111+
let jwt =
112+
Claims::new(&self.credentials, scopes, self.subject.as_deref()).to_jwt(&self.signer)?;
104113
let rqbody = form_urlencoded::Serializer::new(String::new())
105114
.extend_pairs(&[("grant_type", GRANT_TYPE), ("assertion", jwt.as_str())])
106115
.finish();

0 commit comments

Comments
 (0)