add tera template

This commit is contained in:
Manuel Gugger 2021-11-29 17:35:11 +01:00
parent 66d747fa32
commit b7152c47eb
5 changed files with 39 additions and 31 deletions

View File

@ -9,6 +9,7 @@ edition = "2018"
actix-web = "4.0.0-beta.12"
actix-rt = "2.5.0"
actix-session = "0.5.0-beta.4"
tera = "1.15.0"
oauth2 = "4.1"
base64 = "0.13.0"

View File

@ -3,6 +3,7 @@ extern crate serde_derive;
use actix_session::{Session, CookieSession};
use actix_web::{web, App, HttpResponse, HttpServer};
use tera::{ Tera, Context};
use oauth2::basic::BasicClient;
use oauth2::{
AuthUrl, ClientId, ClientSecret,
@ -15,25 +16,17 @@ mod web_auth;
pub struct AppState {
pub oauth: BasicClient,
pub api_base_url: String,
pub tmpl: Tera
}
fn index(session: Session) -> HttpResponse {
let login = session.get::<String>("login").unwrap();
let link = if login.is_some() { "logout" } else { "login" };
fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
let login = session.get::<web_auth::UserInfo>("user_info").unwrap();
let web_auth_link = if login.is_some() { "logout" } else { "login" };
let html = format!(
r#"<html>
<head><title>OAuth2 Test</title></head>
<body>
{} <a href="/{}">{}</a>
</body>
</html>"#,
login.unwrap_or("".to_string()),
link,
link
);
HttpResponse::Ok().body(html)
let mut ctx = Context::new();
ctx.insert("web_auth_link", web_auth_link);
let rendered = data.tmpl.render("index.html", &ctx).unwrap();
HttpResponse::Ok().body(rendered)
}
#[actix_rt::main]
@ -70,10 +63,16 @@ async fn main() {
.expect("Invalid redirect URL"),
);
let tera =
Tera::new(
concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")
).unwrap();
let app_state = web::Data::new(
AppState {
oauth: client,
api_base_url,
tmpl: tera
}
);

View File

@ -29,14 +29,14 @@ pub fn login(data: web::Data<super::AppState>) -> HttpResponse {
}
pub fn logout(session: Session) -> HttpResponse {
session.remove("login");
session.remove("user_info");
HttpResponse::Found()
.append_header((header::LOCATION, "/".to_string()))
.finish()
}
#[allow(non_snake_case)]
#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct UserInfo {
mail: String,
userPrincipalName: String,
@ -98,18 +98,7 @@ pub async fn auth(
let user_info = read_user(&data.api_base_url, token.access_token()).await;
session.insert("login", user_info.displayName.to_string()).unwrap();
session.insert("user_info", &user_info).unwrap();
let html = format!(
r#"<html>
<head><title>OAuth2 Test</title></head>
<body>
User info:
<pre>{:?}</pre>
<a href="/">Home</a>
</body>
</html>"#,
user_info
);
HttpResponse::Ok().body(html)
HttpResponse::Found().append_header(("location", "/")).finish()
}

14
templates/base.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Actix Web</title>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>

5
templates/index.html Normal file
View File

@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block content %}
<a href="/{{ web_auth_link }}">{{ web_auth_link }}</a>
{% endblock content %}