|
| 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. |
0 commit comments