-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a noninteractive authenticator #159
Open
sean-purcell
wants to merge
2
commits into
dermesser:master
Choose a base branch
from
sean-purcell:noninteractive-authenticator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,154 @@ | ||
//! Module containing functionality for serializing tokens and using them at a later point for | ||
//! non-interactive services. | ||
use crate::authenticator::Authenticator; | ||
use crate::client::SendRequest; | ||
use crate::error::Error; | ||
use crate::refresh::RefreshFlow; | ||
use crate::types::{ApplicationSecret, TokenInfo}; | ||
|
||
use hyper_util::client::legacy::connect::Connect; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Clone, Default, Debug)] | ||
struct Entry { | ||
scopes: Vec<String>, | ||
refresh_token: String, | ||
} | ||
|
||
impl Entry { | ||
fn create<T>(scopes: &[T], refresh_token: String) -> Self | ||
where | ||
T: AsRef<str>, | ||
{ | ||
Entry { | ||
scopes: (scopes.iter().map(|x| x.as_ref().to_string()).collect()), | ||
refresh_token, | ||
} | ||
} | ||
|
||
fn is_subset<T>(&self, scopes: &[T]) -> bool | ||
where | ||
T: AsRef<str>, | ||
{ | ||
scopes | ||
.iter() | ||
.all(|scope| self.scopes.iter().any(|s| s.as_str() == scope.as_ref())) | ||
} | ||
} | ||
|
||
/// These tokens are meant to be constructed interactively using another flow, and then can be | ||
/// serialized to be deserialized and used non-interactively later on. Since access tokens are | ||
/// typically short-lived, this authenticator assumes it will be expired and only stores the | ||
/// refresh token. | ||
#[derive(Deserialize, Serialize, Clone, Default, Debug)] | ||
pub struct NoninteractiveTokens { | ||
app_secret: ApplicationSecret, | ||
refresh_tokens: Vec<Entry>, | ||
} | ||
|
||
impl NoninteractiveTokens { | ||
fn entry_for_scopes<T>(&self, scopes: &[T]) -> Option<&Entry> | ||
where | ||
T: AsRef<str>, | ||
{ | ||
self.refresh_tokens | ||
.iter() | ||
.find(|entry| entry.is_subset(scopes)) | ||
} | ||
|
||
/// Create a builder using an existing authenticator to get tokens interactively, which can be | ||
/// saved and used later non-interactively.. | ||
pub fn builder<'a, C>( | ||
authenticator: &'a Authenticator<C>, | ||
) -> Result<NoninteractiveTokensBuilder<'a, C>, Error> | ||
where | ||
C: Connect + Clone + Send + Sync + 'static, | ||
{ | ||
let app_secret = (match authenticator.app_secret() { | ||
Some(secret) => Ok(secret.clone()), | ||
None => Err(Error::UserError( | ||
"No application secret present in authenticator".into(), | ||
)), | ||
})?; | ||
|
||
Ok(NoninteractiveTokensBuilder { | ||
authenticator, | ||
tokens: NoninteractiveTokens { | ||
app_secret, | ||
refresh_tokens: vec![], | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
/// A builder to construct `NoninteractiveTokens` using an existing authenticator. | ||
#[derive(Clone)] | ||
pub struct NoninteractiveTokensBuilder<'a, C> | ||
where C: Connect + Clone + Send + Sync + 'static { | ||
authenticator: &'a Authenticator<C>, | ||
tokens: NoninteractiveTokens, | ||
} | ||
|
||
impl<'a, C> NoninteractiveTokensBuilder<'a, C> | ||
where | ||
C: Connect + Clone + Send + Sync + 'static, | ||
{ | ||
/// Finalize the `NoninteractiveTokens`. | ||
pub fn build(self) -> NoninteractiveTokens { | ||
self.tokens | ||
} | ||
|
||
/// Add a cached refresh token for a given set of scopes. | ||
pub async fn add_token_for<T>( | ||
mut self, | ||
scopes: &[T], | ||
force_refresh: bool, | ||
) -> Result<NoninteractiveTokensBuilder<'a, C>, Error> | ||
where | ||
T: AsRef<str>, | ||
{ | ||
let info = self.authenticator.find_token_info(scopes, force_refresh).await?; | ||
match info.refresh_token { | ||
Some(token) => { | ||
self.tokens | ||
.refresh_tokens | ||
.push(Entry::create(scopes, token.clone())); | ||
Ok(self) | ||
} | ||
None => Err(Error::UserError( | ||
"Returned token doesn't contain a refresh token".into(), | ||
)), | ||
} | ||
} | ||
} | ||
|
||
/// A flow that uses a `NoninteractiveTokens` instance to provide access tokens. | ||
pub struct NoninteractiveFlow(pub(crate) NoninteractiveTokens); | ||
|
||
impl NoninteractiveFlow { | ||
pub(crate) fn app_secret(&self) -> &ApplicationSecret { | ||
&self.0.app_secret | ||
} | ||
|
||
pub(crate) async fn token<T>( | ||
&self, | ||
hyper_client: &impl SendRequest, | ||
scopes: &[T], | ||
) -> Result<TokenInfo, Error> | ||
where | ||
T: AsRef<str> | ||
{ | ||
let refresh_token = (match self.0.entry_for_scopes(scopes) { | ||
None => Err(Error::UserError(format!( | ||
"No matching token found for scopes {:?}", | ||
scopes | ||
.iter() | ||
.map(|x| x.as_ref().to_string()) | ||
.collect::<Vec<_>>() | ||
))), | ||
Some(entry) => Ok(&entry.refresh_token), | ||
})?; | ||
|
||
RefreshFlow::refresh_token(hyper_client, self.app_secret(), refresh_token.as_str()).await | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be that you have good reasons to implement this, but it looks similar to
storage::JSONTokens
. Maybe you can reuse that type, as the storage requirements seem to be very similar (store/retrieve by scope).