parent
ab0579b45b
commit
e548b36a58
|
|
@ -4,6 +4,6 @@ async fn google_auth(Extension(client): Extension<BasicClient>) -> impl IntoResp
|
||||||
.add_scope(Scope::new("identify".to_string()))
|
.add_scope(Scope::new("identify".to_string()))
|
||||||
.url();
|
.url();
|
||||||
|
|
||||||
// Redirect to Discord's oauth service
|
// Redirect to Google's oauth service
|
||||||
Redirect::to(auth_url.to_string().parse().unwrap())
|
Redirect::to(auth_url.to_string().parse().unwrap())
|
||||||
}
|
}
|
||||||
51
src/main.rs
51
src/main.rs
|
|
@ -18,15 +18,15 @@ use axum::{
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use http::header;
|
use http::header;
|
||||||
use oauth2::{basic::BasicClient, reqwest::async_http_client, AuthorizationCode, TokenResponse};
|
use oauth2::{
|
||||||
|
basic::BasicClient, reqwest::async_http_client, AuthUrl, AuthorizationCode, ClientId,
|
||||||
|
ClientSecret, RedirectUrl, TokenResponse, TokenUrl,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::net::SocketAddr;
|
use std::{env, net::SocketAddr};
|
||||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
mod oauth;
|
|
||||||
use oauth::oauth_client;
|
|
||||||
|
|
||||||
const COOKIE_NAME: &str = "SESSION";
|
const COOKIE_NAME: &str = "SESSION";
|
||||||
|
|
||||||
// The user data we'll get back from Discord.
|
// The user data we'll get back from Discord.
|
||||||
|
|
@ -67,6 +67,7 @@ async fn main() {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
|
.route("/login", get(login))
|
||||||
.layer(Extension(store))
|
.layer(Extension(store))
|
||||||
.layer(Extension(oauth_client));
|
.layer(Extension(oauth_client));
|
||||||
|
|
||||||
|
|
@ -90,6 +91,46 @@ async fn index(user: Option<User>) -> impl IntoResponse {
|
||||||
HtmlTemplate(template)
|
HtmlTemplate(template)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn login() -> impl IntoResponse {
|
||||||
|
let name = "".to_string();
|
||||||
|
let template = LoginTemplate { name };
|
||||||
|
HtmlTemplate(template)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn oauth_client() -> BasicClient {
|
||||||
|
// Environment variables (* = required):
|
||||||
|
// *"CLIENT_ID" "REPLACE_ME";
|
||||||
|
// *"CLIENT_SECRET" "REPLACE_ME";
|
||||||
|
// "REDIRECT_URL" "http://127.0.0.1:3000/auth/authorized";
|
||||||
|
// "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code";
|
||||||
|
// "TOKEN_URL" "https://discord.com/api/oauth2/token";
|
||||||
|
|
||||||
|
let client_id = env::var("GOOGLE_CLIENT_ID").expect("Missing CLIENT_ID!");
|
||||||
|
let client_secret = env::var("GOOGLE_CLIENT_SECRET").expect("Missing CLIENT_SECRET!");
|
||||||
|
let redirect_url = env::var("REDIRECT_URL")
|
||||||
|
.unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string());
|
||||||
|
|
||||||
|
let auth_url = env::var("AUTH_URL").unwrap_or_else(|_| {
|
||||||
|
"https://discord.com/api/oauth2/authorize?response_type=code".to_string()
|
||||||
|
});
|
||||||
|
|
||||||
|
let token_url = env::var("TOKEN_URL")
|
||||||
|
.unwrap_or_else(|_| "https://discord.com/api/oauth2/token".to_string());
|
||||||
|
|
||||||
|
BasicClient::new(
|
||||||
|
ClientId::new(client_id),
|
||||||
|
Some(ClientSecret::new(client_secret)),
|
||||||
|
AuthUrl::new(auth_url).unwrap(),
|
||||||
|
Some(TokenUrl::new(token_url).unwrap()),
|
||||||
|
)
|
||||||
|
.set_redirect_uri(RedirectUrl::new(redirect_url).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(path = "login.html")]
|
||||||
|
struct LoginTemplate {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
struct FreshUserId {
|
struct FreshUserId {
|
||||||
pub user_id: UserId,
|
pub user_id: UserId,
|
||||||
pub cookie: HeaderValue,
|
pub cookie: HeaderValue,
|
||||||
|
|
|
||||||
31
src/oauth.rs
31
src/oauth.rs
|
|
@ -1,31 +0,0 @@
|
||||||
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl};
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
pub fn oauth_client() -> BasicClient {
|
|
||||||
// Environment variables (* = required):
|
|
||||||
// *"CLIENT_ID" "REPLACE_ME";
|
|
||||||
// *"CLIENT_SECRET" "REPLACE_ME";
|
|
||||||
// "REDIRECT_URL" "http://127.0.0.1:3000/auth/authorized";
|
|
||||||
// "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code";
|
|
||||||
// "TOKEN_URL" "https://discord.com/api/oauth2/token";
|
|
||||||
|
|
||||||
let client_id = env::var("GOOGLE_CLIENT_ID").expect("Missing CLIENT_ID!");
|
|
||||||
let client_secret = env::var("GOOGLE_CLIENT_SECRET").expect("Missing CLIENT_SECRET!");
|
|
||||||
let redirect_url = env::var("REDIRECT_URL")
|
|
||||||
.unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string());
|
|
||||||
|
|
||||||
let auth_url = env::var("AUTH_URL").unwrap_or_else(|_| {
|
|
||||||
"https://discord.com/api/oauth2/authorize?response_type=code".to_string()
|
|
||||||
});
|
|
||||||
|
|
||||||
let token_url = env::var("TOKEN_URL")
|
|
||||||
.unwrap_or_else(|_| "https://discord.com/api/oauth2/token".to_string());
|
|
||||||
|
|
||||||
BasicClient::new(
|
|
||||||
ClientId::new(client_id),
|
|
||||||
Some(ClientSecret::new(client_secret)),
|
|
||||||
AuthUrl::new(auth_url).unwrap(),
|
|
||||||
Some(TokenUrl::new(token_url).unwrap()),
|
|
||||||
)
|
|
||||||
.set_redirect_uri(RedirectUrl::new(redirect_url).unwrap())
|
|
||||||
}
|
|
||||||
|
|
@ -44,10 +44,8 @@
|
||||||
<!-- ***** Menu Start ***** -->
|
<!-- ***** Menu Start ***** -->
|
||||||
<ul class="nav">
|
<ul class="nav">
|
||||||
<li class="scroll-to-section"><a href="#top" class="active">Home</a></li>
|
<li class="scroll-to-section"><a href="#top" class="active">Home</a></li>
|
||||||
<li class="scroll-to-section"><a href="#about">About</a></li>
|
|
||||||
<li class="scroll-to-section"><a href="#projects">Projects</a></li>
|
|
||||||
<li class="submenu">
|
<li class="submenu">
|
||||||
<a href="javascript:;">Drop Down</a>
|
<a href="javascript:;">Help</a>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="">About Us</a></li>
|
<li><a href="">About Us</a></li>
|
||||||
<li><a href="">Features</a></li>
|
<li><a href="">Features</a></li>
|
||||||
|
|
@ -56,7 +54,7 @@
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li class="scroll-to-section"><a href="#contact-us">Contact Us</a></li>
|
<li class="scroll-to-section"><a href="#contact-us">Contact Us</a></li>
|
||||||
<li class=""><a href="/auth/google">Login</a></li>
|
<li class=""><a href="/login">Login</a></li>
|
||||||
<div class="search-icon">
|
<div class="search-icon">
|
||||||
<a href="#search"><i class="fa fa-search"></i></a>
|
<a href="#search"><i class="fa fa-search"></i></a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
{% block content %}
|
||||||
|
Hello {{ name }}
|
||||||
|
{% endblock content %}
|
||||||
Loading…
Reference in New Issue