Compare commits

...

8 Commits

Author SHA1 Message Date
filipriec
4ced1a36d4 moved form.rs into the state where it really belongs to 2025-03-25 21:23:52 +01:00
filipriec
45fff34c4c login page being implemented slowly 2025-03-25 16:02:23 +01:00
filipriec
c84fa4a692 frontend implementing login 2025-03-25 15:57:45 +01:00
filipriec
eba3f56ba3 indefinite jwt expiration set 2025-03-25 13:17:13 +01:00
filipriec
71ab588c16 tonic rbac to tower 2025-03-25 12:36:31 +01:00
filipriec
195375c083 temp disable of the rbac 2025-03-25 12:35:10 +01:00
filipriec
34dafcc23e rbac using tonic 2025-03-25 11:33:14 +01:00
filipriec
507f86fcf1 docs 2025-03-25 10:35:22 +01:00
24 changed files with 257 additions and 20 deletions

View File

@@ -0,0 +1,6 @@
// src/components/form.rs
pub mod login;
pub mod register;
pub use login::*;
pub use register::*;

View File

@@ -0,0 +1,111 @@
// src/components/login/login.rs
use ratatui::{
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
style::Style,
text::{Line, Span, Text},
layout::{Alignment, Constraint, Direction, Layout, Rect},
Frame,
};
use crate::config::colors::themes::Theme;
pub struct LoginState {
pub fields: Vec<String>,
pub values: Vec<String>,
pub selected_field: usize,
}
impl LoginState {
pub fn new() -> Self {
Self {
fields: vec!["Username".to_string(), "Password".to_string()],
values: vec![String::new(), String::new()],
selected_field: 0,
}
}
pub fn next_field(&mut self) {
self.selected_field = (self.selected_field + 1) % self.fields.len();
}
pub fn previous_field(&mut self) {
self.selected_field = if self.selected_field == 0 {
self.fields.len() - 1
} else {
self.selected_field - 1
};
}
pub fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme) {
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
let inner_area = block.inner(area);
f.render_widget(block, area);
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Length(5),
Constraint::Min(1),
])
.split(inner_area);
// Title
let title = Line::from(Span::styled("Login", Style::default().fg(theme.highlight)));
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
f.render_widget(title_widget, chunks[0]);
// Login form
let form_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
])
.split(chunks[1]);
// Username field
let username_block = Block::default()
.title("Username")
.borders(Borders::ALL)
.border_style(if self.selected_field == 0 {
Style::default().fg(theme.highlight)
} else {
Style::default().fg(theme.border)
});
let username = Paragraph::new(self.values[0].as_str())
.block(username_block);
f.render_widget(username, form_chunks[0]);
// Password field
let password_block = Block::default()
.title("Password")
.borders(Borders::ALL)
.border_style(if self.selected_field == 1 {
Style::default().fg(theme.highlight)
} else {
Style::default().fg(theme.border)
});
let password = Paragraph::new("*".repeat(self.values[1].len()))
.block(password_block);
f.render_widget(password, form_chunks[1]);
// Submit button
let submit_block = Block::default()
.borders(Borders::ALL)
.border_style(if self.selected_field == 2 {
Style::default().fg(theme.highlight)
} else {
Style::default().fg(theme.border)
});
let submit = Paragraph::new("Submit")
.block(submit_block)
.alignment(Alignment::Center);
f.render_widget(submit, form_chunks[2]);
}
}

View File

View File

