Be able to authenticate and get back Success, PasswordReset, and Locked

This commit is contained in:
Savanni D'Gerinel 2025-01-20 22:19:16 -05:00
parent 84ee790f0b
commit 90224a6841
15 changed files with 184 additions and 141 deletions

View File

@ -141,7 +141,6 @@ impl Core {
id: user.id,
name: user.name,
is_admin: user.admin,
games: vec![],
})
.collect()),
Err(err) => fatal(err),
@ -153,7 +152,6 @@ impl Core {
user_id: UserId,
) -> ResultExt<Option<UserOverview>, AppError, FatalError> {
let users = return_error!(self.list_users().await);
let games = return_error!(self.list_games().await);
let user = match users.into_iter().find(|user| user.id == user_id) {
Some(user) => user,
None => return ok(None),
@ -162,11 +160,6 @@ impl Core {
id: user.id.clone(),
name: user.name,
is_admin: user.is_admin,
games: games
.into_iter()
.filter(|g| g.gm == user.id)
.map(|g| g.id)
.collect(),
}))
}

View File

@ -1,23 +1,20 @@
mod game_management;
mod user_management;
mod types;
use axum::{http::StatusCode, Json};
use futures::Future;
pub use game_management::*;
pub use user_management::*;
pub use types::*;
use result_extended::ResultExt;
use serde::{Deserialize, Serialize};
use crate::{
core::Core,
types::{AppError, FatalError},
};
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct HealthCheck {
pub ok: bool,
}
pub async fn wrap_handler<F, A, Fut>(f: F) -> (StatusCode, Json<Option<A>>)
where
F: FnOnce() -> Fut,

View File

