forked from Roflin/gamenight
Added Location and location ownership/rights to gamenight.
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
use std::future::{Ready, ready};
|
||||
use std::future::{ready, Ready};
|
||||
|
||||
use actix_web::{FromRequest, http, HttpRequest, dev::Payload, web::Data};
|
||||
use actix_web::{dev::Payload, http, web::Data, FromRequest, HttpRequest};
|
||||
use chrono::Utc;
|
||||
use jsonwebtoken::{encode, Header, EncodingKey, decode, DecodingKey, Validation};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use gamenight_database::{user::{get_user, User}, DbPool};
|
||||
use gamenight_database::{
|
||||
user::{get_user, User},
|
||||
DbPool,
|
||||
};
|
||||
|
||||
use super::error::ApiError;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
exp: i64,
|
||||
uid: Uuid
|
||||
uid: Uuid,
|
||||
}
|
||||
|
||||
pub struct AuthUser(pub User);
|
||||
@@ -25,24 +28,31 @@ pub struct AuthUser(pub User);
|
||||
// pub role: Role,
|
||||
// }
|
||||
|
||||
impl From<User> for AuthUser {
|
||||
impl From<User> for AuthUser {
|
||||
fn from(value: User) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_claims(req: &HttpRequest) -> Result<Claims, ApiError> {
|
||||
let token = req.headers()
|
||||
fn get_claims(req: &HttpRequest) -> Result<Claims, ApiError> {
|
||||
let token = req
|
||||
.headers()
|
||||
.get(http::header::AUTHORIZATION)
|
||||
.map(|h| h.to_str().unwrap().split_at(7).1.to_string());
|
||||
|
||||
let token = token.ok_or(ApiError{
|
||||
let token = token.ok_or(ApiError {
|
||||
status: 400,
|
||||
message: "JWT-token was not specified in the Authorization header as Bearer: token".to_string()
|
||||
message: "JWT-token was not specified in the Authorization header as Bearer: token"
|
||||
.to_string(),
|
||||
})?;
|
||||
|
||||
let secret = "secret";
|
||||
Ok(decode::<Claims>(token.as_str(), &DecodingKey::from_secret(secret.as_bytes()), &Validation::default())?.claims)
|
||||
Ok(decode::<Claims>(
|
||||
token.as_str(),
|
||||
&DecodingKey::from_secret(secret.as_bytes()),
|
||||
&Validation::default(),
|
||||
)?
|
||||
.claims)
|
||||
}
|
||||
|
||||
pub fn get_token(user: &User) -> Result<String, ApiError> {
|
||||
@@ -55,7 +65,8 @@ pub fn get_token(user: &User) -> Result<String, ApiError> {
|
||||
Ok(encode(
|
||||
&Header::default(),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(secret.as_bytes()))?)
|
||||
&EncodingKey::from_secret(secret.as_bytes()),
|
||||
)?)
|
||||
}
|
||||
|
||||
impl FromRequest for AuthUser {
|
||||
@@ -63,15 +74,15 @@ impl FromRequest for AuthUser {
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
||||
ready(
|
||||
(|| -> Result<AuthUser, ApiError>{
|
||||
let pool = req.app_data::<Data<DbPool>>().expect("No database configured");
|
||||
let mut conn = pool.get().expect("couldn't get db connection from pool");
|
||||
let uid = get_claims(req)?.uid;
|
||||
let user = get_user(&mut conn, uid)?;
|
||||
|
||||
Ok(user.into())
|
||||
})()
|
||||
)
|
||||
ready((|| -> Result<AuthUser, ApiError> {
|
||||
let pool = req
|
||||
.app_data::<Data<DbPool>>()
|
||||
.expect("No database configured");
|
||||
let mut conn = pool.get().expect("couldn't get db connection from pool");
|
||||
let uid = get_claims(req)?.uid;
|
||||
let user = get_user(&mut conn, uid)?;
|
||||
|
||||
Ok(user.into())
|
||||
})())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user