diff --git a/src/db.rs b/src/db.rs index 4c6537e..d1bd3d1 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,16 +5,28 @@ use uuid::Uuid; pub struct OAuthApplication { pub client_id: Uuid, pub client_secret: crate::misc::U256, + pub client_name: String, + pub client_desc: String, } impl OAuthApplication { // TEST: functionality - pub fn create() -> Self { - Self { client_id: Uuid::new_v4(), client_secret: crate::misc::random_256() } + pub fn create(client_name: &String, client_desc: &String) -> Self { + 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 { - Self { client_id, client_secret } + pub fn new(client_id: &Uuid, client_secret: &crate::misc::U256, client_name: &String, client_desc: &String) -> Self { + 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 client_id: Uuid, pub client_secret: Vec, + pub client_name: String, + pub client_desc: String, } impl OAuthHashedApplication { // TEST: functionality 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) -> Self { - Self { client_id: client_id.to_owned(), client_secret: client_secret.to_owned() } + pub fn new(client_id: &Uuid, client_secret: &Vec, client_name: &String, client_desc: &String) -> Self { + 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 { 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 { - 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 scope_id: Uuid, + pub resource_id: Uuid, + pub scope_name: String, pub scope_desc: String, } impl OAuthScope { // TEST: functionality - pub fn create(description: String) -> Self { - Self { scope_id: Uuid::new_v4(), scope_desc: description } + pub fn create(resource_id: &Uuid, name: &String, description: &String) -> Self { + 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 { - Self { scope_id, scope_desc } + pub fn new(scope_id: &Uuid, resource_id: &Uuid, scope_name: &String, scope_desc: &String) -> Self { + 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>, } -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 { @@ -122,29 +196,41 @@ impl OAuthDatabase { /// Initialize the database. pub async fn init(&mut self) -> Result<(), tokio_postgres::Error> { // 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?; + 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 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 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(()) } /// Register an application. /// Applicant: client - pub async fn trusted_register_application(&mut self) -> Result { + pub async fn trusted_register_application(&mut self, name: &String, description: &String) -> Result { // TEST: functionality - let app = OAuthApplication::create(); + let app = OAuthApplication::create(name, description); 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) } + /// Register a resource. + /// Applicant: resource + pub async fn trusted_register_resource(&mut self, name: &String, description: &String) -> Result { + // 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. /// Applicant: resource - pub async fn trusted_register_scope(&mut self, description: String) -> Result { + pub async fn trusted_register_scope(&mut self, resource_id: &Uuid, name: &String, description: &String) -> Result { // TEST: functionality - 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?; + let scope = OAuthScope::create(resource_id, name, description); + 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) } @@ -158,9 +244,9 @@ impl OAuthDatabase { /// Register a user. /// Applicant: resource - pub async fn trusted_register_user(&mut self) -> Result { - let user = OAuthUser::create(); - self.psql_execute("insert into Users(user_id) values ($1)", &[&user.user_id]).await?; + pub async fn trusted_register_user(&mut self, name: &String) -> Result { + let user = OAuthUser::create(name); + self.psql_execute("insert into Users(user_id, user_name) values ($1, $2)", &[&user.user_id, &user.user_name]).await?; Ok(user) } }