feat!(db): add Resources
Build Docker Image / build-nightly (push) Successful in 1m39s Details

This commit is contained in:
subcrip 2024-04-04 18:15:56 +08:00
parent 904b809ca4
commit de4d6194f4
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
1 changed files with 124 additions and 38 deletions

162
src/db.rs
View File

@ -5,16 +5,28 @@ use uuid::Uuid;
pub struct OAuthApplication { pub struct OAuthApplication {
pub client_id: Uuid, pub client_id: Uuid,
pub client_secret: crate::misc::U256, pub client_secret: crate::misc::U256,
pub client_name: String,
pub client_desc: String,
} }
impl OAuthApplication { impl OAuthApplication {
// TEST: functionality // TEST: functionality
pub fn create() -> Self { pub fn create(client_name: &String, client_desc: &String) -> Self {
Self { client_id: Uuid::new_v4(), client_secret: crate::misc::random_256() } Self {
client_id: Uuid::new_v4(),
client_secret: crate::misc::random_256(),
client_name: client_name.to_owned(),
client_desc: client_desc.to_owned()
}
} }
pub fn new(client_id: Uuid, client_secret: crate::misc::U256) -> Self { pub fn new(client_id: &Uuid, client_secret: &crate::misc::U256, client_name: &String, client_desc: &String) -> Self {
Self { client_id, client_secret } Self {
client_id: client_id.to_owned(),
client_secret: client_secret.to_owned(),
client_name: client_name.to_owned(),
client_desc:client_desc.to_owned(),
}
} }
} }
@ -23,16 +35,28 @@ impl OAuthApplication {
pub struct OAuthHashedApplication { pub struct OAuthHashedApplication {
pub client_id: Uuid, pub client_id: Uuid,
pub client_secret: Vec<u8>, pub client_secret: Vec<u8>,
pub client_name: String,
pub client_desc: String,
} }
impl OAuthHashedApplication { impl OAuthHashedApplication {
// TEST: functionality // TEST: functionality
pub fn from(app: &OAuthApplication) -> Self { pub fn from(app: &OAuthApplication) -> Self {
Self { client_id: app.client_id, client_secret: crate::misc::digest(&app.client_secret) } Self {
client_id: app.client_id,
client_secret: crate::misc::digest(&app.client_secret),
client_name: app.client_name.to_owned(),
client_desc: app.client_desc.to_owned(),
}
} }
pub fn new(client_id: &Uuid, client_secret: &Vec<u8>) -> Self { pub fn new(client_id: &Uuid, client_secret: &Vec<u8>, client_name: &String, client_desc: &String) -> Self {
Self { client_id: client_id.to_owned(), client_secret: client_secret.to_owned() } Self {
client_id: client_id.to_owned(),
client_secret: client_secret.to_owned(),
client_name: client_name.to_owned(),
client_desc: client_desc.to_owned(),
}
} }
} }
@ -44,27 +68,90 @@ pub struct OAuthApplicationAccess {
impl OAuthApplicationAccess { impl OAuthApplicationAccess {
pub fn create(app: &OAuthHashedApplication, scope: &OAuthScope) -> Self { pub fn create(app: &OAuthHashedApplication, scope: &OAuthScope) -> Self {
Self { access_id: Uuid::new_v4(), client_id: app.client_id, scope_id: scope.scope_id } 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 { 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() } Self {
access_id: access_id.to_owned(),
client_id: client_id.to_owned(),
scope_id: scope_id.to_owned()
}
} }
} }
pub struct OAuthScope { pub struct OAuthScope {
pub scope_id: Uuid, pub scope_id: Uuid,
pub resource_id: Uuid,
pub scope_name: String,
pub scope_desc: String, pub scope_desc: String,
} }
impl OAuthScope { impl OAuthScope {
// TEST: functionality // TEST: functionality
pub fn create(description: String) -> Self { pub fn create(resource_id: &Uuid, name: &String, description: &String) -> Self {
Self { scope_id: Uuid::new_v4(), scope_desc: description } Self {
scope_id: Uuid::new_v4(),
resource_id: resource_id.to_owned(),
scope_name: name.to_owned(),
scope_desc: description.to_owned(),
}
} }
pub fn new(scope_id: Uuid, scope_desc: String) -> Self { pub fn new(scope_id: &Uuid, resource_id: &Uuid, scope_name: &String, scope_desc: &String) -> Self {
Self { scope_id, scope_desc } Self {
scope_id: scope_id.to_owned(),
resource_id: resource_id.to_owned(),
scope_name: scope_name.to_owned(),
scope_desc: scope_desc.to_owned(),
}
}
}
pub struct OAuthUser {
pub user_id: Uuid,
pub user_name: String,
}
impl OAuthUser {
pub fn create(name: &String) -> Self {
Self {
user_id: Uuid::new_v4(),
user_name: name.to_owned(),
}
}
pub fn new(user_id: &Uuid, user_name: &String) -> Self {
Self {
user_id: user_id.to_owned(),
user_name: user_name.to_owned(),
}
}
}
pub struct OAuthResource {
pub resource_id: Uuid,
pub resource_name: String,
pub resource_desc: String,
}
impl OAuthResource {
pub fn create(name: &String, description: &String) -> Self {
Self {
resource_id: Uuid::new_v4(),
resource_name: name.to_owned(),
resource_desc: description.to_owned(),
}
}
pub fn new(resource_id: &Uuid, resource_name: &String, resource_desc: &String) -> Self {
Self {
resource_id: resource_id.to_owned(),
resource_name: resource_name.to_owned(),
resource_desc: resource_desc.to_owned(),
}
} }
} }
@ -74,19 +161,6 @@ pub struct OAuthDatabase {
handle: tokio::task::JoinHandle<Result<(), tokio_postgres::Error>>, 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 { impl OAuthDatabase {
/// Establish a new connection to a database. /// 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> { pub async fn connect(host: &str, port: u16, user: &str, password: &str, db_name: &str) -> Result<Self, tokio_postgres::Error> {
@ -122,29 +196,41 @@ impl OAuthDatabase {
/// Initialize the database. /// Initialize the database.
pub async fn init(&mut self) -> Result<(), tokio_postgres::Error> { pub async fn init(&mut self) -> Result<(), tokio_postgres::Error> {
// TEST: functionality // TEST: functionality
self.psql_execute("create table Applications(client_id uuid primary key, client_secret bytea)", &[]).await?; self.psql_execute("create table Applications(client_id uuid primary key, client_secret bytea, client_name text, client_desc text)", &[]).await?;
self.psql_execute("create table Scopes(scope_id uuid primary key, scope_desc text)", &[]).await?; self.psql_execute("create table Resources(resource_id uuid primary key, resource_name text, resource_desc text)", &[]).await?;
self.psql_execute("create table Scopes(scope_id uuid primary key, resource_id uuid, scope_name text, scope_desc text)", &[]).await?;
self.psql_execute("create table ApplicationAccess(access_id uuid primary key, client_id uuid, scope_id uuid)", &[]).await?; 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?; self.psql_execute("create table Users(user_id uuid primary key, user_name text)", &[]).await?;
// TODO: struct
self.psql_execute("create table MasterDBAccess(master_db_token bytea primary key, master_db_desc text)", &[]).await?;
Ok(()) Ok(())
} }
/// Register an application. /// Register an application.
/// Applicant: client /// Applicant: client
pub async fn trusted_register_application(&mut self) -> Result<OAuthApplication, tokio_postgres::Error> { pub async fn trusted_register_application(&mut self, name: &String, description: &String) -> Result<OAuthApplication, tokio_postgres::Error> {
// TEST: functionality // TEST: functionality
let app = OAuthApplication::create(); let app = OAuthApplication::create(name, description);
let app_hash = OAuthHashedApplication::from(&app); 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?; self.psql_execute("insert into Applications (client_id, client_secret, client_name, client_desc) values ($1, $2, $3, $4)", &[&app_hash.client_id, &app_hash.client_secret, &app_hash.client_name, &app_hash.client_desc]).await?;
Ok(app) Ok(app)
} }
/// Register a resource.
/// Applicant: resource
pub async fn trusted_register_resource(&mut self, name: &String, description: &String) -> Result<OAuthResource, tokio_postgres::Error> {
// TEST: functionality
let resource = OAuthResource::create(name, description);
self.psql_execute("insert into Resources (resource_id, resource_name, resource_desc) values ($1, $2, $3)", &[&resource.resource_id, &resource.resource_name, &resource.resource_desc]).await?;
Ok(resource)
}
/// Register a scope. /// Register a scope.
/// Applicant: resource /// Applicant: resource
pub async fn trusted_register_scope(&mut self, description: String) -> Result<OAuthScope, tokio_postgres::Error> { pub async fn trusted_register_scope(&mut self, resource_id: &Uuid, name: &String, description: &String) -> Result<OAuthScope, tokio_postgres::Error> {
// TEST: functionality // TEST: functionality
let scope = OAuthScope::create(description); let scope = OAuthScope::create(resource_id, name, description);
self.psql_execute("insert into Scopes (scope_id, scope_desc) values ($1, $2)", &[&scope.scope_id, &scope.scope_desc]).await?; self.psql_execute("insert into Scopes (scope_id, resource_id, scope_desc) values ($1, $2, $3)", &[&scope.scope_id, &scope.resource_id, &scope.scope_desc]).await?;
Ok(scope) Ok(scope)
} }
@ -158,9 +244,9 @@ impl OAuthDatabase {
/// Register a user. /// Register a user.
/// Applicant: resource /// Applicant: resource
pub async fn trusted_register_user(&mut self) -> Result<OAuthUser, tokio_postgres::Error> { pub async fn trusted_register_user(&mut self, name: &String) -> Result<OAuthUser, tokio_postgres::Error> {
let user = OAuthUser::create(); let user = OAuthUser::create(name);
self.psql_execute("insert into Users(user_id) values ($1)", &[&user.user_id]).await?; self.psql_execute("insert into Users(user_id, user_name) values ($1, $2)", &[&user.user_id, &user.user_name]).await?;
Ok(user) Ok(user)
} }
} }