-
Notifications
You must be signed in to change notification settings - Fork 42
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
Refactor login endpoints, instrument console endpoints #7374
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,104 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
use dropshot::{http_response_found, HttpError, HttpResponseFound}; | ||
use nexus_auth::context::OpContext; | ||
use nexus_db_model::{ConsoleSession, Name}; | ||
use nexus_db_queries::authn::silos::IdentityProviderType; | ||
use nexus_db_queries::db::identity::Asset; | ||
use nexus_types::external_api::{params::RelativeUri, shared::RelayState}; | ||
use omicron_common::api::external::Error; | ||
|
||
impl super::Nexus { | ||
pub(crate) async fn login_saml_redirect( | ||
&self, | ||
opctx: &OpContext, | ||
silo_name: &Name, | ||
provider_name: &Name, | ||
redirect_uri: Option<RelativeUri>, | ||
) -> Result<HttpResponseFound, HttpError> { | ||
let (.., identity_provider) = self | ||
.datastore() | ||
.identity_provider_lookup(&opctx, silo_name, provider_name) | ||
.await?; | ||
|
||
match identity_provider { | ||
IdentityProviderType::Saml(saml_identity_provider) => { | ||
// Relay state is sent to the IDP, to be sent back to the SP | ||
// after a successful login. | ||
let relay_state = | ||
RelayState { redirect_uri }.to_encoded().map_err(|e| { | ||
HttpError::for_internal_error(format!( | ||
"encoding relay state failed: {}", | ||
e | ||
)) | ||
})?; | ||
|
||
let sign_in_url = saml_identity_provider | ||
.sign_in_url(Some(relay_state)) | ||
.map_err(|e| { | ||
HttpError::for_internal_error(e.to_string()) | ||
})?; | ||
|
||
http_response_found(sign_in_url) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) async fn login_saml( | ||
&self, | ||
opctx: &OpContext, | ||
body_bytes: dropshot::UntypedBody, | ||
silo_name: &Name, | ||
provider_name: &Name, | ||
) -> Result<(ConsoleSession, String), HttpError> { | ||
let (authz_silo, db_silo, identity_provider) = self | ||
.datastore() | ||
.identity_provider_lookup(&opctx, silo_name, provider_name) | ||
.await?; | ||
let (authenticated_subject, relay_state_string) = | ||
match identity_provider { | ||
IdentityProviderType::Saml(saml_identity_provider) => { | ||
let body_bytes = dbg!(body_bytes.as_str())?; | ||
saml_identity_provider.authenticated_subject( | ||
&body_bytes, | ||
self.samael_max_issue_delay(), | ||
)? | ||
} | ||
}; | ||
let relay_state = | ||
relay_state_string.and_then(|v| RelayState::from_encoded(v).ok()); | ||
let user = self | ||
.silo_user_from_authenticated_subject( | ||
&opctx, | ||
&authz_silo, | ||
&db_silo, | ||
&authenticated_subject, | ||
) | ||
.await?; | ||
let session = self.create_session(opctx, user).await?; | ||
let next_url = relay_state | ||
.and_then(|r| r.redirect_uri) | ||
.map(|u| u.to_string()) | ||
.unwrap_or_else(|| "/".to_string()); | ||
Ok((session, next_url)) | ||
} | ||
|
||
// TODO: move this logic, it's weird | ||
pub(crate) async fn create_session( | ||
&self, | ||
opctx: &OpContext, | ||
user: Option<nexus_db_queries::db::model::SiloUser>, | ||
) -> Result<ConsoleSession, Error> { | ||
let session = match user { | ||
Some(user) => self.session_create(&opctx, user.id()).await?, | ||
None => Err(Error::Unauthenticated { | ||
internal_message: String::from( | ||
"no matching user found or credentials were not valid", | ||
), | ||
})?, | ||
}; | ||
Ok(session) | ||
} | ||
} |
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
Oops, something went wrong.
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.
I still don't like this method, but at least it's a few lines shorter now