-
Notifications
You must be signed in to change notification settings - Fork 180
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
Authentication #37
Merged
Merged
Authentication #37
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
543bd95
Initial authentication implementation
enderger 23027eb
Store user info in the database, improve encapsulation in profiles
enderger cfe6c60
Add user list, remove unused dependencies, add spantraces
enderger a2e3a9e
Implement user remove, update UUID crate
enderger 311b1da
Add user set-default
enderger 48d3f70
Revert submodule macro usage
enderger b8c95a8
Make tracing significantly less verbose
enderger 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,100 @@ | ||
//! Authentication flow interface | ||
use crate::{launcher::auth as inner, State}; | ||
use futures::prelude::*; | ||
use tokio::sync::oneshot; | ||
|
||
pub use inner::Credentials; | ||
|
||
/// Authenticate a user with Hydra | ||
/// To run this, you need to first spawn this function as a task, then | ||
/// open a browser to the given URL and finally wait on the spawned future | ||
/// with the ability to cancel in case the browser is closed before finishing | ||
#[tracing::instrument] | ||
pub async fn authenticate( | ||
browser_url: oneshot::Sender<url::Url>, | ||
) -> crate::Result<Credentials> { | ||
let mut flow = inner::HydraAuthFlow::new().await?; | ||
let state = State::get().await?; | ||
let mut users = state.users.write().await; | ||
|
||
let url = flow.prepare_login_url().await?; | ||
browser_url.send(url).map_err(|url| { | ||
crate::ErrorKind::OtherError(format!( | ||
"Error sending browser url to parent: {url}" | ||
)) | ||
})?; | ||
|
||
let credentials = flow.extract_credentials().await?; | ||
users.insert(&credentials)?; | ||
|
||
if state.settings.read().await.default_user.is_none() { | ||
let mut settings = state.settings.write().await; | ||
settings.default_user = Some(credentials.id); | ||
} | ||
|
||
Ok(credentials) | ||
} | ||
|
||
/// Refresh some credentials using Hydra, if needed | ||
#[tracing::instrument] | ||
pub async fn refresh( | ||
user: uuid::Uuid, | ||
update_name: bool, | ||
) -> crate::Result<Credentials> { | ||
let state = State::get().await?; | ||
let mut users = state.users.write().await; | ||
|
||
futures::future::ready(users.get(user)?.ok_or_else(|| { | ||
crate::ErrorKind::OtherError(format!( | ||
"Tried to refresh nonexistent user with ID {user}" | ||
)) | ||
.as_error() | ||
})) | ||
.and_then(|mut credentials| async move { | ||
if chrono::offset::Utc::now() > credentials.expires { | ||
inner::refresh_credentials(&mut credentials).await?; | ||
if update_name { | ||
inner::refresh_username(&mut credentials).await?; | ||
} | ||
} | ||
users.insert(&credentials)?; | ||
Ok(credentials) | ||
}) | ||
.await | ||
} | ||
|
||
/// Remove a user account from the database | ||
#[tracing::instrument] | ||
pub async fn remove_user(user: uuid::Uuid) -> crate::Result<()> { | ||
let state = State::get().await?; | ||
let mut users = state.users.write().await; | ||
|
||
if state.settings.read().await.default_user == Some(user) { | ||
let mut settings = state.settings.write().await; | ||
settings.default_user = users | ||
.0 | ||
.first()? | ||
.map(|it| uuid::Uuid::from_slice(&it.0)) | ||
.transpose()?; | ||
} | ||
|
||
users.remove(user)?; | ||
Ok(()) | ||
} | ||
|
||
/// Check if a user exists in Theseus | ||
#[tracing::instrument] | ||
pub async fn has_user(user: uuid::Uuid) -> crate::Result<bool> { | ||
let state = State::get().await?; | ||
let users = state.users.read().await; | ||
|
||
Ok(users.contains(user)?) | ||
} | ||
|
||
/// Get a copy of the list of all user credentials | ||
#[tracing::instrument] | ||
pub async fn users() -> crate::Result<Box<[Credentials]>> { | ||
let state = State::get().await?; | ||
let users = state.users.read().await; | ||
users.iter().collect() | ||
} |
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
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.
are these macros needed everywhere with this lib?
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.
Only on functions which need to be known about in the spantrace. Unfortunately, since async Rust doesn't have any real consistent stack to trace usefully, we need to use the macro to determine where we are in actual control flow.