-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support file type of subject token source (#275)
- Loading branch information
Showing
3 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
foundation/auth/src/token_source/external_account_source/file_credential_source.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::credentials::Format; | ||
|
||
use super::{error::Error, subject_token_source::SubjectTokenSource}; | ||
|
||
pub struct FileCredentialSource { | ||
file: String, | ||
format: Option<Format>, | ||
} | ||
|
||
impl FileCredentialSource { | ||
pub fn new(file: String, format: Option<Format>) -> Self { | ||
Self { file, format } | ||
} | ||
|
||
async fn read_credential(&self) -> Result<String, Error> { | ||
let content = tokio::fs::read_to_string(&self.file).await?; | ||
match self.format.as_ref().map(|f| f.tp.as_str()).unwrap_or("") { | ||
"json" => { | ||
let data: serde_json::Value = serde_json::from_str(&content)?; | ||
if let Some(token) = data[&self.format.as_ref().unwrap().subject_token_field_name].as_str() { | ||
Ok(token.to_string()) | ||
} else { | ||
Err(Error::MissingSubjectTokenFieldName) | ||
} | ||
} | ||
"text" | "" => Ok(content), | ||
_ => Err(Error::UnsupportedFormatType), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SubjectTokenSource for FileCredentialSource { | ||
async fn subject_token(&self) -> Result<String, Error> { | ||
self.read_credential().await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters