feat(handler): Implement OIDC login & callback handlers

This commit is contained in:
Vincent Ambo 2018-04-08 22:37:36 +02:00
parent 115f50ae37
commit a63bc782b0

View file

@ -12,6 +12,7 @@ use db::*;
use actix::prelude::{Addr, Syn}; use actix::prelude::{Addr, Syn};
use futures::Future; use futures::Future;
use errors::{Result, ConverseError}; use errors::{Result, ConverseError};
use oidc::*;
type ConverseResponse = Box<Future<Item=HttpResponse, Error=ConverseError>>; type ConverseResponse = Box<Future<Item=HttpResponse, Error=ConverseError>>;
@ -20,6 +21,9 @@ pub struct AppState {
/// Address of the database actor /// Address of the database actor
pub db: Addr<Syn, DbExecutor>, pub db: Addr<Syn, DbExecutor>,
/// Address of the OIDC actor
pub oidc: Addr<Syn, OidcExecutor>,
/// Compiled templates /// Compiled templates
pub tera: tera::Tera, pub tera: tera::Tera,
} }
@ -104,3 +108,22 @@ pub fn reply_thread(state: State<AppState>, input: Form<NewPost>) -> ConverseRes
}) })
.responder() .responder()
} }
/// This handler initiates an OIDC login.
pub fn login(state: State<AppState>) -> ConverseResponse {
state.oidc.send(GetLoginUrl)
.from_err()
.and_then(|url| Ok(HttpResponse::TemporaryRedirect()
.header("Location", url)
.finish()))
.responder()
}
pub fn callback(state: State<AppState>, data: Form<CodeResponse>) -> ConverseResponse {
state.oidc.send(RetrieveToken(data.0))
.from_err()
.and_then(|author| {
Ok(HttpResponse::from(format!("{:?}", author)))
})
.responder()
}