can delete

This commit is contained in:
nora 2021-07-16 17:07:28 +02:00
parent 357b6ce794
commit dfc6771135
2 changed files with 5 additions and 25 deletions

View file

@ -3,9 +3,8 @@ use crate::models::User;
use actix_web::dev::{Payload, ServiceRequest}; use actix_web::dev::{Payload, ServiceRequest};
use actix_web::error::ErrorUnauthorized; use actix_web::error::ErrorUnauthorized;
use actix_web::http::header::Header; use actix_web::http::header::Header;
use actix_web::{FromRequest, HttpMessage, HttpRequest, HttpResponse}; use actix_web::{FromRequest, HttpMessage, HttpRequest};
use actix_web_httpauth::extractors::bearer::BearerAuth; use actix_web_httpauth::extractors::bearer::BearerAuth;
use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::headers::authorization; use actix_web_httpauth::headers::authorization;
use actix_web_httpauth::headers::authorization::Bearer; use actix_web_httpauth::headers::authorization::Bearer;
use chrono::Utc; use chrono::Utc;
@ -41,19 +40,6 @@ impl FromRequest for Claims {
} }
} }
pub async fn validator(
req: ServiceRequest,
credentials: BearerAuth,
) -> Result<ServiceRequest, actix_web::Error> {
match validate_token(credentials.token()) {
Ok(claims) => {
req.extensions_mut().insert(claims);
Ok(req)
}
Err(err) => Err(err.into()),
}
}
fn validate_token(token: &str) -> Result<Claims, actix_web::Error> { fn validate_token(token: &str) -> Result<Claims, actix_web::Error> {
let secret = std::env::var("JWT_SECRET").expect("JWT_SECRET env var"); let secret = std::env::var("JWT_SECRET").expect("JWT_SECRET env var");

View file

@ -1,7 +1,6 @@
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
use crate::auth::validator;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use actix_web_httpauth::middleware::HttpAuthentication; use actix_web_httpauth::middleware::HttpAuthentication;
use diesel::prelude::*; use diesel::prelude::*;
@ -28,19 +27,14 @@ async fn main() -> std::io::Result<()> {
.expect("Failed to create pool."); .expect("Failed to create pool.");
HttpServer::new(move || { HttpServer::new(move || {
let auth_middleware = HttpAuthentication::bearer(validator);
App::new() App::new()
.data(pool.clone()) .data(pool.clone())
.route("/users", web::post().to(handlers::add_user))
.route("/test", web::get().to(handlers::test_auth)) .route("/test", web::get().to(handlers::test_auth))
.route("/admin", web::post().to(handlers::admin_login)) .route("/admin", web::post().to(handlers::admin_login))
.service( .route("/users", web::get().to(handlers::get_users))
web::scope("/users") .route("/users", web::post().to(handlers::add_user))
.wrap(auth_middleware) .route("/users/{id}", web::get().to(handlers::get_user_by_id))
.route("", web::get().to(handlers::get_users)) .route("/users/{id}", web::delete().to(handlers::delete_user))
.route("/{id}", web::get().to(handlers::get_user_by_id))
.route("/{id}", web::delete().to(handlers::delete_user)),
)
}) })
.bind("127.0.0.1:8080")? .bind("127.0.0.1:8080")?
.run() .run()