Rohan Sircar
3 years ago
11 changed files with 1212 additions and 597 deletions
-
5.dockerignore
-
1491Cargo.lock
-
46Cargo.toml
-
46Dockerfile
-
11src/actions/users.rs
-
27src/main.rs
-
2src/routes/auth.rs
-
38src/routes/users.rs
-
2src/services.rs
-
84src/services/user_service.rs
-
43src/utils/auth.rs
@ -0,0 +1,5 @@ |
|||||
|
target |
||||
|
.git |
||||
|
.idea |
||||
|
.vscode |
||||
|
# test.db |
1491
Cargo.lock
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,46 @@ |
|||||
|
FROM rust:1.45 as builder |
||||
|
|
||||
|
# ENV CARGO_HOME=/actix-demo/.cargo |
||||
|
RUN USER=root cargo new --bin actix-demo |
||||
|
WORKDIR /actix-demo |
||||
|
# COPY ./.cargo ./.cargo |
||||
|
COPY ./Cargo.toml ./Cargo.toml |
||||
|
COPY ./Cargo.lock ./Cargo.lock |
||||
|
RUN cargo build --release |
||||
|
RUN rm src/*.rs |
||||
|
|
||||
|
COPY ./src ./src |
||||
|
|
||||
|
#RUN rm ./target/release/deps/actix-demo* |
||||
|
RUN cargo build --release |
||||
|
|
||||
|
|
||||
|
|
||||
|
FROM debian:buster-slim |
||||
|
ARG APP=/usr/src/app |
||||
|
|
||||
|
RUN apt-get update \ |
||||
|
&& apt-get install -y ca-certificates tzdata \ |
||||
|
&& rm -rf /var/lib/apt/lists/* |
||||
|
|
||||
|
EXPOSE 8000 |
||||
|
|
||||
|
ENV TZ=Etc/UTC \ |
||||
|
APP_USER=appuser |
||||
|
|
||||
|
RUN groupadd $APP_USER \ |
||||
|
&& useradd -g $APP_USER $APP_USER \ |
||||
|
&& mkdir -p ${APP} |
||||
|
|
||||
|
COPY ./.env ${APP}/.env |
||||
|
COPY ./migrations ${APP}/migrations |
||||
|
COPY ./static ${APP}/static |
||||
|
COPY ./test.db ${APP}/test.db |
||||
|
COPY --from=builder /actix-demo/target/release/actix-demo ${APP}/actix-demo |
||||
|
|
||||
|
RUN chown -R $APP_USER:$APP_USER ${APP} |
||||
|
|
||||
|
USER $APP_USER |
||||
|
WORKDIR ${APP} |
||||
|
|
||||
|
CMD ["./actix-demo"] |
@ -0,0 +1,2 @@ |
|||||
|
pub mod user_service;
|
||||
|
pub use self::user_service::*;
|
@ -0,0 +1,84 @@ |
|||||
|
use std::rc::Rc;
|
||||
|
|
||||
|
use diesel::SqliteConnection;
|
||||
|
|
||||
|
use crate::{actions, errors, models, types::DbPool};
|
||||
|
|
||||
|
pub trait UserService {
|
||||
|
fn find_user_by_uid(
|
||||
|
&self,
|
||||
|
uid: i32,
|
||||
|
) -> Result<Option<models::UserDTO>, errors::DomainError>;
|
||||
|
fn _find_user_by_name(
|
||||
|
&self,
|
||||
|
user_name: String,
|
||||
|
) -> Result<Option<models::UserDTO>, errors::DomainError>;
|
||||
|
|
||||
|
fn get_all(
|
||||
|
&self,
|
||||
|
) -> Result<Option<Vec<models::UserDTO>>, errors::DomainError>;
|
||||
|
|
||||
|
fn insert_new_user(
|
||||
|
&self,
|
||||
|
nu: models::NewUser,
|
||||
|
) -> Result<models::UserDTO, errors::DomainError>;
|
||||
|
|
||||
|
// fn woot(&self) -> i32;
|
||||
|
|
||||
|
fn verify_password<'a>(
|
||||
|
&self,
|
||||
|
user_name: &'a String,
|
||||
|
given_password: &'a String,
|
||||
|
) -> Result<bool, errors::DomainError>;
|
||||
|
}
|
||||
|
|
||||
|
#[derive(Clone)]
|
||||
|
pub struct UserServiceImpl {
|
||||
|
pub pool: DbPool,
|
||||
|
}
|
||||
|
|
||||
|
impl UserService for UserServiceImpl {
|
||||
|
fn find_user_by_uid(
|
||||
|
&self,
|
||||
|
uid: i32,
|
||||
|
) -> Result<Option<models::UserDTO>, errors::DomainError> {
|
||||
|
let conn = self.pool.get()?;
|
||||
|
actions::find_user_by_uid(uid, &conn)
|
||||
|
}
|
||||
|
|
||||
|
fn _find_user_by_name(
|
||||
|
&self,
|
||||
|
user_name: String,
|
||||
|
) -> Result<Option<models::UserDTO>, errors::DomainError> {
|
||||
|
let conn = self.pool.get()?;
|
||||
|
actions::_find_user_by_name(user_name, &conn)
|
||||
|
}
|
||||
|
|
||||
|
fn get_all(
|
||||
|
&self,
|
||||
|
) -> Result<Option<Vec<models::UserDTO>>, errors::DomainError> {
|
||||
|
let conn = self.pool.get()?;
|
||||
|
actions::get_all(&conn)
|
||||
|
}
|
||||
|
|
||||
|
fn insert_new_user(
|
||||
|
&self,
|
||||
|
nu: models::NewUser,
|
||||
|
) -> Result<models::UserDTO, errors::DomainError> {
|
||||
|
let conn = self.pool.get()?;
|
||||
|
actions::insert_new_user(nu, &conn)
|
||||
|
}
|
||||
|
|
||||
|
fn verify_password<'b>(
|
||||
|
&self,
|
||||
|
user_name: &'b String,
|
||||
|
given_password: &'b String,
|
||||
|
) -> Result<bool, errors::DomainError> {
|
||||
|
let conn = self.pool.get()?;
|
||||
|
actions::verify_password(user_name, given_password, &conn)
|
||||
|
}
|
||||
|
|
||||
|
// async fn woot(&self) -> i32 {
|
||||
|
// 1
|
||||
|
// }
|
||||
|
}
|
@ -1,28 +1,31 @@ |
|||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||
|
|
||||
use crate::AppConfig;
|
|
||||
use crate::routes::validate_basic_auth;
|
use crate::routes::validate_basic_auth;
|
||||
|
use crate::AppConfig;
|
||||
|
|
||||
use actix_web::{dev::ServiceRequest, web, Error};
|
use actix_web::{dev::ServiceRequest, web, Error};
|
||||
|
|
||||
|
// pub async fn validator(
|
||||
|
// req: ServiceRequest,
|
||||
|
// credentials: BasicAuth,
|
||||
|
// ) -> Result<ServiceRequest, Error> {
|
||||
|
// println!("{}", credentials.user_id());
|
||||
|
// println!("{:?}", credentials.password());
|
||||
|
// // verify credentials from db
|
||||
|
// let config = req.app_data::<AppConfig>().expect("Error getting config");
|
||||
|
|
||||
pub async fn validator(
|
|
||||
req: ServiceRequest,
|
|
||||
credentials: BasicAuth,
|
|
||||
) -> Result<ServiceRequest, Error> {
|
|
||||
println!("{}", credentials.user_id());
|
|
||||
println!("{:?}", credentials.password());
|
|
||||
// verify credentials from db
|
|
||||
let config = req.app_data::<AppConfig>().expect("Error getting config");
|
|
||||
|
// let valid =
|
||||
|
// web::block(move || validate_basic_auth(credentials, config)).await?;
|
||||
|
// if valid {
|
||||
|
// debug!("Success");
|
||||
|
// Ok(req)
|
||||
|
// } else {
|
||||
|
// println!("blah");
|
||||
|
// let err: Error = crate::errors::DomainError::new_password_error(
|
||||
|
// "Wrong password or account does not exist".to_string(),
|
||||
|
// )
|
||||
|
// .into();
|
||||
|
|
||||
let valid =
|
|
||||
web::block(move || validate_basic_auth(credentials, &config)).await?;
|
|
||||
if valid {
|
|
||||
debug!("Success");
|
|
||||
Ok(req)
|
|
||||
} else {
|
|
||||
Err(crate::errors::DomainError::new_password_error(
|
|
||||
"Wrong password or account does not exist".to_string(),
|
|
||||
).into())
|
|
||||
}
|
|
||||
}
|
|
||||
|
// Err(err)
|
||||
|
// }
|
||||
|
// }
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue