Skip to content

Commit

Permalink
issue-3 bearer_guard
Browse files Browse the repository at this point in the history
  • Loading branch information
smart--petea committed Sep 29, 2023
1 parent d8c32cb commit 882f76a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ thiserror = "1.0"
serde_valid = "0.16.3"
serde_json = { version = "1.0.105", features = [] }
serde_derive = "1.0.188"
actix-web-httpauth = "0.8.1"
actix-cors = "0.6.4"

[dependencies.sqlx]
version = "0.6.3"
Expand Down
49 changes: 27 additions & 22 deletions src/startup.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
use actix_web::dev::Server;
use actix_cors::Cors;
use actix_web::dev::{Server, ServiceRequest};
use actix_web::middleware::Logger;
use actix_web::{
// http::header::HeaderName,
web::{self, Form},
App, HttpServer,
App,
Error,
HttpServer,
};
use actix_web_httpauth::{extractors::bearer::BearerAuth, middleware::HttpAuthentication};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use std::net::TcpListener;
use serde::{Deserialize, Serialize};
// use serde_derive::{Deserialize, Serialize};
// use uuid::Uuid;

#[derive(Serialize, Deserialize, Debug)]
pub struct AppState {
pub user_id: i32 // @todo User must be move later to actix session and obtained from auth
pub user_id: i32, // @todo User must be move later to actix session and obtained from auth
}

async fn bearer_guard(
req: ServiceRequest,
credentials: BearerAuth,
) -> Result<ServiceRequest, (Error, ServiceRequest)> {
eprintln!("{credentials:?}");
//todo check that credentials.token is a real. get in sync with auth server
//todo get user from auth server
//todo save the server in the request state
//todo get the user in the rating route
Ok(req)
}

pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
let db_pool = web::Data::new(db_pool);
let server = HttpServer::new(move || {
App::new()
.wrap(Logger::default())
.wrap(HttpAuthentication::bearer(bearer_guard))
.wrap(Cors::permissive())
.service(
web::resource("/health_check")
.route(web::get()
.to(crate::routes::health_check)),
web::resource("/health_check").route(web::get().to(crate::routes::health_check)),
)
.service(
web::resource("/rating")
Expand All @@ -41,23 +54,15 @@ pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Er
// .route(web::post()
// .to(crate::routes::stack::add)),
// )
.service(web::resource("/stack").route(web::post().to(crate::routes::stack::add::add)))
.service(
web::resource("/stack")
.route(web::post()
.to(crate::routes::stack::add::add)),
)
.service(
web::resource("/stack/deploy")
.route(web::post()
.to(crate::routes::stack::deploy)),
web::resource("/stack/deploy").route(web::post().to(crate::routes::stack::deploy)),
)
.app_data(db_pool.clone())
.app_data(web::Data::new(AppState {
user_id: 1,
}))
.app_data(web::Data::new(AppState { user_id: 1 }))
})
.listen(listener)?
.run();
.listen(listener)?
.run();

Ok(server)
}

0 comments on commit 882f76a

Please sign in to comment.