refactor: structure of an hexagonal architecture
Created a kernel crate to store models and future action implementations. Will be useful to create admin cli.
This commit is contained in:
parent
69af48bb62
commit
3713cc2443
87 changed files with 834 additions and 474 deletions
12
lib/http_server/src/services/app_session.rs
Normal file
12
lib/http_server/src/services/app_session.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
use fully_pub::fully_pub;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
/// represent a general app session (from http basic auth)
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[fully_pub]
|
||||
struct AppClientSession {
|
||||
client_id: String
|
||||
}
|
||||
|
||||
3
lib/http_server/src/services/mod.rs
Normal file
3
lib/http_server/src/services/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod session;
|
||||
pub mod oauth2;
|
||||
pub mod app_session;
|
||||
20
lib/http_server/src/services/oauth2.rs
Normal file
20
lib/http_server/src/services/oauth2.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use std::str::FromStr;
|
||||
use anyhow::{Result, Context};
|
||||
|
||||
use kernel::models::{authorization::AuthorizationScope, config::Application};
|
||||
|
||||
pub fn verify_redirect_uri(app: &Application, input_redirect_uri: &str) -> bool {
|
||||
app.allowed_redirect_uris
|
||||
.iter()
|
||||
.any(|uri| *uri == input_redirect_uri)
|
||||
}
|
||||
|
||||
pub fn parse_scope(scope_str: &str) -> Result<Vec<AuthorizationScope>> {
|
||||
let mut scopes: Vec<AuthorizationScope> = vec![];
|
||||
for part in scope_str.split(' ') {
|
||||
scopes.push(
|
||||
AuthorizationScope::from_str(part).context("Cannot parse space-delimited scope.")?
|
||||
)
|
||||
}
|
||||
Ok(scopes)
|
||||
}
|
||||
25
lib/http_server/src/services/session.rs
Normal file
25
lib/http_server/src/services/session.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use anyhow::Result;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use jsonwebtoken::{encode, decode, Header, Algorithm, Validation, EncodingKey, DecodingKey};
|
||||
use kernel::context::AppSecrets;
|
||||
|
||||
|
||||
pub fn create_token<T: Serialize>(secrets: &AppSecrets, claims: T) -> String {
|
||||
let token = encode(
|
||||
&Header::default(),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(secrets.jwt_secret.as_bytes())
|
||||
).expect("Create token");
|
||||
|
||||
token
|
||||
}
|
||||
|
||||
pub fn verify_token<T: DeserializeOwned>(secrets: &AppSecrets, jwt: &str) -> Result<T> {
|
||||
let token_data = decode::<T>(
|
||||
jwt,
|
||||
&DecodingKey::from_secret(secrets.jwt_secret.as_bytes()),
|
||||
&Validation::new(Algorithm::HS256)
|
||||
)?;
|
||||
|
||||
Ok(token_data.claims)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue