Compare commits
No commits in common. "77abf8811a2f988f4f514f98e63bed8332e2b039" and "a370b94d8b38e8e55241095ec9ad5fc0480029f9" have entirely different histories.
77abf8811a
...
a370b94d8b
6 changed files with 7 additions and 82 deletions
3
.env
3
.env
|
@ -1,2 +1 @@
|
||||||
DATABASE_URL=postgres://alisa:alisa@localhost/alisa
|
DATABASE_URL=postgres://alisa:alisa@localhost/alisa
|
||||||
TOKEN_SECRET=secret
|
|
40
Cargo.lock
generated
40
Cargo.lock
generated
|
@ -573,7 +573,6 @@ dependencies = [
|
||||||
"argon2",
|
"argon2",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"entity",
|
"entity",
|
||||||
"jsonwebtoken",
|
|
||||||
"migration",
|
"migration",
|
||||||
"sea-orm",
|
"sea-orm",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -1285,10 +1284,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"js-sys",
|
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi",
|
||||||
"wasm-bindgen",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1549,21 +1546,6 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "jsonwebtoken"
|
|
||||||
version = "9.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b9ae10193d25051e74945f1ea2d0b42e03cc3b890f7e4cc5faa44997d808193f"
|
|
||||||
dependencies = [
|
|
||||||
"base64 0.21.7",
|
|
||||||
"js-sys",
|
|
||||||
"pem",
|
|
||||||
"ring",
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
"simple_asn1",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kv-log-macro"
|
name = "kv-log-macro"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
|
@ -1902,16 +1884,6 @@ version = "1.0.15"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pem"
|
|
||||||
version = "3.0.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8e459365e590736a54c3fa561947c84837534b8e9af6fc5bf781307e82658fae"
|
|
||||||
dependencies = [
|
|
||||||
"base64 0.22.1",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pem-rfc7468"
|
name = "pem-rfc7468"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
|
@ -2648,18 +2620,6 @@ version = "0.1.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
|
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "simple_asn1"
|
|
||||||
version = "0.6.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "adc4e5204eb1910f40f9cfa375f6f05b68c3abac4b6fd879c8ff5e7ae8a0a085"
|
|
||||||
dependencies = [
|
|
||||||
"num-bigint",
|
|
||||||
"num-traits",
|
|
||||||
"thiserror",
|
|
||||||
"time",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "slab"
|
name = "slab"
|
||||||
version = "0.4.9"
|
version = "0.4.9"
|
||||||
|
|
|
@ -17,4 +17,3 @@ sea-orm = { version = "0.12", features = [
|
||||||
"with-uuid",
|
"with-uuid",
|
||||||
] }
|
] }
|
||||||
dotenvy = "*"
|
dotenvy = "*"
|
||||||
jsonwebtoken = "*"
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
use jsonwebtoken::{EncodingKey, Header, Validation};
|
|
||||||
use migration::token;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
|
||||||
struct Claims {
|
|
||||||
sub: Uuid,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_jwt(
|
|
||||||
user: entity::user::Model,
|
|
||||||
key: &EncodingKey,
|
|
||||||
) -> Result<String, jsonwebtoken::errors::Error> {
|
|
||||||
let claims = Claims {
|
|
||||||
sub: user.id,
|
|
||||||
name: user.name,
|
|
||||||
};
|
|
||||||
jsonwebtoken::encode(&Header::default(), &claims, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify(token: &str) {
|
|
||||||
let validation = Validation::new(jsonwebtoken::Algorithm::HS256);
|
|
||||||
// jsonwebtoken::decode(token, , validation)
|
|
||||||
}
|
|
|
@ -1,12 +1,14 @@
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::{ErrorBadRequest, ErrorInternalServerError},
|
error::{ErrorBadRequest, ErrorInternalServerError},
|
||||||
web, HttpResponse, Responder,
|
web, Responder,
|
||||||
};
|
};
|
||||||
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
||||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{auth::create_jwt, AppState};
|
use crate::AppState;
|
||||||
|
|
||||||
|
use super::user::UserWithoutPassword;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct LoginRequest {
|
pub struct LoginRequest {
|
||||||
|
@ -22,7 +24,6 @@ impl AuthController {
|
||||||
login: web::Json<LoginRequest>,
|
login: web::Json<LoginRequest>,
|
||||||
) -> actix_web::Result<impl Responder> {
|
) -> actix_web::Result<impl Responder> {
|
||||||
let db = &state.db;
|
let db = &state.db;
|
||||||
let jwt_secret = &state.secret;
|
|
||||||
let login = login.into_inner();
|
let login = login.into_inner();
|
||||||
|
|
||||||
let user = entity::user::Entity::find()
|
let user = entity::user::Entity::find()
|
||||||
|
@ -38,7 +39,6 @@ impl AuthController {
|
||||||
.verify_password(login.password.as_bytes(), &parsed_hash)
|
.verify_password(login.password.as_bytes(), &parsed_hash)
|
||||||
.map_err(ErrorBadRequest)?;
|
.map_err(ErrorBadRequest)?;
|
||||||
|
|
||||||
let jwt = create_jwt(user, jwt_secret).map_err(ErrorInternalServerError)?;
|
Ok(web::Json(UserWithoutPassword::from(user)))
|
||||||
Ok(HttpResponse::Ok().body(jwt))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
use jsonwebtoken::EncodingKey;
|
|
||||||
use migration::MigratorTrait;
|
use migration::MigratorTrait;
|
||||||
use sea_orm::{Database, DatabaseConnection};
|
use sea_orm::{Database, DatabaseConnection};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use routes::config;
|
use routes::config;
|
||||||
mod auth;
|
|
||||||
mod controller;
|
mod controller;
|
||||||
mod routes;
|
mod routes;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
db: DatabaseConnection,
|
db: DatabaseConnection,
|
||||||
secret: EncodingKey,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
|
@ -23,7 +20,6 @@ async fn main() -> std::io::Result<()> {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
let jwt_secret = env::var("TOKEN_SECRET").expect("TOKEN_SECRET must be set");
|
|
||||||
|
|
||||||
let conn = Database::connect(&db_url)
|
let conn = Database::connect(&db_url)
|
||||||
.await
|
.await
|
||||||
|
@ -35,10 +31,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.expect("Running migrations failed");
|
.expect("Running migrations failed");
|
||||||
println!("Finished running migrations");
|
println!("Finished running migrations");
|
||||||
|
|
||||||
let state = AppState {
|
let state = AppState { db: conn };
|
||||||
db: conn,
|
|
||||||
secret: EncodingKey::from_secret(jwt_secret.as_bytes()),
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("Listening for connections...");
|
println!("Listening for connections...");
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
|
Loading…
Reference in a new issue