add start

This commit is contained in:
Kasper Juul Hermansen 2022-10-02 14:15:45 +02:00
parent 351b92e54b
commit 4c66fb1e7a
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
13 changed files with 293 additions and 14 deletions

84
Cargo.lock generated
View File

@ -68,6 +68,17 @@ version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c794e162a5eff65c72ef524dfe393eb923c354e350bb78b9c7383df13f3bc142" checksum = "c794e162a5eff65c72ef524dfe393eb923c354e350bb78b9c7383df13f3bc142"
[[package]]
name = "argon2"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db4ce4441f99dbd377ca8a8f57b698c44d0d6e712d8329b5040da5a64aa1ce73"
dependencies = [
"base64ct",
"blake2",
"password-hash",
]
[[package]] [[package]]
name = "ascii_utils" name = "ascii_utils"
version = "0.9.3" version = "0.9.3"
@ -256,12 +267,27 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "base64ct"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea2b2456fd614d856680dcd9fcc660a51a820fa09daef2e49772b56a193c8474"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "blake2"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9cf849ee05b2ee5fba5e36f97ff8ec2533916700fc0758d40d92136a42f3388"
dependencies = [
"digest",
]
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.2" version = "0.10.2"
@ -330,9 +356,12 @@ name = "como_bin"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"argon2",
"async-graphql", "async-graphql",
"axum", "axum",
"cookie",
"dotenv", "dotenv",
"rand_core",
"sqlx", "sqlx",
"tokio", "tokio",
"tower-http", "tower-http",
@ -341,6 +370,16 @@ dependencies = [
"uuid", "uuid",
] ]
[[package]]
name = "cookie"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "344adc371239ef32293cb1c4fe519592fcf21206c79c02854320afcdf3ab4917"
dependencies = [
"time",
"version_check",
]
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.2" version = "0.2.2"
@ -490,6 +529,9 @@ name = "either"
version = "1.7.0" version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
@ -982,6 +1024,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.13.0" version = "1.13.0"
@ -1036,6 +1087,17 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "password-hash"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700"
dependencies = [
"base64ct",
"rand_core",
"subtle",
]
[[package]] [[package]]
name = "paste" name = "paste"
version = "1.0.8" version = "1.0.8"
@ -1458,6 +1520,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio-stream", "tokio-stream",
"url", "url",
"uuid",
"webpki-roots", "webpki-roots",
"whoami", "whoami",
] ]
@ -1471,9 +1534,12 @@ dependencies = [
"dotenvy", "dotenvy",
"either", "either",
"heck", "heck",
"hex",
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"serde",
"serde_json",
"sha2", "sha2",
"sqlx-core", "sqlx-core",
"sqlx-rt", "sqlx-rt",
@ -1580,6 +1646,24 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "time"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3f9a28b618c3a6b9251b6908e9c99e04b9e5c02e6581ccbb67d59c34ef7f9b"
dependencies = [
"itoa",
"libc",
"num_threads",
"time-macros",
]
[[package]]
name = "time-macros"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"

View File

@ -10,9 +10,18 @@ async-graphql = "4.0.6"
axum = "0.5.13" axum = "0.5.13"
tokio = { version = "1.20.1", features = ["full"] } tokio = { version = "1.20.1", features = ["full"] }
uuid = { version = "1.1.2", features = ["v4", "fast-rng"] } uuid = { version = "1.1.2", features = ["v4", "fast-rng"] }
sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "postgres", "migrate"] } sqlx = { version = "0.6", features = [
"runtime-tokio-rustls",
"postgres",
"migrate",
"uuid",
"offline",
] }
anyhow = "1.0.60" anyhow = "1.0.60"
dotenv = "0.15.0" dotenv = "0.15.0"
tracing = "0.1.36" tracing = "0.1.36"
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] } tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
tower-http = { version = "0.3.4", features = ["full"] } tower-http = { version = "0.3.4", features = ["full"] }
argon2 = "0.4"
rand_core = { version = "0.6", features = ["std"] }
cookie = "0.16"

View File

@ -1,4 +1,8 @@
-- Add migration script here -- Add migration script here
CREATE TABLE IF NOT EXISTS events ( CREATE TABLE IF NOT EXISTS users (
id BIGSERIAL PRIMARY KEY id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username varchar not null,
password_hash varchar not null
); );
CREATE unique index users_username_idx on users(username)

View File

@ -1 +0,0 @@
-- Add migration script here

56
como_bin/sqlx-data.json Normal file
View File

@ -0,0 +1,56 @@
{
"db": "PostgreSQL",
"3b4484c5ccfd4dcb887c4e978fe6e45d4c9ecc2a73909be207dced79ddf17d87": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Uuid"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Varchar",
"Varchar"
]
}
},
"query": "\n INSERT INTO users (username, password_hash) \n VALUES ( $1, $2 ) \n RETURNING id\n "
},
"d3f222cf6c3d9816705426fdbed3b13cb575bb432eb1f33676c0b414e67aecaf": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Uuid"
},
{
"name": "username",
"ordinal": 1,
"type_info": "Varchar"
},
{
"name": "password_hash",
"ordinal": 2,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "\n SELECT * from users\n where username=$1\n "
}
}

2
como_bin/src/gqlx/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod users;

View File

View File

