BLOG-85 Implement OIDC authentication #93
@ -8,6 +8,10 @@ use crate::{
|
|||||||
application::error::auth_error::AuthError,
|
application::error::auth_error::AuthError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const SESSION_KEY_AUTH_STATE: &str = "auth_state";
|
||||||
|
const SESSION_KEY_AUTH_NONCE: &str = "auth_nonce";
|
||||||
|
const SESSION_KEY_USER: &str = "user";
|
||||||
|
|
||||||
pub fn configure_auth_routes(cfg: &mut web::ServiceConfig) {
|
pub fn configure_auth_routes(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(
|
cfg.service(
|
||||||
web::scope("/auth")
|
web::scope("/auth")
|
||||||
@ -25,11 +29,11 @@ async fn oidc_login_handler(
|
|||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(auth_url) => {
|
Ok(auth_url) => {
|
||||||
if let Err(e) = session.insert("auth_state", auth_url.state) {
|
if let Err(e) = session.insert(SESSION_KEY_AUTH_STATE, auth_url.state) {
|
||||||
log::error!("{e:?}");
|
log::error!("{e:?}");
|
||||||
return HttpResponse::InternalServerError().finish();
|
return HttpResponse::InternalServerError().finish();
|
||||||
}
|
}
|
||||||
if let Err(e) = session.insert("auth_nonce", auth_url.nonce) {
|
if let Err(e) = session.insert(SESSION_KEY_AUTH_NONCE, auth_url.nonce) {
|
||||||
log::error!("{e:?}");
|
log::error!("{e:?}");
|
||||||
return HttpResponse::InternalServerError().finish();
|
return HttpResponse::InternalServerError().finish();
|
||||||
}
|
}
|
||||||
@ -49,12 +53,12 @@ async fn oidc_callback_handler(
|
|||||||
query: web::Query<OidcCallbackQueryDto>,
|
query: web::Query<OidcCallbackQueryDto>,
|
||||||
session: Session,
|
session: Session,
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
let expected_state: String = match session.get("auth_state") {
|
let expected_state: String = match session.get(SESSION_KEY_AUTH_STATE) {
|
||||||
Ok(Some(state)) => state,
|
Ok(Some(state)) => state,
|
||||||
_ => return HttpResponse::BadRequest().finish(),
|
_ => return HttpResponse::BadRequest().finish(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let expected_nonce: String = match session.get("auth_nonce") {
|
let expected_nonce: String = match session.get(SESSION_KEY_AUTH_NONCE) {
|
||||||
Ok(Some(nonce)) => nonce,
|
Ok(Some(nonce)) => nonce,
|
||||||
_ => return HttpResponse::BadRequest().finish(),
|
_ => return HttpResponse::BadRequest().finish(),
|
||||||
};
|
};
|
||||||
@ -63,11 +67,11 @@ async fn oidc_callback_handler(
|
|||||||
.oidc_callback(query.into_inner(), &expected_state, &expected_nonce)
|
.oidc_callback(query.into_inner(), &expected_state, &expected_nonce)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
session.remove("auth_state");
|
session.remove(SESSION_KEY_AUTH_STATE);
|
||||||
session.remove("auth_nonce");
|
session.remove(SESSION_KEY_AUTH_NONCE);
|
||||||
match result {
|
match result {
|
||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
if let Err(e) = session.insert("user", user) {
|
if let Err(e) = session.insert(SESSION_KEY_USER, user) {
|
||||||
log::error!("{e:?}");
|
log::error!("{e:?}");
|
||||||
return HttpResponse::InternalServerError().finish();
|
return HttpResponse::InternalServerError().finish();
|
||||||
}
|
}
|
||||||
@ -87,6 +91,11 @@ async fn oidc_callback_handler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn logout_handler() -> impl Responder {
|
async fn logout_handler(session: Session) -> impl Responder {
|
||||||
HttpResponse::Ok().finish()
|
session.remove(SESSION_KEY_AUTH_STATE);
|
||||||
|
session.remove(SESSION_KEY_AUTH_NONCE);
|
||||||
|
session.remove(SESSION_KEY_USER);
|
||||||
|
HttpResponse::Found()
|
||||||
|
.append_header((header::LOCATION, "/"))
|
||||||
|
.finish()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user