Skip to content

Commit 62504af

Browse files
committed
Add HTTP Basic Auth in fetch
1 parent c13a0f8 commit 62504af

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG.md
22

3+
## 0.33.0 (unreleased)
4+
5+
- Add support for HTTP Basic Authentication in the [fetch](https://sql-page.com/documentation.sql?component=fetch#component) function.
6+
37
## 0.32.1 (2025-01-03)
48

59
This is a bugfix release.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlpage"
3-
version = "0.32.1"
3+
version = "0.33.0"
44
edition = "2021"
55
description = "Build data user interfaces entirely in SQL. A web server that takes .sql files and formats the query result using pre-made configurable professional-looking components."
66
keywords = ["web", "sql", "framework"]

examples/official-site/sqlpage/migrations/40_fetch.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ select $user_search as title,
2929
CAST($api_results->>0->>''lat'' AS FLOAT) as latitude,
3030
CAST($api_results->>0->>''lon'' AS FLOAT) as longitude;
3131
```
32+
3233
#### POST query with a body
3334
3435
In this example, we use the complex form of the function to make an
@@ -59,6 +60,23 @@ select
5960
$api_results as contents;
6061
```
6162
63+
64+
#### Authenticated request using Basic Auth
65+
66+
Here''s how to make a request to an API that requires [HTTP Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication):
67+
68+
```sql
69+
set request = json_object(
70+
''url'', ''https://api.example.com/data'',
71+
''username'', ''my_username'',
72+
''password'', ''my_password''
73+
);
74+
set api_results = sqlpage.fetch($request);
75+
```
76+
77+
> This will add the `Authorization: Basic bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQK` header to the request,
78+
> where `bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQK` is the base64 encoding of the string `my_username:my_password`.
79+
6280
# JSON parameter format
6381
6482
The fetch function accepts either a URL string, or a JSON object with the following parameters:
@@ -67,6 +85,8 @@ The fetch function accepts either a URL string, or a JSON object with the follow
6785
- `headers`: A JSON object with the headers to send.
6886
- `body`: The body of the request. If it is a JSON object, it will be sent as JSON. If it is a string, it will be sent as is.
6987
- `timeout_ms`: The maximum time to wait for the request, in milliseconds. Defaults to 5000.
88+
- `username`: Username for HTTP Basic Authentication. Introduced in version 0.33.0.
89+
- `password`: Password for HTTP Basic Authentication. Only used if username is provided. Introduced in version 0.33.0.
7090
7191
'
7292
);

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ async fn fetch(
151151
for (k, v) in http_request.headers {
152152
req = req.insert_header((k.as_ref(), v.as_ref()));
153153
}
154+
if let Some(username) = http_request.username {
155+
let password = http_request.password.unwrap_or_default();
156+
req = req.basic_auth(username, password);
157+
}
154158
log::info!("Fetching {}", http_request.url);
155159
let mut response = if let Some(body) = http_request.body {
156160
let val = body.get();

src/webserver/database/sqlpage_functions/http_fetch_request.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub(super) struct HttpFetchRequest<'b> {
1414
pub timeout_ms: Option<u64>,
1515
#[serde(borrow, deserialize_with = "deserialize_map_to_vec_pairs")]
1616
pub headers: HeaderVec<'b>,
17+
pub username: Option<Cow<'b, str>>,
18+
pub password: Option<Cow<'b, str>>,
1719
#[serde(borrow)]
1820
pub body: Option<Cow<'b, serde_json::value::RawValue>>,
1921
}
@@ -52,6 +54,8 @@ impl<'a> BorrowFromStr<'a> for HttpFetchRequest<'a> {
5254
url: s,
5355
method: None,
5456
headers: Vec::new(),
57+
username: None,
58+
password: None,
5559
body: None,
5660
timeout_ms: None,
5761
}
@@ -78,6 +82,8 @@ impl HttpFetchRequest<'_> {
7882
.collect(),
7983
body: self.body.map(Cow::into_owned).map(Cow::Owned),
8084
timeout_ms: self.timeout_ms,
85+
username: self.username.map(Cow::into_owned).map(Cow::Owned),
86+
password: self.password.map(Cow::into_owned).map(Cow::Owned),
8187
}
8288
}
8389
}

0 commit comments

Comments
 (0)