You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Description
Add a whoami() function to proxy.mjs. It can be used to access the
claims from the user's identity token.
### Type of change
* [x] New feature
* [ ] Feature improvement
* [ ] Bug fix
* [x] Documentation
* [ ] Cleanup / refactoring
* [ ] Other (please explain)
### How is this change tested ?
* [ ] Unit tests
* [x] Manual tests (explain)
* [ ] Tests are not needed
---------
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This JavaScript module provides client-side functionality for HTML pages served by `tlsproxy`. It handles CSRF protection, session management, and internationalization (i18n) dynamically in the user's browser.
4
+
5
+
## Features
6
+
7
+
### 1. Automatic CSRF Protection
8
+
9
+
The script automatically protects against Cross-Site Request Forgery (CSRF) attacks.
10
+
11
+
- It wraps the standard `window.fetch` function.
12
+
- Before any `fetch` request is sent, it reads the session ID from the `__tlsproxySid` cookie.
13
+
- It then adds the session ID to the request headers as `x-csrf-token`.
14
+
15
+
This process is automatic. Any page that includes this module will have its `fetch` requests protected.
16
+
17
+
### 2. Session Management
18
+
19
+
The module exports functions to manage the user's authentication session.
20
+
21
+
-**`logout()`**:
22
+
- Sends a `POST` request to the `/.sso/logout` endpoint to terminate the session on the backend.
23
+
- Upon success, it redirects the user to the logout page.
24
+
25
+
-**`whoami()`**:
26
+
- Sends a `POST` request to the `/.sso/` endpoint.
27
+
- Returns a promise that resolves with a JSON object containing information about the currently authenticated user.
28
+
29
+
### 3. Internationalization (i18n)
30
+
31
+
The script provides dynamic, client-side translation of web pages.
32
+
33
+
-**Language Detection**: On page load, it detects the user's preferred languages from `navigator.languages`.
34
+
-**Translation Loading**: It fetches the appropriate language file from `/.sso/languages.json` based on the detected language. It has a fallback mechanism to find the best-matching language or default to English (`en`).
35
+
-**Dynamic Translation**:
36
+
- It scans the document for any HTML elements that have a `tkey` attribute.
37
+
- It replaces the content (or placeholder text for inputs) of these elements with the translated string corresponding to the `tkey` value.
38
+
- It sets the `lang` and `dir` (text direction, e.g., `ltr` or `rtl`) attributes on the `<html>` tag.
39
+
-**Language Selector**:
40
+
- If a language selector element (with `id="lang-selector"`) does not already exist on the page, the script dynamically creates and appends one.
41
+
- This `<select>` element allows the user to switch languages on the fly. It is populated with all available languages from the backend.
42
+
43
+
## Usage
44
+
45
+
This script is intended to be included as a module in HTML pages served by `tlsproxy`, such as the login, logout, or SSO status pages.
0 commit comments