@@ -6,7 +6,7 @@ use ratatui::{
Frame,
};
use crate::config::colors::themes::Theme;
use crate::ui::form::FormState;
use crate::state::pages::form::FormState;
use crate::components::handlers::canvas::render_canvas;
pub fn render_form(

View File

@@ -8,7 +8,7 @@ use ratatui::{
prelude::Alignment,
};
use crate::config::colors::themes::Theme;
use crate::ui::form::FormState;
use crate::state::pages::form::FormState;
pub fn render_canvas(
f: &mut Frame,

View File

@@ -1,4 +1,4 @@
// src/components/handlers/intro.rs
// src/components/intro/intro.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
@@ -33,7 +33,7 @@ impl IntroState {
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(35),
Constraint::Length(5),
Constraint::Length(7), // Increased to accommodate 3 buttons
Constraint::Percentage(35),
])
.split(inner_area);
@@ -48,10 +48,14 @@ impl IntroState {
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[1]);
// Buttons
// Buttons - now with 3 options
let button_area = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.constraints([
Constraint::Percentage(33),
Constraint::Percentage(33),
Constraint::Percentage(33),
])
.split(chunks[1].inner(Margin {
horizontal: 1,
vertical: 1
@@ -71,6 +75,13 @@ impl IntroState {
self.selected_option == 1,
theme,
);
self.render_button(
f,
button_area[2],
"Login",
self.selected_option == 2,
theme,
);
}
fn render_button(&self, f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
@@ -101,10 +112,22 @@ impl IntroState {
}
pub fn next_option(&mut self) {
self.selected_option = (self.selected_option + 1) % 2;
self.selected_option = (self.selected_option + 1) % 3;
}
pub fn previous_option(&mut self) {
self.selected_option = if self.selected_option == 0 { 1 } else { 0 };
self.selected_option = if self.selected_option == 0 { 2 } else { self.selected_option - 1 };
}
pub fn handle_selection(&self, app_state: &mut crate::state::state::AppState) {
match self.selected_option {
0 => { /* Continue logic */ }
1 => { /* Admin logic */ }
2 => {
app_state.ui.show_intro = false;
app_state.ui.show_login = true;
}
_ => {}
}
}
}

View File

@@ -4,9 +4,11 @@ pub mod intro;
pub mod admin;
pub mod common;
pub mod form;
pub mod auth;
pub use handlers::*;
pub use intro::*;
pub use admin::*;
pub use common::*;
pub use form::*;
pub use auth::*;

View File

@@ -5,7 +5,7 @@ use crate::config::binds::config::Config;
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::tui::terminal::core::TerminalCore;
use crate::tui::controls::commands::CommandHandler;
use crate::ui::handlers::form::FormState;
use crate::state::pages::form::FormState;
use crate::state::state::AppState;
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};

View File

@@ -6,7 +6,7 @@ use crate::tui::terminal::{
grpc_client::GrpcClient,
};
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use crate::state::pages::form::FormState;
use crate::modes::canvas::common;
pub async fn handle_edit_event_internal(

View File

@@ -2,7 +2,7 @@
use crossterm::event::{KeyEvent};
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use crate::state::pages::form::FormState;
use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::tui::terminal::grpc_client::GrpcClient;

View File

@@ -3,7 +3,7 @@
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use crate::state::pages::form::FormState;
use crate::tui::controls::commands::CommandHandler;
use crate::tui::terminal::core::TerminalCore;
use crate::modes::{

View File

@@ -3,7 +3,7 @@
use crossterm::event::KeyEvent;
use crate::config::binds::config::Config;
use crate::state::state::AppState;
use crate::ui::handlers::form::FormState;
use crate::state::pages::form::FormState;
pub async fn handle_navigation_event(
key: KeyEvent,

View File

@@ -7,7 +7,7 @@ use crate::tui::terminal::{
};
use crate::tui::controls::commands::CommandHandler;
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use crate::state::pages::form::FormState;
use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::{
common::{command_mode},

View File

@@ -1,2 +1,3 @@
// src/state/mod.rs
pub mod state;
pub mod pages;

View File

@@ -0,0 +1,3 @@
// src/state/pages.rs
pub mod form;

View File

@@ -1,4 +1,4 @@
// src/client/ui/handlers/form.rs
// src/state/pages/form.rs
use crate::config::colors::themes::Theme;
use ratatui::layout::Rect;
use ratatui::Frame;

View File

@@ -11,6 +11,7 @@ pub struct UiState {
pub show_intro: bool,
pub show_admin: bool,
pub show_form: bool,
pub show_login: bool,
pub intro_state: IntroState,
}
@@ -75,6 +76,7 @@ impl Default for UiState {
show_intro: true,
show_admin: false,
show_form: false,
show_login: false,
intro_state: IntroState::new(),
}
}

View File

@@ -1,7 +1,6 @@
// src/client/ui/handlers.rs
pub mod ui;
pub mod form;
pub mod render;
pub mod rat_state;

View File

@@ -12,7 +12,7 @@ use crate::components::{
use crate::config::colors::themes::Theme;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::Frame;
use super::form::FormState;
use crate::state::pages::form::FormState;
use crate::state::state::AppState;
pub fn render_ui(

View File

@@ -6,7 +6,8 @@ use crate::tui::controls::CommandHandler;
use crate::tui::terminal::EventReader;
use crate::config::colors::themes::Theme;
use crate::config::binds::config::Config;
use crate::ui::handlers::{form::FormState, render::render_ui};
use crate::ui::handlers::render::render_ui;
use crate::state::pages::form::FormState;
use crate::modes::handlers::event::EventHandler;
use crate::state::state::AppState;
use crate::components::admin::{admin_panel::AdminPanelState};

View File

@@ -0,0 +1,51 @@
grpcurl -plaintext -d '{
"username": "testuser3",
"email": "test3@example.com",
"password": "your_password",
"password_confirmation": "your_password"
}' localhost:50051 multieko2.auth.AuthService/Register
{
"id": "96d2fd35-b39d-4c05-916a-66134453d34c",
"username": "testuser3",
"email": "test3@example.com",
"role": "accountant"
}
grpcurl -plaintext -d '{
"identifier": "testuser3"
}' localhost:50051 multieko2.auth.AuthService/Login
ERROR:
Code: Unauthenticated
Message: Invalid credentials
grpcurl -plaintext -d '{
"identifier": "testuser3",
"password": "your_password"
}' localhost:50051 multieko2.auth.AuthService/Login
{
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI5NmQyZmQzNS1iMzlkLTRjMDUtOTE2YS02NjEzNDQ1M2QzNGMiLCJleHAiOjE3NDI5ODE2MTAsInJvbGUiOiJhY2NvdW50YW50In0.78VIR3X4QZohzeI5x3xmkmqcICTusOC6PELPohMV-k8",
"tokenType": "Bearer",
"expiresIn": 86400,
"userId": "96d2fd35-b39d-4c05-916a-66134453d34c",
"role": "accountant"
}
grpcurl -plaintext -d '{
"username": "testuser4",
"email": "test4@example.com"
}' localhost:50051 multieko2.auth.AuthService/Register
{
"id": "413d7ecc-f231-48af-8c5a-566b1dc2bf0b",
"username": "testuser4",
"email": "test4@example.com",
"role": "accountant"
}
grpcurl -plaintext -d '{
"identifier": "test4@example.com"
}' localhost:50051 multieko2.auth.AuthService/Login
{
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0MTNkN2VjYy1mMjMxLTQ4YWYtOGM1YS01NjZiMWRjMmJmMGIiLCJleHAiOjE3NDI5ODE3MDEsInJvbGUiOiJhY2NvdW50YW50In0.4Hzu3tTZRNGHnBSgeCbGy2tFTl8EzpPdXBhcW8kuIc8",
"tokenType": "Bearer",
"expiresIn": 86400,
"userId": "413d7ecc-f231-48af-8c5a-566b1dc2bf0b",
"role": "accountant"
}
╭─    ~/Doc/pr/multieko2/server    auth ······ ✔
╰─

View File

@@ -2,6 +2,8 @@
pub mod jwt;
pub mod middleware;
// TODO implement RBAC on all of the endpoints
// pub mod rbac;
pub use jwt::*;
pub use middleware::*;

View File

@@ -35,7 +35,7 @@ pub fn init_jwt() -> Result<(), AuthError> {
pub fn generate_token(user_id: Uuid, role: &str) -> Result<String, AuthError> {
let keys = KEYS.get().ok_or(AuthError::ConfigError("JWT not initialized".to_string()))?;
let exp = OffsetDateTime::now_utc() + Duration::hours(24);
let exp = OffsetDateTime::now_utc() + Duration::days(365000);
let claims = Claims {
sub: user_id,
exp: exp.unix_timestamp(),

View File

@@ -0,0 +1,36 @@
// src/auth/logic/rbac.rs
use tower::ServiceBuilder;
use crate::auth::logic::rbac;
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
// ... existing setup code ...
// Create service layers
let adresar_layer = ServiceBuilder::new()
.layer(rbac::create_adresar_layer())
.into_inner();
let uctovnictvo_layer = ServiceBuilder::new()
.layer(rbac::create_uctovnictvo_layer())
.into_inner();
// Create services with layers
let adresar_service = AdresarServer::new(AdresarService { db_pool: db_pool.clone() })
.layer(adresar_layer);
let uctovnictvo_service = UctovnictvoServer::new(UctovnictvoService { db_pool: db_pool.clone() })
.layer(uctovnictvo_layer);
// ... repeat for other services ...
Server::builder()
.add_service(auth_server)
.add_service(adresar_service)
.add_service(uctovnictvo_service)
// ... other services ...
.serve(addr)
.await?;
Ok(())
}