feat(db): register access and users

This commit is contained in:
subcrip 2024-03-31 23:37:14 +08:00
parent fde2b72325
commit dfd7c6c9fd
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
2 changed files with 54 additions and 25 deletions

View File

@ -9,11 +9,11 @@ pub struct OAuthApplication {
impl OAuthApplication {
// TEST: functionality
pub fn new() -> Self {
pub fn create() -> Self {
Self { client_id: Uuid::new_v4(), client_secret: crate::misc::random_256() }
}
pub fn from(client_id: Uuid, client_secret: crate::misc::U256) -> Self {
pub fn new(client_id: Uuid, client_secret: crate::misc::U256) -> Self {
Self { client_id, client_secret }
}
}
@ -30,6 +30,26 @@ impl OAuthHashedApplication {
pub fn from(app: &OAuthApplication) -> Self {
Self { client_id: app.client_id, client_secret: crate::misc::digest(&app.client_secret) }
}
pub fn new(client_id: &Uuid, client_secret: &Vec<u8>) -> Self {
Self { client_id: client_id.to_owned(), client_secret: client_secret.to_owned() }
}
}
pub struct OAuthApplicationAccess {
pub access_id: Uuid,
pub client_id: Uuid,
pub scope_id: Uuid,
}
impl OAuthApplicationAccess {
pub fn create(app: &OAuthHashedApplication, scope: &OAuthScope) -> Self {
Self { access_id: Uuid::new_v4(), client_id: app.client_id, scope_id: scope.scope_id }
}
pub fn new(access_id: &Uuid, client_id: &Uuid, scope_id: &Uuid) -> Self {
Self { access_id: access_id.to_owned(), client_id: client_id.to_owned(), scope_id: scope_id.to_owned() }
}
}
pub struct OAuthScope {
@ -39,11 +59,11 @@ pub struct OAuthScope {
impl OAuthScope {
// TEST: functionality
pub fn new(description: String) -> Self {
pub fn create(description: String) -> Self {
Self { scope_id: Uuid::new_v4(), scope_desc: description }
}
pub fn from(scope_id: Uuid, scope_desc: String) -> Self {
pub fn new(scope_id: Uuid, scope_desc: String) -> Self {
Self { scope_id, scope_desc }
}
}
@ -54,6 +74,19 @@ pub struct OAuthDatabase {
handle: tokio::task::JoinHandle<Result<(), tokio_postgres::Error>>,
}
pub struct OAuthUser {
pub user_id: Uuid,
}
impl OAuthUser {
pub fn create() -> Self {
Self { user_id: Uuid::new_v4() }
}
pub fn new(user_id: Uuid) -> Self {
Self { user_id }
}
}
impl OAuthDatabase {
/// Establish a new connection to a database.
pub async fn connect(host: &str, port: u16, user: &str, password: &str, db_name: &str) -> Result<Self, tokio_postgres::Error> {
@ -91,8 +124,8 @@ impl OAuthDatabase {
// TEST: functionality
self.psql_execute("create table Applications(client_id uuid primary key, client_secret bytea)", &[]).await?;
self.psql_execute("create table Scopes(scope_id uuid primary key, scope_desc text)", &[]).await?;
// TODO: OAuthApplicationAccess struct
self.psql_execute("create table ApplicationAccess(access_id uuid primary key, client_id uuid, scope_id uuid)", &[]).await?;
self.psql_execute("create table Users(user_id uuid primary key)", &[]).await?;
Ok(())
}
@ -100,7 +133,7 @@ impl OAuthDatabase {
/// Applicant: client
pub async fn trusted_register_application(&mut self) -> Result<OAuthApplication, tokio_postgres::Error> {
// TEST: functionality
let app = OAuthApplication::new();
let app = OAuthApplication::create();
let app_hash = OAuthHashedApplication::from(&app);
self.psql_execute("insert into Applications (client_id, client_secret) values ($1, $2)", &[&app_hash.client_id, &app_hash.client_secret]).await?;
Ok(app)
@ -110,17 +143,26 @@ impl OAuthDatabase {
/// Applicant: resource
pub async fn trusted_register_scope(&mut self, description: String) -> Result<OAuthScope, tokio_postgres::Error> {
// TEST: functionality
let scope = OAuthScope::new(description);
let scope = OAuthScope::create(description);
self.psql_execute("insert into Scopes (scope_id, scope_desc) values ($1, $2)", &[&scope.scope_id, &scope.scope_desc]).await?;
Ok(scope)
}
/// Register an access relation.
/// Applicant: client
pub async fn trusted_register_access(&mut self, app: OAuthHashedApplication, scope: OAuthScope) -> Result<(), tokio_postgres::Error> {
// TODO: implementation
pub async fn trusted_register_access(&mut self, app: &OAuthHashedApplication, scope: &OAuthScope) -> Result<(), tokio_postgres::Error> {
let access = OAuthApplicationAccess::create(app, scope);
self.psql_execute("insert into ApplicationAccess(access_id, client_id, scope_id) values($1, $2, $3)", &[&access.access_id, &access.client_id, &access.scope_id]).await?;
Ok(())
}
/// Register a user.
/// Applicant: resource
pub async fn trusted_register_user(&mut self) -> Result<OAuthUser, tokio_postgres::Error> {
let user = OAuthUser::create();
self.psql_execute("insert into Users(user_id) values ($1)", &[&user.user_id]).await?;
Ok(user)
}
}
#[cfg(test)]

View File

@ -4,25 +4,12 @@ use tide::prelude::*;
mod db;
mod misc;
#[derive(Serialize, Deserialize)]
struct Test {
x: i32,
y: i32
}
impl Test {
fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
#[tokio::main]
async fn main() {
let a = Test::new(2, 3);
let mut b = db::OAuthDatabase::connect("localhost", 5432, "postgres", "configjson", "myoauth").await.unwrap();
// let s = serde_json::to_string(&a);
// json!(a);
println!("{}", json!(a));
// b.init().await.unwrap();
let u = b.trusted_register_user().await.unwrap();
println!("Successfully registered user with uid = {}", u.user_id);
}
#[tokio::test]