Ignore:
Timestamp:
09/24/26 17:43:19 (5 days ago)
Author:
Stefan <trsunovstefan@…>
Branches:
main
Children:
0cee8ec
Parents:
a531b45
Message:

Wiki docs, phase 6 and phase 7 added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/P4-Prototype/UseCase0002Implementation.md

    ra531b45 ref1c1c7  
    1 # Use-case 0002 Implementation — Log in
     1# Use-case 0002 Implementation - Log in
    22
    3 **Initiating actor:** Visitor. **Source file:** `server/auth.go`, function `Login` + `authenticate`.
     3**Initiating actor:** Visitor
    44
    5 ## Scenario (implemented)
     5**Other actors:** —
    66
    7 1. **User** chooses option `[2] Login`.
     7A registered user authenticates with a username and password so that the system
     8treats all following actions as actions of that Trader. The system looks the user up
     9by username and compares the stored password hash with the SHA-256 hash of the
     10entered password. An unknown username and a wrong password give the same answer,
     11`Invalid credentials.`, so the system does not reveal which usernames exist. After a
     12successful login the user's id and username are kept in the in-process session and
     13the authenticated (Trader) menu is shown, from which all other Trader use-cases
     14start.
    815
     16Original use-case description (P3): [UseCase0002](../P3-UseCaseModel/UseCase0002.md).
     17Implementation: [`server/auth.go`](../../server/auth.go), functions `Login` and
     18`authenticate` (password hash by `hashPassword`).
    919
    10 2. **System** prompts for username and password.
     20## Scenario
    1121
    12 3. **User** enters `alice` / `test123`.
     221. **Visitor** chooses `[2] Login` in the anonymous menu (types `2`).
     232. **System** prints `-- Login --` and asks for `Username:` and then `Password:`.
    1324
    14 4. **System** looks up the user and compares hashes:
     25   The screenshot shows steps 1–2: option `2` is chosen and the `Username:` prompt
     26   is waiting for input.
     27
     28   ![UC0002 steps 1-2: Visitor chooses Login, system asks for credentials](screenshots/uc0002_1_login.png)
     29
     303. **Visitor** enters the username and the password. (If either is empty, the
     31   system prints `Username and password are required.` without accessing the
     32   database.)
     334. **System** looks up the user (`$1` = entered username):
    1534
    1635   ```sql
    17    SELECT id, password_hash FROM users WHERE username = $1;
     36   SELECT id, password_hash FROM users WHERE username = $1
    1837   ```
    1938
    20    The Go side (`authenticate` in `server/auth.go`) compares the returned `password_hash` against `sha256hex(entered_password)`.
     395. If no row is returned (`sql.ErrNoRows` in Go), the **System** responds
     40   `Invalid credentials.` and the scenario ends.
     416. If a row is returned, the **System** compares the returned `password_hash` with
     42   `hashPassword(entered password)` (hex-encoded SHA-256, computed in Go). On a
     43   mismatch it responds `Invalid credentials.` and the scenario ends.
    2144
    22 5. **System** on success stores `{UserID, Username}` in the in-process `Session` and shows the authenticated menu.
     45   The screenshot shows this failure path with an existing user and a wrong
     46   password: `alice` / `wrongpass`. The query from step 4 finds alice's row, the
     47   hash comparison of step 6 fails, and the system prints `Invalid credentials.` and
     48   returns to the anonymous menu. (An unknown username — step 5 — prints exactly the
     49   same message.)
    2350
    24    ![A rejected login followed by a successful one](screenshots/uc0002_login.png)
     51   ![UC0002 steps 5-6: wrong password, Invalid credentials](screenshots/uc0002_5_6_invalid.png)
     52
     537. On a match, the **System** stores the returned `id` and the username in the
     54   session (`s.UserID`, `s.Username`), prints `Login successful.` and displays the
     55   authenticated menu headed `--- Logged in as alice ---`.
     56
     57   The screenshot shows steps 3–7 of the second, successful attempt with the seed
     58   credentials `alice` / `test123` (the first, rejected attempt is still visible at
     59   the top of the window).
     60
     61   ![UC0002 steps 3-7: correct credentials, authenticated menu](screenshots/uc0002_7_success.png)
     62
     63The query runs on the `project` schema (the connection sets
     64`search_path=project,public`), so `users` means `project.users`.
     65
     66### Alternate flow 4a (P3) — lookup combined with the live balance
     67
     68P3 describes an optional variant that checks the password in SQL and returns the
     69balances in the same query. The P4 prototype does **not** use it: login always uses
     70the query from step 4 with the hash comparison in Go, and the balances are read
     71separately when the Trader asks for them (`[1] View balance`, see
     72[UseCase0003](UseCase0003Implementation.md)).
    2573
    2674## Seed credentials
    2775
    28 | Username | Password | Balance |
    29 |----------|----------|---------|
    30 | `alice`    | `test123` | 10000.00 USD |
    31 | `bob`      | `test123` |  5000.00 USD |
    32 | `charlie`  | `test123` |  2500.00 USD |
     76State after `./eduberza -init` (`server/db/data_load.sql`):
     77
     78| Username  | Password  | Available balance | Invested balance | Holdings |
     79|-----------|-----------|------------------:|-----------------:|----------|
     80| `alice`   | `test123` | 8250.00 USD       | 1750.00 USD      | 0.5 ETH  |
     81| `bob`     | `test123` | 5000.00 USD       | 0.00 USD         | —        |
     82| `charlie` | `test123` | 2500.00 USD       | 0.00 USD         | —        |
     83
     84## How to reproduce
     85
     86```sh
     87./eduberza -init
     88./eduberza
     89# [2] Login: alice / wrongpass  -> Invalid credentials.
     90# [2] Login: alice / test123    -> Login successful.  (authenticated menu)
     91```
     92
     93The screenshots come from one real run of exactly these inputs.
Note: See TracChangeset for help on using the changeset viewer.