@ -1,13 +1,49 @@
use async_graphql::{Context, EmptyMutation, EmptySubscription, Object, Schema, SimpleObject}; use async_graphql::{Context, EmptySubscription, Object, Schema, SimpleObject};
use uuid::Uuid; use uuid::Uuid;
pub type CibusSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>; use crate::services::users_service::UserService;
pub type CibusSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
pub struct MutationRoot;
#[Object]
impl MutationRoot {
async fn login(
&self,
ctx: &Context<'_>,
username: String,
password: String,
) -> anyhow::Result<bool> {
let user_service = ctx.data_unchecked::<UserService>();
let valid = user_service.validate_user(username, password).await?;
Ok(match valid {
Some(..) => true,
None => false,
})
}
async fn register(
&self,
ctx: &Context<'_>,
username: String,
password: String,
) -> anyhow::Result<String> {
let user_service = ctx.data_unchecked::<UserService>();
let user_id = user_service.add_user(username, password).await?;
Ok(user_id.into())
}
}
pub struct QueryRoot; pub struct QueryRoot;
#[Object] #[Object]
impl QueryRoot { impl QueryRoot {
async fn get_upcoming(&self, ctx: &Context<'_>) -> Vec<Event> { async fn get_upcoming(&self, _ctx: &Context<'_>) -> Vec<Event> {
vec![Event::new( vec![Event::new(
None, None,
"Some-name".into(), "Some-name".into(),

View File

@ -1,6 +1,8 @@
use std::env::{self, current_dir}; use std::env::{self, current_dir};
mod gqlx;
mod graphql; mod graphql;
mod services;
use axum::{ use axum::{
extract::Extension, extract::Extension,
@ -12,14 +14,15 @@ use axum::{
use async_graphql::{ use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig}, http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Request, Response, Schema, EmptySubscription, Request, Response, Schema,
}; };
use graphql::CibusSchema; use graphql::CibusSchema;
use services::users_service;
use sqlx::PgPool; use sqlx::PgPool;
use tower_http::{cors::CorsLayer, trace::TraceLayer}; use tower_http::{cors::CorsLayer, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use crate::graphql::QueryRoot; use crate::graphql::{MutationRoot, QueryRoot};
async fn graphql_handler(schema: Extension<CibusSchema>, req: Json<Request>) -> Json<Response> { async fn graphql_handler(schema: Extension<CibusSchema>, req: Json<Request>) -> Json<Response> {
schema.execute(req.0).await.into() schema.execute(req.0).await.into()
@ -39,7 +42,8 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::registry() tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new( .with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| { std::env::var("RUST_LOG").unwrap_or_else(|_| {
"como_bin=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info".into() "como_bin=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info,sqlx=info,async_graphql=debug"
.into()
}), }),
)) ))
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
@ -58,7 +62,9 @@ async fn main() -> anyhow::Result<()> {
// Schema // Schema
println!("Building schema"); println!("Building schema");
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish(); let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
.data(users_service::UserService::new(pool))
.finish();
// CORS // CORS
let cors = vec!["http://localhost:3000".parse().unwrap()]; let cors = vec!["http://localhost:3000".parse().unwrap()];

View File

@ -0,0 +1 @@
pub struct CookieService {}

View File

@ -0,0 +1,2 @@
pub mod cookie_service;
pub mod users_service;

View File

@ -0,0 +1,80 @@
use std::sync::Arc;
use anyhow::anyhow;
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use rand_core::OsRng;
use sqlx::{Pool, Postgres};
pub struct UserService {
pgx: Pool<Postgres>,
}
impl UserService {
pub fn new(pgx: Pool<Postgres>) -> Self {
Self { pgx }
}
pub async fn add_user(&self, username: String, password: String) -> anyhow::Result<String> {
let hashed_password = self.hash_password(password)?;
let rec = sqlx::query!(
r#"
INSERT INTO users (username, password_hash)
VALUES ( $1, $2 )
RETURNING id
"#,
username,
hashed_password
)
.fetch_one(&self.pgx)
.await?;
Ok(rec.id.to_string())
}
pub async fn validate_user(
&self,
username: String,
password: String,
) -> anyhow::Result<Option<()>> {
let rec = sqlx::query!(
r#"
SELECT * from users
where username=$1
"#,
username,
)
.fetch_optional(&self.pgx)
.await?;
match rec {
Some(user) => match self.validate_password(password, user.password_hash)? {
true => Ok(Some(())),
false => Ok(None),
},
None => Ok(None),
}
}
fn hash_password(&self, password: String) -> anyhow::Result<String> {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
let password_hash = argon2
.hash_password(password.as_bytes(), &salt)
.map_err(|e| anyhow!(e))?
.to_string();
Ok(password_hash)
}
fn validate_password(&self, password: String, hashed_password: String) -> anyhow::Result<bool> {
let argon2 = Argon2::default();
let parsed_hash = PasswordHash::new(&hashed_password).map_err(|e| anyhow!(e))?;
match argon2.verify_password(password.as_bytes(), &parsed_hash) {
Ok(..) => Ok(true),
Err(..) => Ok(false),
}
}
}

View File

@ -2,6 +2,6 @@
set -e set -e
cuddle_cli render_templates --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml cuddle_cli render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml
docker compose -f $TMP/docker-compose.local_up.yml up -d --remove-orphans docker compose -f $TMP/docker-compose.local_up.yml up -d --remove-orphans