token-from-header
This commit is contained in:
parent
ece504bbd1
commit
e32e4aade5
17
src/main.rs
17
src/main.rs
|
@ -1,4 +1,4 @@
|
||||||
use actix_web::{web, App, HttpResponse, HttpServer, web::Bytes};
|
use actix_web::{HttpRequest, web, App, HttpResponse, HttpServer, web::Bytes};
|
||||||
use actix_web::middleware::Logger;
|
use actix_web::middleware::Logger;
|
||||||
use redis::{Client, AsyncCommands};
|
use redis::{Client, AsyncCommands};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -18,9 +18,13 @@ struct AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect_handler(
|
async fn connect_handler(
|
||||||
token: web::Path<String>,
|
req: HttpRequest,
|
||||||
state: web::Data<AppState>,
|
state: web::Data<AppState>,
|
||||||
) -> Result<HttpResponse, actix_web::Error> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
|
let token = match req.headers().get("Authorization") {
|
||||||
|
Some(val) => val.to_str().unwrap_or(""),
|
||||||
|
None => return Err(ErrorUnauthorized("Unauthorized")),
|
||||||
|
};
|
||||||
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
|
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
|
||||||
eprintln!("TOKEN check failed: {}", e);
|
eprintln!("TOKEN check failed: {}", e);
|
||||||
ErrorUnauthorized("Unauthorized")
|
ErrorUnauthorized("Unauthorized")
|
||||||
|
@ -86,9 +90,13 @@ async fn connect_handler(
|
||||||
|
|
||||||
|
|
||||||
async fn disconnect_handler(
|
async fn disconnect_handler(
|
||||||
token: web::Path<String>,
|
req: HttpRequest,
|
||||||
state: web::Data<AppState>,
|
state: web::Data<AppState>,
|
||||||
) -> Result<HttpResponse, actix_web::Error> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
|
let token = match req.headers().get("Authorization") {
|
||||||
|
Some(val) => val.to_str().unwrap_or(""),
|
||||||
|
None => return Err(ErrorUnauthorized("Unauthorized")),
|
||||||
|
};
|
||||||
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
|
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
|
||||||
eprintln!("TOKEN check failed: {}", e);
|
eprintln!("TOKEN check failed: {}", e);
|
||||||
ErrorUnauthorized("Unauthorized")
|
ErrorUnauthorized("Unauthorized")
|
||||||
|
@ -116,9 +124,8 @@ async fn main() -> std::io::Result<()> {
|
||||||
tasks: tasks.clone(),
|
tasks: tasks.clone(),
|
||||||
redis: client.clone(),
|
redis: client.clone(),
|
||||||
};
|
};
|
||||||
println!("Redis client initialized");
|
println!("Starting...");
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
println!("Webserver initialized");
|
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.app_data(web::Data::new(state.clone()))
|
.app_data(web::Data::new(state.clone()))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user