Update to newest versions of all crates
This commit is contained in:
parent
da8582a02f
commit
00225a2f09
|
|
@ -5,40 +5,41 @@
|
|||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Debug Game",
|
||||
"preLaunchTask": "godot-rust: Build Debug",
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"program": "${config:godot-rust.environment.godotEditorPath}",
|
||||
"cwd": "${config:godot-rust.environment.godotProjectPath}",
|
||||
"presentation": {
|
||||
"group": "Debug",
|
||||
"order": 1
|
||||
"name": "Debug executable 'jean-marie'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=jean-marie",
|
||||
"--package=jean-marie"
|
||||
],
|
||||
"filter": {
|
||||
"name": "jean-marie",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"name": "Release Game",
|
||||
"preLaunchTask": "godot-rust: Build Release",
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"program": "${config:godot-rust.environment.godotEditorPath}",
|
||||
"cwd": "${config:godot-rust.environment.godotProjectPath}",
|
||||
"presentation": {
|
||||
"group": "Debug",
|
||||
"order": 2
|
||||
"name": "Debug unit tests in executable 'jean-marie'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=jean-marie",
|
||||
"--package=jean-marie"
|
||||
],
|
||||
"filter": {
|
||||
"name": "jean-marie",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "Attach to Game",
|
||||
"pid": "${command:pickMyProcess}",
|
||||
"type": "lldb",
|
||||
"request": "attach",
|
||||
"program": "${config:godot-rust.environment.godotEditorPath}",
|
||||
"port": 6007,
|
||||
"presentation": {
|
||||
"group": "Debug",
|
||||
"order": 3
|
||||
}
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
32
Cargo.toml
32
Cargo.toml
|
|
@ -1,25 +1,27 @@
|
|||
[package]
|
||||
name = "jean-marie"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
# Update all dependencies with `cargo upgrade -i allow && cargo update`
|
||||
[dependencies]
|
||||
axum = { version = "0.5.16", features = ["headers"] }
|
||||
axum_database_sessions = { version = "4.1.0", features = [ "postgres-rustls"] }
|
||||
axum = { version = "0.7.5" }
|
||||
axum_session = { version = "0.14.2" }
|
||||
axum-server = { version = "0.7.1" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.68"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
#serde_json = "1.0.68"
|
||||
tokio = { version = "1.40", features = ["full"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version="0.3", features = ["env-filter"] }
|
||||
uuid = { version = "1.1.2", features = ["v4", "serde"] }
|
||||
async-session = "3.0.0"
|
||||
askama = "0.11"
|
||||
oauth2 = "4.1"
|
||||
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "json"] }
|
||||
headers = "0.3"
|
||||
http = "0.2"
|
||||
tower-http = { version = "0.3.4", features = ["full"] }
|
||||
sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "postgres", "macros", "migrate", "chrono", "json"]}
|
||||
anyhow = "1.0"
|
||||
#uuid = { version = "1.1.2", features = ["v4", "serde"] }
|
||||
#async-session = "3.0.0"
|
||||
askama = "0.12"
|
||||
oauth2 = "4.4"
|
||||
#reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "json"] }
|
||||
#headers = "0.3"
|
||||
http = "1.1"
|
||||
tower-http = { version = "0.5.2", features = ["full"] }
|
||||
#sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "postgres", "macros", "migrate", "chrono", "json"]}
|
||||
#anyhow = "1.0"
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
use async_session::{MemoryStore, Session, SessionStore as _};
|
||||
use axum_session::{Session, SessionAnyPool, SessionConfig, SessionStore, SessionLayer};
|
||||
use axum::{
|
||||
extract::{Extension, Query},
|
||||
http::header::{HeaderMap, SET_COOKIE},
|
||||
response::{IntoResponse, Redirect},
|
||||
};
|
||||
use axum_server;
|
||||
use oauth2::{
|
||||
basic::BasicClient, reqwest::async_http_client, AuthUrl, AuthorizationCode, ClientId,
|
||||
ClientSecret, CsrfToken, PkceCodeChallenge, RedirectUrl, Scope, TokenUrl,
|
||||
|
|
@ -11,9 +12,6 @@ use oauth2::{
|
|||
use serde::Deserialize;
|
||||
use std::{collections::HashMap, env};
|
||||
|
||||
// use crate::User;
|
||||
use crate::COOKIE_NAME;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct AuthRequest {
|
||||
|
|
@ -40,22 +38,23 @@ pub async fn google_auth() -> impl IntoResponse {
|
|||
Redirect::to(&auth_url.to_string())
|
||||
}
|
||||
|
||||
pub async fn google_authorized(
|
||||
pub async fn google_authorized(session: Session<SessionAnyPool>,
|
||||
Query(query): Query<AuthRequest>,
|
||||
Extension(store): Extension<MemoryStore>,
|
||||
Extension(oauth_clients): Extension<HashMap<&str, BasicClient>>,
|
||||
// Extension(oauth_clients): Extension<HashMap<&str, BasicClient>>,
|
||||
) -> impl IntoResponse {
|
||||
// Check for Google client
|
||||
if oauth_clients.contains_key("Google") {
|
||||
// Get Google client
|
||||
let google_oauth_client = oauth_clients.get(&"Google").unwrap();
|
||||
// if oauth_clients.contains_key("Google") {
|
||||
|
||||
println!("{:?}", query);
|
||||
|
||||
// Get an auth token
|
||||
let token = google_oauth_client
|
||||
.exchange_code(AuthorizationCode::new(query.code.clone()))
|
||||
.request_async(async_http_client)
|
||||
.await
|
||||
.unwrap();
|
||||
//let token = match google_oauth_client
|
||||
//.exchange_code(AuthorizationCode::new(query.code.clone()))
|
||||
//.request_async(async_http_client)
|
||||
//.await {
|
||||
// Ok(token) => token,
|
||||
// Err(_) => panic!("Didn't get a token"),
|
||||
// };
|
||||
/*
|
||||
// Fetch user data from google
|
||||
let client = reqwest::Client::new();
|
||||
|
|
@ -71,22 +70,8 @@ pub async fn google_authorized(
|
|||
.unwrap();
|
||||
*/
|
||||
|
||||
// Create a new session filled with user data
|
||||
let session = Session::new();
|
||||
//session.insert("user", &user_data).unwrap();
|
||||
|
||||
// Store session and get corresponding cookie
|
||||
let cookie = store.store_session(session).await.unwrap().unwrap();
|
||||
|
||||
// Build the cookie
|
||||
let cookie = format!("{}={}; SameSite=Lax; Path=/", COOKIE_NAME, cookie);
|
||||
|
||||
// Set cookie
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(SET_COOKIE, cookie.parse().unwrap());
|
||||
|
||||
//(headers, Redirect::to("/dashboard".parse().unwrap()))
|
||||
}
|
||||
// }
|
||||
|
||||
let mut page = String::new();
|
||||
page.push_str(&"Display the data returned by Google\n".to_string());
|
||||
|
|
@ -100,6 +85,13 @@ pub async fn google_authorized(
|
|||
}
|
||||
|
||||
pub fn google_oauth_client() -> BasicClient {
|
||||
if std::env::var_os("GOOGLE_CLIENT_ID").is_none() {
|
||||
std::env::set_var("GOOGLE_CLIENT_ID", "735264084619-clsmvgdqdmum4rvrcj0kuk28k9agir1c.apps.googleusercontent.com")
|
||||
}
|
||||
if std::env::var_os("GOOGLE_CLIENT_SECRET").is_none() {
|
||||
std::env::set_var("GOOGLE_CLIENT_SECRET", "L6uI7FQGoMJd-ay1HO_iGJ6M")
|
||||
}
|
||||
|
||||
let redirect_url = env::var("REDIRECT_URL")
|
||||
.unwrap_or_else(|_| "http://localhost:40192/auth/google".to_string());
|
||||
// .unwrap_or_else(|_| "https://www.jean-marie.ca/auth/google".to_string());
|
||||
|
|
|
|||
86
src/main.rs
86
src/main.rs
|
|
@ -1,14 +1,18 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::{net::SocketAddr, collections::HashMap};
|
||||
use askama::Template;
|
||||
use axum_database_sessions::{AxumSession, AxumPgPool, AxumSessionConfig, AxumSessionStore, AxumSessionLayer};
|
||||
use axum_session::{Session, SessionAnyPool, SessionConfig, SessionStore, SessionLayer};
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{get, get_service}, response::{Html, IntoResponse, Response}
|
||||
routing::{get, get_service}, response::{Html, IntoResponse, Response}, Extension
|
||||
};
|
||||
use http::StatusCode;
|
||||
use oauth2::basic::BasicClient;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
mod google_oauth;
|
||||
use google_oauth::*;
|
||||
|
||||
struct HtmlTemplate<T>(T);
|
||||
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
|
|
@ -54,64 +58,70 @@ async fn main() {
|
|||
// initialize tracing
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let session_config = AxumSessionConfig::default()
|
||||
let session_config = SessionConfig::default()
|
||||
.with_table_name("sessions");
|
||||
|
||||
let session_store = AxumSessionStore::<AxumPgPool>::new(None, session_config);
|
||||
session_store.initiate().await.unwrap();
|
||||
let session_store = SessionStore::<SessionAnyPool>::new(None, session_config).await.unwrap();
|
||||
|
||||
// Create HashMap to store oauth configurations
|
||||
// let mut oauth_clients = HashMap::<&str, BasicClient>::new();
|
||||
|
||||
// Get the client structures
|
||||
// let facebook_oauth_client = facebook_oauth_client();
|
||||
// let discord_oauth_client = discord_oauth_client();
|
||||
// let google_oauth_client = google_oauth_client();
|
||||
|
||||
// Get oauth clients for the hashmap
|
||||
// oauth_clients.insert("Facebook", facebook_oauth_client);
|
||||
// oauth_clients.insert("Discord", discord_oauth_client);
|
||||
// oauth_clients.insert("Google", google_oauth_client);
|
||||
|
||||
// build our application with some routes
|
||||
let app = Router::new()
|
||||
.nest(
|
||||
"/assets",
|
||||
get_service(ServeDir::new("templates/assets")).handle_error(
|
||||
|error: std::io::Error| async move {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
)
|
||||
},
|
||||
),
|
||||
.route("/assets", get_service(ServeDir::new("templates/assets"))
|
||||
// .handle_error(
|
||||
// |error: std::io::Error| async move {
|
||||
// (
|
||||
// StatusCode::INTERNAL_SERVER_ERROR,
|
||||
// format!("Unhandled internal error: {}", error),
|
||||
// )
|
||||
// },
|
||||
// ),
|
||||
)
|
||||
.route("/", get(index))
|
||||
.route("/login", get(login))
|
||||
.layer(AxumSessionLayer::new(session_store));
|
||||
.route("/google_auth", get(google_auth))
|
||||
.route("/auth/google", get(google_authorized))
|
||||
.layer(SessionLayer::new(session_store))
|
||||
// .layer(Extension(oauth_clients));
|
||||
;
|
||||
|
||||
// run it
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 40192));
|
||||
tracing::debug!("listening on {}", addr);
|
||||
axum::Server::bind(&addr)
|
||||
axum_server::bind(addr)
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn index(session: AxumSession<AxumPgPool>) -> impl IntoResponse {
|
||||
let logged_in = session.get("logged_in").await.unwrap_or(false);
|
||||
let name = session.get("name").await.unwrap_or("You're not logged in.".to_string());
|
||||
async fn index(session: Session<SessionAnyPool>) -> impl IntoResponse {
|
||||
let logged_in = session.get("logged_in").unwrap_or(false);
|
||||
let name = session.get("name").unwrap_or("You're not logged in.".to_string());
|
||||
|
||||
let template = IndexTemplate { logged_in, name};
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
async fn login(session: AxumSession<AxumPgPool>) -> impl IntoResponse {
|
||||
let logged_in = session.get("logged_in").await.unwrap_or(false);
|
||||
let name = session.get("name").await.unwrap_or("".to_string());
|
||||
async fn login(session: Session<SessionAnyPool>) -> impl IntoResponse {
|
||||
let logged_in = session.get("logged_in").unwrap_or(false);
|
||||
let name = session.get("name").unwrap_or("".to_string());
|
||||
|
||||
session.set_store(true).await;
|
||||
session.set_store(true);
|
||||
|
||||
session.set("logged_in", logged_in);
|
||||
session.set("name", &name);
|
||||
|
||||
let template = LoginTemplate { logged_in, name };
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
//No need to set the sessions accepted or not with gdpr mode disabled
|
||||
async fn greet(session: AxumSession<AxumPgPool>) -> String {
|
||||
let mut count: usize = session.get("count").await.unwrap_or(0);
|
||||
|
||||
// Allow the Session data to be keep in memory and the database for the lifetime.
|
||||
session.set_store(true).await;
|
||||
count += 1;
|
||||
session.set("count", count).await;
|
||||
|
||||
count.to_string()
|
||||
}
|
||||
Loading…
Reference in New Issue