Skip to content

Commit

Permalink
fix(rust): fixed enrollment when not successfully completed on first try
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-baldo committed Nov 16, 2023
1 parent bf419f8 commit 84d54cf
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions implementations/rust/ockam/ockam_app_lib/src/enroll/enroll_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ impl AppState {
pub async fn enroll_user(&self) -> Result<()> {
let result = self.enroll_with_token().await;

if let Err(err) = result {
error!(?err, "Failed to enroll user");
self.update_orchestrator_status(OrchestratorStatus::Disconnected);
self.publish_state().await;
self.notify(Notification {
kind: Kind::Error,
title: "Failed to enroll user".to_string(),
message: format!("{}", err),
});
return Err(err);
let enrolled = match result {
Ok(enrolled) => enrolled,
Err(err) => {
error!(?err, "Failed to enroll user");
self.update_orchestrator_status(OrchestratorStatus::Disconnected);
self.publish_state().await;
self.notify(Notification {
kind: Kind::Error,
title: "Failed to enroll user".to_string(),
message: format!("{}", err),
});
return Err(err);
}
};

if !enrolled {
// no error to report but the enrollment was interrupted
return Ok(());
}

// Reset the node manager to include the project's setup, needed to create the relay.
// This is necessary because the project data is used in the worker initialization,
// which can't be rerun manually once the worker is started.
Expand All @@ -61,10 +70,10 @@ impl AppState {
Ok(())
}

async fn enroll_with_token(&self) -> Result<()> {
async fn enroll_with_token(&self) -> Result<bool> {
if self.is_enrolled().await.unwrap_or_default() {
debug!("User is already enrolled");
return Ok(());
return Ok(false);
}

self.update_orchestrator_status(OrchestratorStatus::WaitingForToken);
Expand All @@ -77,10 +86,6 @@ impl AppState {
// retrieve the user information
let user_info = oidc_service.get_user_info(&token).await?;
info!(?user_info, "User info retrieved successfully");
let cli_state = self.state().await;
cli_state
.users_info
.overwrite(&user_info.email, user_info.clone())?;

if !user_info.email_verified {
self.notify(Notification {
Expand All @@ -91,9 +96,18 @@ impl AppState {
Please review your inbox and follow the provided steps \
to complete the verification process"
.to_string(),
})
});
self.update_orchestrator_status(OrchestratorStatus::Disconnected);
self.publish_state().await;
return Ok(false);
}

let cli_state = self.state().await;
cli_state
.users_info
.overwrite(&user_info.email, user_info.clone())?;
cli_state.users_info.set_default(&user_info.email)?;

// enroll the current user using that token on the controller
{
let controller = self.controller().await.into_diagnostic()?;
Expand All @@ -120,7 +134,7 @@ impl AppState {
message: "You can now use the Ockam app".to_string(),
});

Ok(())
Ok(true)
}

async fn retrieve_space(&self) -> Result<Space> {
Expand Down

0 comments on commit 84d54cf

Please sign in to comment.