@ -0,0 +1,83 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use crate::database::UserId;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct HealthCheck {
pub ok: bool,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type", content = "content")]
#[typeshare]
pub enum AccountState {
Normal,
PasswordReset(String),
Locked,
}
impl From<crate::types::AccountState> for AccountState {
fn from(s: crate::types::AccountState) -> Self {
match s {
crate::types::AccountState::Normal => Self::Normal,
crate::types::AccountState::PasswordReset(r) => {
Self::PasswordReset(format!("{}", r.format("%Y-%m-%d %H:%M:%S")))
}
crate::types::AccountState::Locked => Self::Locked,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct User {
pub id: UserId,
pub name: String,
pub password: String,
pub admin: bool,
pub state: AccountState,
}
impl From<crate::types::User> for User {
fn from(u: crate::types::User) -> Self {
Self {
id: u.id,
name: u.name,
password: u.password,
admin: u.admin,
state: AccountState::from(u.state),
}
}
}
/*
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct UserOverview {
pub id: UserId,
pub name: String,
pub is_admin: bool,
pub games: Vec<crate::types::GameOverview>,
}
impl UserOverview {
pub fn new(user: crate::types::UserOverview, games: Vec<crate::types::GameOverview>) -> Self {
let s = Self::from(user);
Self{ games, ..s }
}
}
impl From<crate::types::UserOverview> for UserOverview {
fn from(input: crate::types::UserOverview) -> Self {
Self {
id: input.id,
name: input.name,
is_admin: input.is_admin,
games: vec![],
}
}
}
*/

View File

@ -7,7 +7,7 @@ use typeshare::typeshare;
use crate::{
core::{AuthResponse, Core},
database::{SessionId, UserId},
types::{AppError, FatalError, User, UserOverview},
types::{AppError, FatalError, User},
};
#[derive(Clone, Debug, Deserialize, Serialize)]
@ -106,20 +106,19 @@ pub async fn get_user(
core: Core,
headers: HeaderMap,
user_id: Option<UserId>,
) -> ResultExt<Option<UserOverview>, AppError, FatalError> {
) -> ResultExt<Option<crate::types::UserOverview>, AppError, FatalError> {
auth_required(core.clone(), headers, |user| async move {
match user_id {
Some(user_id) => core.user(user_id).await,
None => core.user(user.id).await,
}
})
.await
}).await
}
pub async fn get_users(
core: Core,
headers: HeaderMap,
) -> ResultExt<Vec<UserOverview>, AppError, FatalError> {
) -> ResultExt<Vec<crate::types::UserOverview>, AppError, FatalError> {
auth_required(core.clone(), headers, |_user| async move {
core.list_users().await
})

View File

@ -7,9 +7,7 @@ use axum::{
routing::{get, post, put},
Json, Router,
};
use serde::{Deserialize, Serialize};
use tower_http::cors::{Any, CorsLayer};
use typeshare::typeshare;
use crate::{
core::Core,
@ -20,50 +18,6 @@ use crate::{
},
};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type", content = "content")]
#[typeshare]
pub enum AccountState {
Normal,
PasswordReset(String),
Locked,
}
impl From<crate::types::AccountState> for AccountState {
fn from(s: crate::types::AccountState) -> Self {
match s {
crate::types::AccountState::Normal => Self::Normal,
crate::types::AccountState::PasswordReset(r) => {
Self::PasswordReset(format!("{}", r.format("%Y-%m-%d %H:%M:%S")))
}
crate::types::AccountState::Locked => Self::Locked,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct User {
pub id: UserId,
pub name: String,
pub password: String,
pub admin: bool,
pub state: AccountState,
}
impl From<crate::types::User> for User {
fn from(u: crate::types::User) -> Self {
Self {
id: u.id,
name: u.name,
password: u.password,
admin: u.admin,
state: AccountState::from(u.state),
}
}
}
pub fn routes(core: Core) -> Router {
Router::new()
.route(

View File

@ -167,16 +167,17 @@ pub enum Message {
UpdateTabletop(Tabletop),
}
#[derive(Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct UserOverview {
pub id: UserId,
pub name: String,
pub is_admin: bool,
pub games: Vec<GameId>,
}
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct GameOverview {
pub id: GameId,

View File

@ -32,8 +32,8 @@ interface AuthedViewProps {
const AuthedView = ({ client, children }: PropsWithChildren<AuthedViewProps>) => {
const [state, manager] = useContext(StateContext)
return (
<Authentication onAdminPassword={(password) => {
manager.setAdminPassword(password)
<Authentication onSetPassword={(password1, password2) => {
manager.setPassword(password1, password2)
}} onAuth={(username, password) => manager.auth(username, password)}>
{children}
</Authentication>

View File

@ -1,4 +1,4 @@
import { SessionId, UserId, UserProfile } from "visions-types";
import { AuthResponse, SessionId, UserId, UserOverview } from "visions-types";
export type PlayingField = {
backgroundImage: string;
@ -63,14 +63,18 @@ export class Client {
return fetch(url).then((response) => response.json());
}
async setAdminPassword(password: string) {
async setPassword(password_1: string, password_2: string) {
const url = new URL(this.base);
url.pathname = `/api/v1/admin_password`;
console.log("setting the admin password to: ", password);
return fetch(url, { method: 'PUT', headers: [['Content-Type', 'application/json']], body: JSON.stringify(password) });
url.pathname = `/api/v1/user/password`;
return fetch(url, {
method: 'PUT', headers: [['Content-Type', 'application/json']], body: JSON.stringify({
password_1, password_2,
})
});
}
async auth(username: string, password: string): Promise<SessionId | undefined> {
async auth(username: string, password: string): Promise<AuthResponse> {
const url = new URL(this.base);
url.pathname = `/api/v1/auth`
const response = await fetch(url, {
@ -78,11 +82,10 @@ export class Client {
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({ 'username': username, 'password': password })
});
const session_id: SessionId = await response.json();
return session_id;
return await response.json();
}
async profile(sessionId: SessionId, userId: UserId | undefined): Promise<UserProfile | undefined> {
async profile(sessionId: SessionId, userId: UserId | undefined): Promise<UserOverview | undefined> {
const url = new URL(this.base);
if (userId) {
url.pathname = `/api/v1/user${userId}`

View File

@ -2,8 +2,8 @@ import { GameOverview } from "visions-types"
import { CardElement } from '../Card/Card';
export const GameOverviewElement = ({ game_type, game_name, gm, players }: GameOverview) => {
return (<CardElement name={game_name}>
export const GameOverviewElement = ({ name, gm, players }: GameOverview) => {
return (<CardElement name={name}>
<p><i>GM</i> {gm}</p>
<ul>
{players.map((player) => player)}

View File

@ -1,29 +1,21 @@
import { UserProfile } from 'visions-types';
import { GameOverview, UserOverview } from 'visions-types';
import { CardElement, GameOverviewElement, UserManagementElement } from '..';
import './Profile.css';
interface ProfileProps {
profile: UserProfile,
users: UserProfile[],
profile: UserOverview,
games: GameOverview[],
}
export const ProfileElement = ({ profile, users }: ProfileProps) => {
const adminNote = profile.is_admin ? <div> <i>Note: this user is an admin</i> </div> : <></>;
export const ProfileElement = ({ profile, games }: ProfileProps) => {
const adminNote = profile.isAdmin ? <div> <i>Note: this user is an admin</i> </div> : <></>;
return (<div className="profile">
<CardElement name={profile.name}>
<div>Games: {profile.games.map((game) => {
return <span key={game.id}>{game.game_name} ({game.game_type})</span>;
<div>Games: {games.map((game) => {
return <span key={game.id}>{game.name} ({game.type})</span>;
}) }</div>
{adminNote}
</CardElement>
<div className="profile_columns">
<UserManagementElement users={users} />
<div>
{profile.games.map((game) => <GameOverviewElement {...game} />)}
</div>
</div>
</div>)
}

View File

@ -1,8 +1,8 @@
import { UserProfile } from "visions-types"
import { UserOverview } from "visions-types"
import { CardElement } from ".."
interface UserManagementProps {
users: UserProfile[]
users: UserOverview[]
}
export const UserManagementElement = ({ users }: UserManagementProps ) => {

View File

@ -3,7 +3,7 @@ import { SessionId, Status, Tabletop } from "visions-types";
import { Client } from "../../client";
import { assertNever } from "../../utils";
type AuthState = { type: "NoAdmin" } | { type: "Unauthed" } | { type: "Authed", sessionId: string };
type AuthState = { type: "Unauthed" } | { type: "Authed", sessionId: string } | { type: "PasswordReset", sessionId: string };
export enum LoadingState {
Loading,
@ -21,7 +21,7 @@ type Action = { type: "SetAuthState", content: AuthState };
const initialState = (): AppState => {
let state: AppState = {
state: LoadingState.Ready,
auth: { type: "NoAdmin" },
auth: { type: "Unauthed" },
tabletop: { backgroundColor: { red: 0, green: 0, blue: 0 }, backgroundImage: undefined },
}
@ -36,6 +36,7 @@ const initialState = (): AppState => {
const stateReducer = (state: AppState, action: Action): AppState => {
switch (action.type) {
case "SetAuthState": {
console.log("setReducer: ", action);
return { ...state, auth: action.content }
}
/*
@ -51,9 +52,9 @@ export const authState = (state: AppState): AuthState => state.auth
export const getSessionId = (state: AppState): SessionId | undefined => {
switch (state.auth.type) {
case "NoAdmin": return undefined
case "Unauthed": return undefined
case "Authed": return state.auth.sessionId
case "PasswordReset": return state.auth.sessionId
default: {
assertNever(state.auth)
return undefined
@ -70,35 +71,38 @@ class StateManager {
this.dispatch = dispatch;
}
async status() {
async setPassword(password1: string, password2: string) {
if (!this.client || !this.dispatch) return;
const { admin_enabled } = await this.client.health();
if (!admin_enabled) {
this.dispatch({ type: "SetAuthState", content: { type: "NoAdmin" } });
}
return admin_enabled;
}
async setAdminPassword(password: string) {
if (!this.client || !this.dispatch) return;
await this.client.setAdminPassword(password);
let admin_enabled = await this.status();
if (admin_enabled) {
this.dispatch({ type: "SetAuthState", content: { type: "Unauthed" } });
}
await this.client.setPassword(password1, password2);
}
async auth(username: string, password: string) {
if (!this.client || !this.dispatch) return;
let sessionId = await this.client.auth(username, password);
let authResponse = await this.client.auth(username, password);
switch (authResponse.type) {
case "Success": {
window.localStorage.setItem("sessionId", authResponse.content);
this.dispatch({ type: "SetAuthState", content: { type: "Authed", sessionId: authResponse.content } });
break;
}
case "PasswordReset": {
window.localStorage.setItem("sessionId", authResponse.content);
this.dispatch({ type: "SetAuthState", content: { type: "PasswordReset", sessionId: authResponse.content } });
break;
}
case "Locked": {
this.dispatch({ type: "SetAuthState", content: { type: "Unauthed" } });
break;
}
}
/*
if (sessionId) {
window.localStorage.setItem("sessionId", sessionId);
this.dispatch({ type: "SetAuthState", content: { type: "Authed", sessionId } });
}
*/
}
}
@ -111,10 +115,6 @@ export const StateProvider = ({ client, children }: PropsWithChildren<StateProvi
const stateManager = useRef(new StateManager(client, dispatch));
useEffect(() => {
stateManager.current.status();
}, [stateManager]);
return <StateContext.Provider value={[state, stateManager.current]}>
{children}
</StateContext.Provider>;

View File

@ -7,10 +7,27 @@ interface UserRowProps {
}
const UserRow = ({ user }: UserRowProps) => {
let accountState = "Normal";
switch (user.state.type) {
case "Normal": {
accountState = "Normal";
break;
}
case "PasswordReset": {
accountState = `PasswordReset until ${user.state.content}`;
break;
}
case "Locked": {
accountState = "Locked";
break;
}
}
return (<tr>
<td> {user.name} </td>
<td> {user.admin && "admin"} </td>
<td> {user.enabled && "enabled"} </td>
<td> {accountState} </td>
</tr>);
}

View File

@ -4,43 +4,36 @@ import { assertNever } from '../../utils';
import './Authentication.css';
interface AuthenticationProps {
onAdminPassword: (password: string) => void;
onSetPassword: (password1: string, password2: string) => void;
onAuth: (username: string, password: string) => void;
}
export const Authentication = ({ onAdminPassword, onAuth, children }: PropsWithChildren<AuthenticationProps>) => {
export const Authentication = ({ onSetPassword, onAuth, children }: PropsWithChildren<AuthenticationProps>) => {
// No admin password set: prompt for the admin password
// Password set, nobody logged in: prompt for login
// User logged in: show the children
let [userField, setUserField] = useState<string>("");
let [pwField, setPwField] = useState<string>("");
let [pwField1, setPwField1] = useState<string>("");
let [pwField2, setPwField2] = useState<string>("");
let [state, _] = useContext(StateContext);
console.log("Authentication component", state.state);
switch (state.state) {
case LoadingState.Loading: {
return <div>Loading</div>
}
case LoadingState.Ready: {
switch (state.auth.type) {
case "NoAdmin": {
return <div className="auth">
<div className="card">
<h1> Welcome to your new Visions VTT Instance </h1>
<p> Set your admin password: </p>
<input type="password" placeholder="Password" onChange={(evt) => setPwField(evt.target.value)} />
<input type="submit" value="Submit" onClick={() => onAdminPassword(pwField)} />
</div>
</div>;
}
case "Unauthed": {
return <div className="auth card">
<div className="card">
<h1> Welcome to Visions VTT </h1>
<div className="auth__input-line">
<input type="text" placeholder="Username" onChange={(evt) => setUserField(evt.target.value)} />
<input type="password" placeholder="Password" onChange={(evt) => setPwField(evt.target.value)} />
<input type="submit" value="Sign in" onClick={() => onAuth(userField, pwField)} />
<input type="password" placeholder="Password" onChange={(evt) => setPwField1(evt.target.value)} />
<input type="submit" value="Sign in" onClick={() => onAuth(userField, pwField1)} />
</div>
</div>
</div>;
@ -48,6 +41,17 @@ export const Authentication = ({ onAdminPassword, onAuth, children }: PropsWithC
case "Authed": {
return <div> {children} </div>;
}
case "PasswordReset": {
return <div className="auth">
<div className="card">
<h1> Password Reset </h1>
<p> Your password currently requires a reset. </p>
<input type="password" placeholder="Password" onChange={(evt) => setPwField1(evt.target.value)} />
<input type="password" placeholder="Retype your Password" onChange={(evt) => setPwField2(evt.target.value)} />
<input type="submit" value="Submit" onClick={() => onSetPassword(pwField1, pwField2)} />
</div>
</div>;
}
default: {
assertNever(state.auth);
return <div></div>;

View File

@ -1,5 +1,5 @@
import { useContext, useEffect, useState } from 'react';
import { UserProfile } from 'visions-types';
import { UserOverview } from 'visions-types';
import { Client } from '../../client';
import { ProfileElement } from '../../components';
import { getSessionId, StateContext, StateProvider } from '../../providers/StateProvider/StateProvider';
@ -10,8 +10,8 @@ interface MainProps {
export const MainView = ({ client }: MainProps) => {
const [state, _manager] = useContext(StateContext)
const [profile, setProfile] = useState<UserProfile | undefined>(undefined)
const [users, setUsers] = useState<UserProfile[]>([])
const [profile, setProfile] = useState<UserOverview | undefined>(undefined)
const [users, setUsers] = useState<UserOverview[]>([])
const sessionId = getSessionId(state)
useEffect(() => {
@ -23,7 +23,7 @@ export const MainView = ({ client }: MainProps) => {
return (
<div>
{profile && <ProfileElement profile={profile} users={[]} />}
{profile && <ProfileElement profile={profile} games={[]} />}
</div>
)
}