feat: db: get_site_from_secret for API authentication
This commit is contained in:
parent
76692109bc
commit
ec7d698252
2 changed files with 107 additions and 0 deletions
|
@ -1,5 +1,43 @@
|
|||
{
|
||||
"db": "PostgreSQL",
|
||||
"1ac91b492001493430c686d9cd7d6be03ada4b4c431d7bc112ef2105eba0e82d": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "repo_url",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "branch",
|
||||
"ordinal": 1,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "hostname",
|
||||
"ordinal": 2,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "owned_by",
|
||||
"ordinal": 3,
|
||||
"type_info": "Int4"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT repo_url, branch, hostname, owned_by\n FROM librepages_sites\n WHERE site_secret = $1\n "
|
||||
},
|
||||
"1be33ea4fe0e6079c88768ff912b824f4b0250193f2d086046c1fd0da125ae0c": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
|
@ -52,6 +90,26 @@
|
|||
},
|
||||
"query": "DELETE FROM librepages_sites\n WHERE hostname = ($1)\n AND owned_by = ( SELECT ID FROM librepages_users WHERE name = $2);\n "
|
||||
},
|
||||
"53f3c21c06c8d1c218537dfa9183fd0604aaf28200d8aa12e97db4ac317df39e": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int4"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT name FROM librepages_users WHERE ID = $1"
|
||||
},
|
||||
"5c5d774bde06c0ab83c3616a56a28f12dfd9c546cbaac9f246d3b350c587823e": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
|
|
49
src/db.rs
49
src/db.rs
|
@ -270,6 +270,49 @@ impl Database {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_site_from_secret(&self, site_secret: &str) -> ServiceResult<Site> {
|
||||
struct S {
|
||||
repo_url: String,
|
||||
branch: String,
|
||||
hostname: String,
|
||||
owned_by: i32,
|
||||
}
|
||||
|
||||
let site = sqlx::query_as!(
|
||||
S,
|
||||
"SELECT repo_url, branch, hostname, owned_by
|
||||
FROM librepages_sites
|
||||
WHERE site_secret = $1
|
||||
",
|
||||
site_secret,
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
||||
|
||||
struct Owner {
|
||||
name: String,
|
||||
}
|
||||
let owner = sqlx::query_as!(
|
||||
Owner,
|
||||
"SELECT name FROM librepages_users WHERE ID = $1",
|
||||
site.owned_by
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
||||
|
||||
let site = Site {
|
||||
site_secret: site_secret.to_owned(),
|
||||
branch: site.branch,
|
||||
hostname: site.hostname,
|
||||
owner: owner.name,
|
||||
repo_url: site.repo_url,
|
||||
};
|
||||
|
||||
Ok(site)
|
||||
}
|
||||
|
||||
pub async fn get_site(&self, owner: &str, hostname: &str) -> ServiceResult<Site> {
|
||||
let site = sqlx::query_as!(
|
||||
InnerSite,
|
||||
|
@ -606,6 +649,12 @@ mod tests {
|
|||
let db_site = db.get_site(p.username, &site.hostname).await.unwrap();
|
||||
assert_eq!(db_site, site);
|
||||
|
||||
// get site by secret
|
||||
assert_eq!(
|
||||
db_site,
|
||||
db.get_site_from_secret(&site.site_secret).await.unwrap()
|
||||
);
|
||||
|
||||
// list all sites owned by user
|
||||
let db_sites = db.list_all_sites(p.username).await.unwrap();
|
||||
assert_eq!(db_sites.len(), 1);
|
||||
|
|
Loading…
Reference in a new issue