|
2 | 2 |
|
3 | 3 | All client API endpoints (`/api/client/fetch_messages`, `/api/client/send_message`, `/api/client/push_token_report`) require authentication via an external authentication service. |
4 | 4 |
|
5 | | -The external authentication service is implemented inside NethVoice FreePBX container [REST APIs](https://github.com/nethesis/ns8-nethvoice/tree/main/freepbx/var/www/html/freepbx/rest). |
| 5 | +The external authentication service is provided by NethCTI Middleware, which manages user credentials, chat capabilities, and Matrix homeserver configuration. |
6 | 6 |
|
7 | | -### External Auth Flow |
| 7 | +### External Auth Flow (2-Step Process) |
8 | 8 |
|
9 | | -When a client sends a request, the proxy: |
10 | | -1. Extracts the `username` (extension) and `password` from the request. |
11 | | -2. Calls `EXT_AUTH_URL` with a POST request containing JSON: `{"extension":"<username>","secret":"<password>"}`. |
12 | | -3. On successful auth (200), parses the response for `main_extension`, `sub_extensions`, and `user_name`, which are converted into a mapping and saved. |
13 | | -4. On failure (401 or other error), returns an authentication error and does NOT save the push token or create a mapping. |
14 | | -5. Auth responses are cached in-memory for `CACHE_TTL_SECONDS` seconds to reduce external service load. |
| 9 | +When a client sends a request with `username` and `password`, the proxy performs a 2-step authentication: |
| 10 | + |
| 11 | +**Step 1: Login (POST to `/api/login`)** |
| 12 | +1. Extract the username part from the `username` field (e.g., if `username` is `[email protected]`, extract `user`) |
| 13 | +2. POST to `{EXT_AUTH_URL}/api/login` with JSON payload: `{"username":"<user>","password":"<password>"}` |
| 14 | +3. On successful auth (200), parse the response to get the JWT `token` field |
| 15 | +4. Decode the JWT to extract claims (without signature verification) |
| 16 | + |
| 17 | +**Step 2: Verify Chat Capability & Fetch Configuration (GET from `/api/chat?users=1`)** |
| 18 | +5. Verify that the JWT contains the `nethvoice_cti.chat` claim set to `true` |
| 19 | +6. If the claim is missing or false, authentication fails |
| 20 | +7. GET from `{EXT_AUTH_URL}/api/chat?users=1` with Bearer token in Authorization header |
| 21 | +8. Parse the response to extract: |
| 22 | + - Matrix homeserver configuration (`matrix.base_url`, `matrix.acrobits_url`) |
| 23 | + - User mappings from the `users` array (`user_name`, `main_extension`, `sub_extensions`) |
| 24 | +9. Convert user data into `MappingRequest` objects and cache them |
| 25 | + |
| 26 | +**Error Handling** |
| 27 | +- On any failure (login error, missing claim, invalid JWT, chat endpoint error), returns authentication error |
| 28 | +- Does NOT save the push token or create a mapping on failure |
| 29 | +- Successful authentications are cached in-memory for `CACHE_TTL_SECONDS` seconds to reduce external service load |
15 | 30 |
|
16 | 31 | If any request is missing a `password`, it fails with authentication error. |
17 | 32 |
|
18 | 33 | ### Environment variables related to auth |
19 | 34 |
|
20 | | -- `EXT_AUTH_URL`: external HTTP endpoint used to validate extension+password for push token reports (eg: `https://voice.nethserver.org/freepbx/rest/testextauth`) |
| 35 | +- `EXT_AUTH_URL`: Base URL of the external authentication service (eg: `https://cti.nethserver.org/`). The proxy automatically appends `/api/login` and `/api/chat?users=1` to this URL. |
21 | 36 | - `EXT_AUTH_TIMEOUT_S`: timeout in seconds for calls to `EXT_AUTH_URL` (default: `5`) |
22 | 37 | - `CACHE_TTL_SECONDS`: cache TTL for external auth responses (default: `3600` seconds) |
0 commit comments