Remove unused imports
This commit is contained in:
parent
0cd7f101ca
commit
a4c250fdd9
79
src/main.rs
79
src/main.rs
|
|
@ -3,8 +3,8 @@ use async_session::{MemoryStore, Session, SessionStore as _};
|
||||||
use axum::{
|
use axum::{
|
||||||
async_trait,
|
async_trait,
|
||||||
extract::{
|
extract::{
|
||||||
self, rejection::TypedHeaderRejectionReason, Extension, FromRequest, Path, Query,
|
self, rejection::TypedHeaderRejectionReason, Extension, FromRequest, Query, RequestParts,
|
||||||
RequestParts, TypedHeader,
|
TypedHeader,
|
||||||
},
|
},
|
||||||
headers::Cookie,
|
headers::Cookie,
|
||||||
http::{
|
http::{
|
||||||
|
|
@ -14,8 +14,8 @@ use axum::{
|
||||||
StatusCode,
|
StatusCode,
|
||||||
},
|
},
|
||||||
response::{Html, IntoResponse, Redirect, Response},
|
response::{Html, IntoResponse, Redirect, Response},
|
||||||
routing::{get, post},
|
routing::get,
|
||||||
Json, Router,
|
Router,
|
||||||
};
|
};
|
||||||
use http::header;
|
use http::header;
|
||||||
use oauth2::{
|
use oauth2::{
|
||||||
|
|
@ -24,11 +24,20 @@ use oauth2::{
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{env, net::SocketAddr};
|
use std::{env, net::SocketAddr};
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
const COOKIE_NAME: &str = "SESSION";
|
const COOKIE_NAME: &str = "SESSION";
|
||||||
|
|
||||||
|
// The user data we'll get back from Discord.
|
||||||
|
// https://discord.com/developers/docs/resources/user#user-object-user-structure
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct User {
|
||||||
|
id: String,
|
||||||
|
avatar: Option<String>,
|
||||||
|
username: String,
|
||||||
|
discriminator: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Set the RUST_LOG, if it hasn't been explicitly defined
|
// Set the RUST_LOG, if it hasn't been explicitly defined
|
||||||
|
|
@ -45,8 +54,7 @@ async fn main() {
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// `GET /` goes to `root`
|
// `GET /` goes to `root`
|
||||||
.route("/greet/:name", get(greet))
|
.route("/", get(index))
|
||||||
.route("/", get(handler))
|
|
||||||
.layer(Extension(store))
|
.layer(Extension(store))
|
||||||
.layer(Extension(oauth_client));
|
.layer(Extension(oauth_client));
|
||||||
|
|
||||||
|
|
@ -60,25 +68,14 @@ async fn main() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handler(user_id: UserIdFromSession) -> impl IntoResponse {
|
// Session is optional
|
||||||
let (headers, user_id, create_cookie) = match user_id {
|
async fn index(user: Option<User>) -> impl IntoResponse {
|
||||||
UserIdFromSession::FoundUserId(user_id) => (HeaderMap::new(), user_id, false),
|
let name = match user {
|
||||||
UserIdFromSession::CreatedFreshUserId(new_user) => {
|
Some(u) => u.username,
|
||||||
let mut headers = HeaderMap::new();
|
None => "You're not logged in.\nVisit `/auth/discord` to do so.".to_string(),
|
||||||
headers.insert(http::header::SET_COOKIE, new_user.cookie);
|
|
||||||
(headers, new_user.user_id, true)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
let template = IndexTemplate { name };
|
||||||
tracing::debug!("handler: user_id={:?} send_headers={:?}", user_id, headers);
|
HtmlTemplate(template)
|
||||||
|
|
||||||
(
|
|
||||||
headers,
|
|
||||||
format!(
|
|
||||||
"user_id={:?} session_cookie_name={} create_new_session_cookie={}",
|
|
||||||
user_id, COOKIE_NAME, create_cookie
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FreshUserId {
|
struct FreshUserId {
|
||||||
|
|
@ -167,14 +164,9 @@ impl UserId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn greet(extract::Path(name): extract::Path<String>) -> impl IntoResponse {
|
|
||||||
let template = HelloTemplate { name };
|
|
||||||
HtmlTemplate(template)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct HelloTemplate {
|
struct IndexTemplate {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,8 +196,8 @@ fn oauth_client() -> BasicClient {
|
||||||
// "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code";
|
// "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code";
|
||||||
// "TOKEN_URL" "https://discord.com/api/oauth2/token";
|
// "TOKEN_URL" "https://discord.com/api/oauth2/token";
|
||||||
|
|
||||||
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID!");
|
let client_id = env::var("GOOGLE_CLIENT_ID").expect("Missing CLIENT_ID!");
|
||||||
let client_secret = env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET!");
|
let client_secret = env::var("GOOGLE_CLIENT_SECRET").expect("Missing CLIENT_SECRET!");
|
||||||
let redirect_url = env::var("REDIRECT_URL")
|
let redirect_url = env::var("REDIRECT_URL")
|
||||||
.unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string());
|
.unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string());
|
||||||
|
|
||||||
|
|
@ -225,27 +217,6 @@ fn oauth_client() -> BasicClient {
|
||||||
.set_redirect_uri(RedirectUrl::new(redirect_url).unwrap())
|
.set_redirect_uri(RedirectUrl::new(redirect_url).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
// The user data we'll get back from Discord.
|
|
||||||
// https://discord.com/developers/docs/resources/user#user-object-user-structure
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
struct User {
|
|
||||||
id: String,
|
|
||||||
avatar: Option<String>,
|
|
||||||
username: String,
|
|
||||||
discriminator: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Session is optional
|
|
||||||
async fn index(user: Option<User>) -> impl IntoResponse {
|
|
||||||
match user {
|
|
||||||
Some(u) => format!(
|
|
||||||
"Hey {}! You're logged in!\nYou may now access `/protected`.\nLog out with `/logout`.",
|
|
||||||
u.username
|
|
||||||
),
|
|
||||||
None => "You're not logged in.\nVisit `/auth/discord` to do so.".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn discord_auth(Extension(client): Extension<BasicClient>) -> impl IntoResponse {
|
async fn discord_auth(Extension(client): Extension<BasicClient>) -> impl IntoResponse {
|
||||||
let (auth_url, _csrf_token) = client
|
let (auth_url, _csrf_token) = client
|
||||||
.authorize_url(CsrfToken::new_random)
|
.authorize_url(CsrfToken::new_random)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue