Skip to content

Commit dc1ebb0

Browse files
committed
Make sign-in component documentation editable as Markdown
1 parent 96b4763 commit dc1ebb0

9 files changed

Lines changed: 293 additions & 272 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
icon: lock
3+
since: 0.7.2
4+
category: server-logic
5+
summary: Restrict access by checking a password against a stored hash.
6+
top:
7+
- name: link
8+
type: TEXT
9+
description: The URL to redirect the user to if they are not logged in. If this parameter is not specified,
10+
the user will stay on the current page, but be asked to log in using a popup in their browser (HTTP basic
11+
authentication).
12+
- name: password
13+
type: TEXT
14+
description: The password that was sent by the user. You can set this to :password if you have a login
15+
form leading to your page.
16+
- name: password_hash
17+
type: TEXT
18+
description: The hash of the password that you stored for the user that is currently trying to log in.
19+
These hashes can be generated ahead of time using a tool like https://argon2.online/.
20+
---
21+
22+
Create pages with password-restricted access.
23+
24+
25+
When you want to add user authentication to your SQLPage application,
26+
you have two main options:
27+
28+
1. The `authentication` component:
29+
- lets you manage usernames and passwords yourself
30+
- does not require any external service
31+
- gives you fine-grained control over
32+
- which pages and actions are protected
33+
- the look of the [login form](?component=login)
34+
- the duration of the session
35+
- the permissions of each user
36+
2. [**Single sign-on**](/sso)
37+
- lets users log in with their existing accounts (like Google, Microsoft, or your organization's own identity provider)
38+
- requires setting up an external service (Google, Microsoft, etc.)
39+
- frees you from implementing a lot of features like password reset, account creation, user management, etc.
40+
41+
This page describes the first option.
42+
43+
When used, this component has to be at the top of your page,
44+
because once the page has begun being sent to the browser,
45+
it is too late to restrict access to it.
46+
47+
The authentication component checks if the user has sent the correct password,
48+
and if not, redirects them to the URL specified in the link parameter.
49+
50+
If you don't want to re-check the password on every page (which is an expensive operation),
51+
you can check the password only once and store a session token in your database
52+
(see the session example below).
53+
54+
You can use the [cookie component](?component=cookie) to set the session token cookie in the client browser,
55+
and then check whether the token matches what you stored in subsequent pages.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
### Usage with HTTP basic authentication
2+
3+
The most basic usage of the authentication component is with the
4+
[`sqlpage.basic_auth_username()`](functions.sql?function=basic_auth_username#function) and
5+
[`sqlpage.basic_auth_password()`](functions.sql?function=basic_auth_password#function) functions.
6+
The component will check if the provided password matches the stored [password hash](/examples/hash_password.sql),
7+
and if not, it will prompt the user to enter a password in a browser popup:
8+
9+
```sql
10+
SELECT 'authentication' AS component,
11+
'$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w' AS password_hash, -- this is a hash of the password 'password'
12+
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
13+
```
14+
15+
You can [generate a password hash using the `hash_password` function](/examples/hash_password.sql).
16+
17+
If you want to have multiple users with different passwords,
18+
you could store them with their password hashes in the database,
19+
or just hardcode them use a `CASE` statement:
20+
21+
```sql
22+
SELECT 'authentication' AS component,
23+
case sqlpage.basic_auth_username()
24+
when 'admin'
25+
then '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w' -- the password is 'password'
26+
when 'user'
27+
then '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$qsrWdjgl96ooYw' -- the password is 'user'
28+
end AS password_hash, -- this is a hash of the password 'password'
29+
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
30+
```
31+
32+
Try this example online: [SQL Basic Auth](/examples/authentication/basic_auth.sql).
33+
34+
### Advanced user session management
35+
36+
*Basic auth* is the simplest way to password-protect a page,
37+
but it is not very flexible nor user-friendly,
38+
because the browser will show an unstyled popup asking for the username and password.
39+
40+
For more advanced authentication, you can store user information and user sessions in your database.
41+
You can then use the [`form`](/component.sql?component=form#component) component to create a custom login form.
42+
When the user submits the form, you check if the password is correct using the `authentication` component.
43+
You then store a unique string of numbers and letters (a session token) both in the user's browser
44+
using the [`cookie`](/component.sql?component=cookie#component) component and in your database.
45+
Then, in all the pages that require authentication, you check if the cookie is present and matches the session token in your database.
46+
47+
You can check if the user has sent the correct password in a form, and if not, redirect them to a login page.
48+
49+
Create a login form in a file called `login.sql` that uses the [login component](?component=login):
50+
51+
```sql
52+
select 'login' as component;
53+
```
54+
55+
And then, in `create_session_token.sql` :
56+
57+
```sql
58+
SELECT 'authentication' AS component,
59+
'login.sql' AS link,
60+
'$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw' AS password_hash, -- generated using sqlpage.hash_password
61+
:password AS password; -- this is the password that the user sent through our form
62+
63+
-- The code after this point is only executed if the user has sent the correct password
64+
65+
```
66+
67+
and in `login.sql` :
68+
69+
```sql
70+
SELECT 'form' AS component, 'Login' AS title, 'my_protected_page.sql' AS action;
71+
SELECT 'password' AS type, 'password' AS name, 'Password' AS label;
72+
```
73+
74+
### Advanced: usage with a session token
75+
76+
Calling the `authentication` component is expensive.
77+
The password hashing algorithm is designed to be slow, so that it is difficult to brute-force the password,
78+
even if an attacker gets access to the database.
79+
80+
If you want to avoid calling the `authentication` component on every page, you can use a session token.
81+
A session token is a random string that is generated when the user logs in, and stored in the database.
82+
It has a limited lifetime, and is stored in a cookie in the user's browser.
83+
When the user visits a page, the session token is sent to the server, and the server checks if it is valid.
84+
85+
```sql
86+
SELECT 'authentication' AS component,
87+
'login.sql' AS link,
88+
(SELECT password_hash FROM user WHERE username = :username) AS password_hash,
89+
:password AS password;
90+
91+
-- The code after this point is only executed if the user has sent the correct password
92+
93+
-- Generate a random session token
94+
INSERT INTO session (id, username)
95+
VALUES (sqlpage.random_string(32), :username)
96+
RETURNING
97+
'cookie' AS component,
98+
'session_token' AS name,
99+
id AS value;
100+
```
101+
102+
### Single sign-on with OIDC (OpenID Connect)
103+
104+
If you don't want to manage your own user database,
105+
you can [use OpenID Connect and OAuth2](/sso) to authenticate users.
106+
This allows users to log in with their Google, Microsoft, or internal company account.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
icon: password-user
3+
since: 0.39.0
4+
category: forms-and-feedback
5+
summary: Display a sign-in form for a username and password.
6+
top:
7+
- name: action
8+
type: TEXT
9+
description: 'An optional link to a target page that will handle the results of the form. '
10+
- name: autofocus
11+
type: BOOLEAN
12+
description: Focus the username field when the page opens. Defaults to true. Set to false when the form
13+
is one part of a larger page.
14+
- class
15+
- name: enctype
16+
type: TEXT
17+
description: Form data encoding.
18+
- name: error_message
19+
type: TEXT
20+
description: An error message to display above the form, typically shown after a failed login attempt.
21+
- name: error_message_md
22+
type: TEXT
23+
description: A markdown error message to display above the form, typically shown after a failed login attempt.
24+
- name: footer
25+
type: TEXT
26+
description: A text placed at the bottom of the authentication form. If both footer and footer_md are specified,
27+
footer takes precedence.
28+
- name: footer_md
29+
type: TEXT
30+
description: A markdown text placed at the bottom of the authentication form. Useful for creating links
31+
to other pages (creating a new account, contacting technical support, etc.).
32+
- name: forgot_password_link
33+
type: TEXT
34+
description: The link to the page allowing the user to reset their password.
35+
- name: forgot_password_text
36+
type: TEXT
37+
description: A text for the link allowing the user to reset their password. If the text is empty, the link
38+
is not displayed.
39+
- id
40+
- name: image
41+
type: URL
42+
description: The URL of an centered image displayed before the title.
43+
- name: image_alt
44+
type: TEXT
45+
description: Alternative text for an informative login image. Leave empty if decorative.
46+
- name: password
47+
type: TEXT
48+
required: true
49+
description: Label and placeholder for the password field.
50+
- name: password_icon
51+
type: ICON
52+
description: Icon to display on the left side of the input field, on the same line.
53+
- name: remember_me_text
54+
type: TEXT
55+
description: A text for the option allowing the user to request the preservation of their identity. If
56+
the text is empty, the option is not displayed.
57+
- name: title
58+
type: TEXT
59+
description: Title of the authentication form.
60+
- name: username
61+
type: TEXT
62+
required: true
63+
description: Label and placeholder for the user account identifier text field.
64+
- name: username_icon
65+
type: ICON
66+
description: Icon to display on the left side of the input field, on the same line.
67+
- name: validate
68+
type: TEXT
69+
description: The text to display in the button at the bottom of the form that submits the values.
70+
- name: validate_color
71+
type: COLOR
72+
description: The color of the button at the bottom of the form that submits the values. Omit this property
73+
to use the default color.
74+
- name: validate_outline
75+
type: COLOR
76+
description: A color to outline the validation button.
77+
- name: validate_shape
78+
type: TEXT
79+
description: The shape of the validation button.
80+
- name: validate_size
81+
type: TEXT
82+
description: The size of the validation button.
83+
---
84+
85+
The login component is an authentication form with numerous customization options.
86+
It offers the main functionalities for this type of form.
87+
The user can enter their username and password.
88+
There are many optional attributes such as the use of icons on input fields, the insertion of a link to a page to reset the password, an option for the application to maintain the user's identity via a cookie.
89+
It is also possible to set the title of the form, display the company logo, or customize the appearance of the form submission button.
90+
91+
This component should be used in conjunction with other components such as [authentication](component.sql?component=authentication) and [cookie](component.sql?component=cookie).
92+
It does not implement any logic and simply collects the username and password to pass them to the code responsible for authentication.
93+
94+
A few things to know :
95+
- The form uses the POST method to transmit information to the destination page,
96+
- The user's username and password are entered into fields with the names `username` and `password`,
97+
- To obtain the values of username and password, you must use the variables `:username` and `:password`,
98+
- When you set the `remember_me_text` property, the variable `:remember` becomes available after form submission to check if the user checked the "remember me" checkbox.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
rows:
3+
- component: login
4+
action: /examples/show_variables
5+
image: ../assets/icon.webp
6+
title: Please login to your account
7+
username: Username
8+
password: Password
9+
autofocus: false
10+
username_icon: user
11+
password_icon: lock
12+
forgot_password_text: Forgot your password?
13+
forgot_password_link: reset_password.sql
14+
remember_me_text: Remember me
15+
footer_md: Don't have an account? [Register here](register.sql)
16+
validate: Sign in
17+
---
18+
19+
Using the main options of the login component
20+
21+
When the user clicks the "Sign in" button, the form is submitted to the `/examples/show_variables.sql` page.
22+
There, you will have access to the variables:
23+
- `:username`: the username entered by the user
24+
- `:password`: the password entered by the user
25+
- `:remember`: the string "on" if the checkbox was checked, or NULL if it was not checked
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
rows:
3+
- component: login
4+
username: Username
5+
password: Password
6+
autofocus: false
7+
---
8+
9+
Basic login form

0 commit comments

Comments
 (0)