Skip to content

Commit afac7ba

Browse files
committed
document new functions
1 parent 7842447 commit afac7ba

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

examples/official-site/sqlpage/migrations/07_authentication.sql

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ INSERT INTO parameter (
2323
VALUES (
2424
'authentication',
2525
'link',
26-
'The URL to redirect the user to if they are not logged in.',
26+
'The URL to redirect the user to if they are not logged in. If this parameter is not specified, the user will stay on the current page, but be asked to log in using a popup in their browser (HTTP basic authentication).',
2727
'TEXT',
2828
TRUE,
2929
TRUE
@@ -50,11 +50,28 @@ INSERT INTO example (component, description)
5050
VALUES (
5151
'authentication',
5252
'
53+
54+
### Usage with HTTP basic authentication
55+
56+
The most basic usage of the authentication component is to let SQLPage handle the authentication through HTTP basic authentication.
57+
This is the simplest way to password-protect a page, but it is not very user-friendly, because the browser will show an unstyled popup asking for the username and password.
58+
The username and password entered by the user will be accessible in your SQL code using the
59+
[`sqlpage.basic_auth_username()`](functions.sql?function=basic_auth_username) and
60+
[`sqlpage.basic_auth_password()`](functions.sql?function=basic_auth_password) functions.
61+
62+
```sql
63+
SELECT ''authentication'' AS component,
64+
''$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw'' AS password_hash, -- generated using https://argon2.online/
65+
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
66+
```
67+
68+
### Usage with a login form
69+
5370
The most basic usage of the authentication component is to simply check if the user has sent the correct password, and if not, redirect them to a login page:
5471
5572
```sql
5673
SELECT ''authentication'' AS component,
57-
''/login'' AS link,
74+
''login.sql'' AS link,
5875
''$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw'' AS password_hash, -- generated using https://argon2.online/
5976
:password AS password; -- this is the password that the user sent through our form
6077
```

examples/official-site/sqlpage/migrations/08_functions.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,36 @@ VALUES (
7171
'name',
7272
'The name of the HTTP header to read.',
7373
'TEXT'
74+
);
75+
INSERT INTO sqlpage_functions ("name", "icon", "description_md")
76+
VALUES (
77+
'basic_auth_username',
78+
'user',
79+
'Returns the username from the [Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) header of the request.
80+
If the header is not present, this function raises an authorization error that will prompt the user to enter their credentials.
81+
82+
### Example
83+
84+
```sql
85+
SELECT ''authentication'' AS component,
86+
(SELECT password_hash from users where name = sqlpage.basic_auth_username()) AS password_hash,
87+
sqlpage.basic_auth_password() AS password;
88+
```
89+
90+
'
91+
),
92+
(
93+
'basic_auth_password',
94+
'key',
95+
'Returns the password from the [Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) header of the request.
96+
If the header is not present, this function raises an authorization error that will prompt the user to enter their credentials.
97+
98+
### Example
99+
100+
```sql
101+
SELECT ''authentication'' AS component,
102+
(SELECT password_hash from users where name = sqlpage.basic_auth_username()) AS password_hash,
103+
sqlpage.basic_auth_password() AS password;
104+
```
105+
'
74106
);

src/webserver/database/sql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn func_call_to_param(func_name: &str, arguments: &mut [FunctionArg]) -> StmtPar
161161
.map_or_else(StmtParam::Error, StmtParam::Cookie),
162162
"header" => extract_single_quoted_string("header", arguments)
163163
.map_or_else(StmtParam::Error, StmtParam::Header),
164-
"basic_auth_user" => StmtParam::BasicAuthUsername,
164+
"basic_auth_username" => StmtParam::BasicAuthUsername,
165165
"basic_auth_password" => StmtParam::BasicAuthPassword,
166166
"hash_password" => extract_variable_argument("hash_password", arguments)
167167
.map(Box::new)

0 commit comments

Comments
 (0)