feat: auth basics

This commit is contained in:
nullishamy 2025-04-24 20:19:54 +01:00
parent 005c13e1d4
commit 9c7c2858cc
Signed by: amy
SSH key fingerprint: SHA256:WmV0uk6WgAQvDJlM8Ld4mFPHZo02CLXXP5VkwQ5xtyk
11 changed files with 215 additions and 29 deletions

View file

@ -1,14 +1,44 @@
use rocket::{form::Form, post, serde::json::Json};
use crate::Db;
use crate::types::oauth::{App, CredentialApplication};
use rocket_db_pools::Connection;
#[post("/apps", data = "<app>")]
pub async fn new_app(app: Form<App>) -> Json<CredentialApplication> {
pub async fn new_app(app: Form<App>, mut db: Connection<Db>) -> Json<CredentialApplication> {
let secret = main::gen_token(15);
// Abort when we encounter a duplicate
let is_app_present = sqlx::query!(
r#"
INSERT INTO app (client_id, client_secret, scopes)
VALUES (?1, ?2, ?3)
"#,
app.client_name,
app.scopes,
secret
)
.execute(&mut **db)
.await
.is_err();
let mut app: App = app.clone();
if is_app_present {
let existing_app = sqlx::query!("SELECT * FROM app WHERE client_id = ?1", app.client_name)
.fetch_one(&mut **db)
.await
.unwrap();
app.client_name = existing_app.client_id;
app.scopes = existing_app.scopes;
}
Json(CredentialApplication {
name: app.client_name.clone(),
scopes: app.scopes.clone(),
redirect_uris: app.redirect_uris.clone(),
client_id: format!("id-for-{}", app.client_name),
client_secret: format!("secret-for-{}", app.client_name),
client_id: app.client_name.clone(),
client_secret: secret,
})
}

View file

@ -1,4 +1,4 @@
use crate::{Db, endpoints::api::user::CredentialAcount};
use crate::{AuthenticatedUser, Db, endpoints::api::user::CredentialAcount};
use rocket::{
get,
serde::{Deserialize, Serialize, json::Json},
@ -32,7 +32,12 @@ pub struct TimelineStatus {
}
#[get("/timelines/home?<limit>")]
pub async fn home(mut db: Connection<Db>, limit: i64) -> Json<Vec<TimelineStatus>> {
pub async fn home(
mut db: Connection<Db>,
limit: i64,
user: AuthenticatedUser,
) -> Json<Vec<TimelineStatus>> {
dbg!(user);
let posts = sqlx::query!(
r#"
SELECT p.id as "post_id", u.id as "user_id", p.content, p.uri as "post_uri",