Skip to content
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

feat(IAM-170): adding slack to whoami #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() -> Result<(), Error> {
info!("starting dino-park-whoami");
let s = settings::Settings::new()?;
let client = cis_client::CisClient::from_settings(&s.cis)?;
info!("initialized cis_client");
let secret = base64::decode(&s.whoami.secret)?;
let ttl_cache = Arc::new(RwLock::new(TtlCache::<String, String>::new(2000)));

Expand Down
58 changes: 23 additions & 35 deletions src/slack/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,26 @@ pub struct SlackClientHandlers {
identity: Arc<BasicClient>,
}

fn send_response(url: &str) -> HttpResponse {
HttpResponse::Found()
.header(http::header::LOCATION, url)
.finish()
}

fn send_error_response() -> HttpResponse {
HttpResponse::Found()
.header(http::header::LOCATION, "/e?identityAdded=error")
.finish()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor niggle: we could call send_response here

}

/**
* First redirect that handles getting authorization for identity scopes
*/
fn redirect_identity(client: web::Data<SlackClientHandlers>, session: Session) -> impl Responder {
let (authorize_url, csrf_state) = client.identity.authorize_url(CsrfToken::new_random);
session
.set("identity_csrf_state", csrf_state.secret().clone())
.map(|_| {
HttpResponse::Found()
.header(http::header::LOCATION, authorize_url.to_string())
.finish()
})
.map(|_| send_response(&authorize_url.to_string()))
}

/**
Expand All @@ -136,26 +144,18 @@ fn redirect_im(
let state = CsrfToken::new(query.state.clone());
if let Some(ref must_state) = session.get::<String>("identity_csrf_state").unwrap() {
if must_state != state.secret() {
return session.set("im_csrf_state", "").map(|_| {
HttpResponse::Found()
.header(http::header::LOCATION, "/e?identityAdded=error")
.finish()
});
return session
.set("im_csrf_state", "")
.map(|_| send_error_response());
}
} else {
return session.set("im_csrf_state", "").map(|_| {
HttpResponse::Found()
.header(http::header::LOCATION, "/e?identityAdded=error")
.finish()
});
return session
.set("im_csrf_state", "")
.map(|_| send_error_response());
}
session
.set("im_csrf_state", csrf_state.secret().clone())
.map(|_| {
HttpResponse::Found()
.header(http::header::LOCATION, authorize_url.to_string())
.finish()
})
.map(|_| send_response(&authorize_url.to_string()))
}

fn auth<T: AsyncCisClientTrait + 'static>(
Expand All @@ -176,18 +176,10 @@ fn auth<T: AsyncCisClientTrait + 'static>(
// Check state token from im_crsf_state
if let Some(ref must_state) = session.get::<String>("im_csrf_state").unwrap() {
if must_state != state.secret() {
return Box::new(future::ok(
HttpResponse::Found()
.header(http::header::LOCATION, "/e?identityAdded=error")
.finish(),
));
return Box::new(future::ok(send_error_response()));
}
} else {
return Box::new(future::ok(
HttpResponse::Found()
.header(http::header::LOCATION, "/e?identityAdded=error")
.finish(),
));
return Box::new(future::ok(send_error_response()));
}
let get = cis_client.clone();
let get_uid = user_id.user_id.clone();
Expand Down Expand Up @@ -263,11 +255,7 @@ fn auth<T: AsyncCisClientTrait + 'static>(
.update_user(&user_id.user_id, profile)
.map_err(Into::into)
})
.and_then(|_| {
HttpResponse::Found()
.header(http::header::LOCATION, "/e?identityAdded=slack")
.finish()
}),
.and_then(|_| send_response("/e?identityAdded=slack")),
)
}

Expand Down