mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 14:58:38 +08:00
JWT Authentication and token generation
This commit is contained in:
@@ -1,23 +1,41 @@
|
||||
use crate::utils::parceyaml::Configuration;
|
||||
use axum::body::Body;
|
||||
use axum::extract::State;
|
||||
use axum::http::{Response, StatusCode};
|
||||
use axum::response::IntoResponse;
|
||||
use axum::routing::{delete, get, head, post, put};
|
||||
use axum::Router;
|
||||
use axum::{Json, Router};
|
||||
use futures::channel::mpsc::Sender;
|
||||
use futures::SinkExt;
|
||||
use log::info;
|
||||
use jsonwebtoken::{encode, EncodingKey, Header};
|
||||
use log::{error, info, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct InputKey {
|
||||
masterkey: String,
|
||||
owner: String,
|
||||
valid: u64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct OutToken {
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[allow(unused_mut)]
|
||||
pub async fn run_server(bindaddress: String, mut toreturn: Sender<Configuration>) {
|
||||
pub async fn run_server(bindaddress: String, masterkey: String, mut toreturn: Sender<Configuration>) {
|
||||
let mut tr = toreturn.clone();
|
||||
let app = Router::new()
|
||||
.route("/{*wildcard}", get(getconfig))
|
||||
.route("/{*wildcard}", post(getconfig))
|
||||
.route("/{*wildcard}", put(getconfig))
|
||||
.route("/{*wildcard}", head(getconfig))
|
||||
.route("/{*wildcard}", delete(getconfig))
|
||||
.route("/{*wildcard}", get(senderror))
|
||||
.route("/{*wildcard}", post(senderror))
|
||||
.route("/{*wildcard}", put(senderror))
|
||||
.route("/{*wildcard}", head(senderror))
|
||||
.route("/{*wildcard}", delete(senderror))
|
||||
.route("/jwt", post(jwt_gen))
|
||||
.with_state(masterkey.clone())
|
||||
.route(
|
||||
"/conf",
|
||||
post(|up: String| async move {
|
||||
@@ -42,26 +60,32 @@ pub async fn run_server(bindaddress: String, mut toreturn: Sender<Configuration>
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn getconfig() -> impl IntoResponse {
|
||||
"Hello from Axum API inside Pingora!\n".to_string();
|
||||
async fn senderror() -> impl IntoResponse {
|
||||
Response::builder().status(StatusCode::BAD_GATEWAY).body(Body::from("No live upstream found!\n")).unwrap()
|
||||
}
|
||||
// curl -XPOST -H 'Content-Type: application/json' --data-binary @./push.json 127.0.0.1:3000/json
|
||||
// curl -XPOST --data-binary @./etc/upstreams.txt 127.0.0.1:3000/conf
|
||||
|
||||
/*
|
||||
async fn config(Json(payload): Json<HashMap<String, UpstreamData>>) -> impl IntoResponse {
|
||||
let upstreams = DashMap::new();
|
||||
for (key, value) in payload {
|
||||
upstreams.insert(key, (value.servers, AtomicUsize::new(value.counter)));
|
||||
async fn jwt_gen(State(masterkey): State<String>, Json(payload): Json<InputKey>) -> (StatusCode, Json<OutToken>) {
|
||||
if payload.masterkey == masterkey {
|
||||
let now = SystemTime::now() + Duration::from_secs(payload.valid * 60);
|
||||
let a = now.duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||
let claim = crate::utils::jwt::Claims { user: payload.owner, exp: a };
|
||||
match encode(&Header::default(), &claim, &EncodingKey::from_secret(payload.masterkey.as_ref())) {
|
||||
Ok(t) => {
|
||||
let tok = OutToken { token: t };
|
||||
info!("Generating token: {:?}", tok);
|
||||
(StatusCode::CREATED, Json(tok))
|
||||
}
|
||||
Err(e) => {
|
||||
let tok = OutToken { token: "ERROR".to_string() };
|
||||
error!("Failed to generate token: {:?}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(tok))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let tok = OutToken {
|
||||
token: "Unauthorised".to_string(),
|
||||
};
|
||||
warn!("Unauthorised JWT generate request: {:?}", tok);
|
||||
(StatusCode::FORBIDDEN, Json(tok))
|
||||
}
|
||||
println!("{:?}", upstreams);
|
||||
Response::builder().status(StatusCode::CREATED).body(Body::from("Config updated!\n")).unwrap()
|
||||
}
|
||||
async fn parse_upstreams(up: String) -> impl IntoResponse {
|
||||
println!("Parsing: {}", up);
|
||||
let serverlist = read_upstreams_from_file(up.as_str());
|
||||
println!("{:?}", serverlist);
|
||||
Response::builder().status(StatusCode::CREATED).body(Body::from("Config updated!\n")).unwrap()
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user