Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- run: cargo fmt --all -- --check
- run: cargo clippy --all-targets --all-features -- -D warnings
- name: Prepare and check official website
run: npm run site:test
run: npm run site:test -- --features odbc-static
- name: Upload prepared website
uses: actions/upload-artifact@v7
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- SQLite query results preserve the full 64-bit range of `INTEGER` values. ([#1636](https://github.com/sqlpage/SQLPage/issues/1636))
- Big number title links have a larger target for touch and pointer users.
- Carousel slide indicators and pagination arrows now have accessible names. Keyboard users can activate carousel arrows and enabled pagination links. Screen readers announce the current page.
- The carousel's `delay` parameter controls the interval between automatically changing slides.
- Button, card, carousel, and login images accept alternative text. Embedded card frames use their own title or the card title.
- Inline links are underlined so they stand out without relying on color. The documentation search button has a clear accessible name separate from its shortcut hint.
- Disabled buttons expose their state to assistive technology and cannot be activated.
Expand Down
7 changes: 3 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ built executable. Component suites use `fixtures/<suite>/{index.sql,test.ts}` an
import the shared `fixture.ts` harness; root-level `*.spec.ts` files cover the
official site.

For changes to component documentation, run `npm run site:test`. It rebuilds
the site and checks the prepared reference against public components and their
template properties.
Run `npm run site:test` after changing component documentation. It builds the
site, verifies its published examples and search index, and tests the parser.

Run `npx playwright test --grep @accessibility` from `tests/end-to-end` to audit
the official site's component documentation and its live examples in SQLPage's
Expand Down Expand Up @@ -113,7 +112,7 @@ Architectural constraints and documentation maintenance rules are in
[AGENTS.md](./AGENTS.md).

Edit existing documentation in the [official site](./examples/official-site/) in place.
Deployment recreates the website's database from the site's SQL migrations.
Deployment uses the SQLite database prepared by the site build.

Run `npm run site` to preview changes using the same site build as deployment.

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ include = [
"/frontend/dist",
]

[[example]]
name = "build-site"
path = "scripts/official-site/main.rs"

[lints.rust]
elided_lifetimes_in_paths = "deny"
unreachable_pub = "deny"
Expand Down Expand Up @@ -147,6 +151,7 @@ lambda-web = [
]

[dev-dependencies]
walkdir = "2"
actix-http = "3"
tempfile = "3"
tokio = { version = "1", features = ["rt", "time", "test-util"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/official-site/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM lovasoa/sqlpage:main

COPY --chown=sqlpage:sqlpage ./sqlpage /etc/sqlpage
ENV SQLPAGE_CONFIGURATION_DIRECTORY=/var/www/sqlpage
COPY --chown=sqlpage:sqlpage . /var/www
46 changes: 44 additions & 2 deletions examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The SQLPage website is of course built with SQLPage !

Things that you may be interested to look at:
- The [custom component we use for our stylish home page](./sqlpage/templates/landing-page.handlebars)
- The [migrations](./sqlpage/migrations) that populate the database with the documentation for all components
- How to [document a component and add examples](#component-reference)
- The [advanced multistep form example](./examples/multistep-form/)

It is hosted as a simple Docker container on [sql-page.com](https://sql-page.com).
Expand All @@ -20,12 +20,54 @@ After `npm run build`, run `npm run site:build` to prepare the site under
`target/official-site/`.
Run SQLPage from that directory, or run
`docker compose -f examples/official-site/docker-compose.yml up --build` from
the repository root.
the repository root. Demo login sessions survive a server restart and reset when the site is rebuilt.

Add `theme=light` or `theme=dark` to a component reference URL to preview its
live examples using SQLPage's stock theme. For example, visit
`/component.sql?component=form&theme=light`.

## Component reference

Document Handlebars component parameters in the first comment of `sqlpage/templates/<component>.handlebars`. Start with a short summary paragraph, then give the complete overview, followed by `## Top-level parameters` and `## Row-level parameters`. Omit empty sections. For the nine components implemented in Rust, put the overview and parameter list in that component's `README.md` under `reference/components/` using the same structure.

```handlebars
{{!--
Display source code with syntax highlighting.

The code component renders text as escaped, highlighted source.

## Top-level parameters

- **id** (TEXT)
- **class** (TEXT)

## Row-level parameters

- **contents** (TEXT, required): A block of source code.
- **language** (TEXT): Language used for syntax highlighting.
--}}
```

Parameters are optional by default. Add `, required` after the type for a required parameter. The standard `id` and `class` parameters inherit their HTML descriptions. Give them a description when their behavior differs.

Put each component's examples in its directory under `reference/components/`. Set its icon, category, and the version that introduced it in `sqlpage/migrations/82_reference_catalog.sql`.

Name each example with a zero-padded number and a short, hyphenated title. Keep its number when editing so links such as `#example-2` remain stable. A runnable example's file path is also its direct URL.

- **[`code/01-display-html-source.sql`](reference/components/code/01-display-html-source.sql)** runs inline. Start with a block comment containing a title and optional Markdown. The SQL after the comment supplies the copyable source, preview, and direct URL.
- **[`shell/01-dark-shell-with-navigation-menus.standalone.sql`](reference/components/shell/01-dark-shell-with-navigation-menus.standalone.sql)** runs only when its direct URL is opened. Use this form when the example controls the page shell or HTTP response.
- **[`cookie/01-set-and-read-a-cookie.md`](reference/components/cookie/01-set-and-read-a-cookie.md)** displays a walkthrough. Start with a `# Title` and use fenced code blocks for SQL that needs an application database, changes state, or spans multiple files.

```sql
/*
Show HTML source
*/
select 'code' as component;
select 'html' as language, '<h1>Hello</h1>' as contents;
```

Run `npm run site:test` to build the site, check its search index and published SQL, and run the documentation parser tests. Run `npm run site` to preview the site.

## Landing page

The home page is rendered by `index.sql` and the `landing-page` custom template.
Expand Down
5 changes: 2 additions & 3 deletions examples/official-site/colors.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
set theme = coalesce($theme, 'custom');

select 'dynamic' as component, json_patch(json_extract(properties, '$[0]'), json_object(
select 'dynamic' as component, json_patch(sqlpage.run_sql('reference/components/shell/01-dark-shell-with-navigation-menus.standalone.sql')->0, json_object(
'title', $theme || ' SQLPage Colors',
'css', case $theme when 'custom' then '/assets/highlightjs-and-tabler-theme.css' end,
'theme', case $theme when 'default' then 'light' else 'dark' end
)) as properties
FROM example WHERE component = 'shell' LIMIT 1;
)) as properties;

select 'tab' as component;
select 'Default theme' as title, '?theme=default' as link, 'Default theme' as description, case $theme when 'default' then 'primary' end as color, $theme = 'default' as disabled;
Expand Down
23 changes: 6 additions & 17 deletions examples/official-site/component.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,14 @@ select 'dynamic' as component, sqlpage.run_sql('sqlpage/docs-reference.sql', jso
select 'title' as component, 2 as level, 'Examples' as contents, 'examples' as id
where exists (select 1 from example where component = $component);

with examples as (
-- HTTP headers and shells can only run at the start of a response.
select *,
case when component not in ('shell', 'http_header') then properties end as result,
case when length(trim(description)) <= 120
and glob('*[^a-zA-Z0-9 ,.''()-]*', trim(description)) = 0
then trim(description) end as short_title
from example_with_source where component = $component
)
select 'dynamic' as component, json_array(
json_object(
'component', 'docs-example', 'number', example_number,
'title', short_title, 'description_md', case when short_title is null then description end,
'source', source,
'preview', result is not null
'component', 'docs-example', 'number', number, 'title', title,
'description_md', description, 'source', source,
'preview', preview
),
json(coalesce(result, '[]')),
-- Keep every native component of the example inside the same result panel.
case when result is not null then json_object('component', 'docs-example', 'end_result', true)
coalesce(sqlpage.run_sql(case when preview then path end), '[]'),
case when preview then json_object('component', 'docs-example', 'end_result', true)
else json_array() end
) as properties
from examples order by example_id;
from example where component = $component order by number;
3 changes: 1 addition & 2 deletions examples/official-site/examples/authentication/index.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ set user_role = (select role from users natural join user_sessions where session
select 'redirect' as component, 'login.sql' as link where $user_role is null;

select 'dynamic' as component,
json_insert(properties, '$[0].menu_item[#]', 'logout') as properties
FROM example WHERE component = 'shell' LIMIT 1;
json_insert(sqlpage.run_sql('reference/components/shell/01-dark-shell-with-navigation-menus.standalone.sql'), '$[0].menu_item[#]', 'logout') as properties;

select 'alert' as component, 'info' as color, CONCAT('You are logged in as ', $user_role) as title;

Expand Down
5 changes: 2 additions & 3 deletions examples/official-site/examples/layouts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ set layout = coalesce($layout, 'boxed');
set sidebar = coalesce($sidebar, 0);

select 'dynamic' as component,
json_patch(properties->0,
json_patch(sqlpage.run_sql('reference/components/shell/01-dark-shell-with-navigation-menus.standalone.sql')->0,
json_object(
'title', 'Shell layouts | SQLPage',
'layout', $layout,
'sidebar', $sidebar = 1
)
) as properties
FROM example WHERE component = 'shell' LIMIT 1;
) as properties;

select 'text' as component, '
# Layouts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
A basic alert message
*/
select
'alert' as "component",
'Attention' as "title",
'This is an important message.' as "description";
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
A list of notifications
*/
select
'alert' as "component",
'Success' as "title",
'Item successfully added to your cart.' as "description",
'check' as "icon",
'green' as "color";

select
'alert' as "component",
'Warning' as "title",
'Your cart is almost full.' as "description",
'alert-triangle' as "icon",
'yellow' as "color";

select
'alert' as "component",
'Error' as "title",
'Your cart is full.' as "description",
'alert-circle' as "icon",
'red' as "color";
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
A full-featured notification message with multiple links
*/
select
'alert' as "component",
'Your dashboard is ready!' as "title",
'analyze' as "icon",
'teal' as "color",
TRUE as "dismissible",
'Your public web dashboard was successfully created.' as "description";

select
'/examples/chart.sql' as "link",
'View your dashboard' as "title";

select
'/' as "link",
'Back to home' as "title",
'secondary' as "color";
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
A dismissible alert

`close_label` gives the close button an accessible name.
*/
select
'alert' as "component",
'Alert' as "title",
'alert-circle' as "icon",
'red' as "color",
TRUE as "important",
TRUE as "dismissible",
'Dismiss this alert' as "close_label",
'SQLPage is entirely free and open source.' as "description",
'https://github.com/sqlpage/SQLPage' as "link",
'See source code' as "link_text";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
An alert message with a Markdown-formatted description
*/
select
'alert' as "component",
'Free and open source' as "title",
'free-rights' as "icon",
'info' as "color",
'*SQLPage* is entirely free and open source. You can **contribute** to it on [GitHub](https://github.com/sqlpage/SQLPage).' as "description_md";
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# HTTP basic authentication and sessions

### Usage with HTTP basic authentication

The most basic usage of the authentication component is with the
[`sqlpage.basic_auth_username()`](/functions.sql?function=basic_auth_username#function) and
[`sqlpage.basic_auth_password()`](/functions.sql?function=basic_auth_password#function) functions.
The component will check if the provided password matches the stored [password hash](/examples/hash_password.sql),
and if not, it will prompt the user to enter a password in a browser popup:

```sql
SELECT 'authentication' AS component,
'$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w' AS password_hash, -- this is a hash of the password 'password'
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
```

You can [generate a password hash using the `hash_password` function](/examples/hash_password.sql).

If you want to have multiple users with different passwords,
you could store them with their password hashes in the database,
or just hardcode them use a `CASE` statement:

```sql
SELECT 'authentication' AS component,
case sqlpage.basic_auth_username()
when 'admin'
then '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w' -- the password is 'password'
when 'user'
then '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$qsrWdjgl96ooYw' -- the password is 'user'
end AS password_hash, -- this is a hash of the password 'password'
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
```

Try this example online: [SQL Basic Auth](/examples/authentication/basic_auth.sql).

### Advanced user session management

*Basic auth* is the simplest way to password-protect a page,
but it is not very flexible nor user-friendly,
because the browser will show an unstyled popup asking for the username and password.

For more advanced authentication, you can store user information and user sessions in your database.
You can then use the [`form`](/component.sql?component=form#component) component to create a custom login form.
When the user submits the form, you check if the password is correct using the `authentication` component.
You then store a unique string of numbers and letters (a session token) both in the user's browser
using the [`cookie`](/component.sql?component=cookie#component) component and in your database.
Then, in all the pages that require authentication, you check if the cookie is present and matches the session token in your database.

You can check if the user has sent the correct password in a form, and if not, redirect them to a login page.

Create a login form in a file called `login.sql` that uses the [login component](/component.sql?component=login):

```sql
select 'login' as component, 'create_session_token.sql' as action;
```

And then, in `create_session_token.sql` :

```sql
SELECT 'authentication' AS component,
'login.sql' AS link,
'$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw' AS password_hash, -- generated using sqlpage.hash_password
:password AS password; -- this is the password that the user sent through our form

-- The code after this point is only executed if the user has sent the correct password

```

### Advanced: usage with a session token

Calling the `authentication` component is expensive.
The password hashing algorithm is designed to be slow, so that it is difficult to brute-force the password,
even if an attacker gets access to the database.

If you want to avoid calling the `authentication` component on every page, you can use a session token.
A session token is a random string that is generated when the user logs in, and stored in the database.
It has a limited lifetime, and is stored in a cookie in the user's browser.
When the user visits a page, the session token is sent to the server, and the server checks if it is valid.

```sql
SELECT 'authentication' AS component,
'login.sql' AS link,
(SELECT password_hash FROM user WHERE username = :username) AS password_hash,
:password AS password;

-- The code after this point is only executed if the user has sent the correct password

-- Generate a random session token
INSERT INTO session (id, username)
VALUES (sqlpage.random_string(32), :username)
RETURNING
'cookie' AS component,
'session_token' AS name,
id AS value;
```

### Single sign-on with OIDC (OpenID Connect)

If you don't want to manage your own user database,
you can [use OpenID Connect and OAuth2](/sso) to authenticate users.
This allows users to log in with their Google, Microsoft, or internal company account.
Loading
Loading