feat(db): register access and users
This commit is contained in:
parent
fde2b72325
commit
dfd7c6c9fd
60
src/db.rs
60
src/db.rs
|
@ -9,11 +9,11 @@ pub struct OAuthApplication {
|
||||||
|
|
||||||
impl OAuthApplication {
|
impl OAuthApplication {
|
||||||
// TEST: functionality
|
// TEST: functionality
|
||||||
pub fn new() -> Self {
|
pub fn create() -> 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() }
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }
|
Self { client_id, client_secret }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,26 @@ impl OAuthHashedApplication {
|
||||||
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) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
pub struct OAuthScope {
|
||||||
|
@ -39,11 +59,11 @@ pub struct OAuthScope {
|
||||||
|
|
||||||
impl OAuthScope {
|
impl OAuthScope {
|
||||||
// TEST: functionality
|
// TEST: functionality
|
||||||
pub fn new(description: String) -> Self {
|
pub fn create(description: String) -> Self {
|
||||||
Self { scope_id: Uuid::new_v4(), scope_desc: description }
|
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 }
|
Self { scope_id, scope_desc }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,6 +74,19 @@ 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> {
|
||||||
|
@ -91,8 +124,8 @@ impl OAuthDatabase {
|
||||||
// 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)", &[]).await?;
|
||||||
self.psql_execute("create table Scopes(scope_id uuid primary key, scope_desc text)", &[]).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 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +133,7 @@ impl OAuthDatabase {
|
||||||
/// 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) -> Result<OAuthApplication, tokio_postgres::Error> {
|
||||||
// TEST: functionality
|
// TEST: functionality
|
||||||
let app = OAuthApplication::new();
|
let app = OAuthApplication::create();
|
||||||
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) values ($1, $2)", &[&app_hash.client_id, &app_hash.client_secret]).await?;
|
||||||
Ok(app)
|
Ok(app)
|
||||||
|
@ -110,17 +143,26 @@ impl OAuthDatabase {
|
||||||
/// 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, description: String) -> Result<OAuthScope, tokio_postgres::Error> {
|
||||||
// TEST: functionality
|
// 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?;
|
self.psql_execute("insert into Scopes (scope_id, scope_desc) values ($1, $2)", &[&scope.scope_id, &scope.scope_desc]).await?;
|
||||||
Ok(scope)
|
Ok(scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register an access relation.
|
/// Register an access relation.
|
||||||
/// Applicant: client
|
/// Applicant: client
|
||||||
pub async fn trusted_register_access(&mut self, app: OAuthHashedApplication, scope: OAuthScope) -> Result<(), tokio_postgres::Error> {
|
pub async fn trusted_register_access(&mut self, app: &OAuthHashedApplication, scope: &OAuthScope) -> Result<(), tokio_postgres::Error> {
|
||||||
// TODO: implementation
|
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(())
|
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)]
|
#[cfg(test)]
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -4,25 +4,12 @@ use tide::prelude::*;
|
||||||
mod db;
|
mod db;
|
||||||
mod misc;
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let a = Test::new(2, 3);
|
|
||||||
let mut b = db::OAuthDatabase::connect("localhost", 5432, "postgres", "configjson", "myoauth").await.unwrap();
|
let mut b = db::OAuthDatabase::connect("localhost", 5432, "postgres", "configjson", "myoauth").await.unwrap();
|
||||||
// let s = serde_json::to_string(&a);
|
// b.init().await.unwrap();
|
||||||
// json!(a);
|
let u = b.trusted_register_user().await.unwrap();
|
||||||
println!("{}", json!(a));
|
println!("Successfully registered user with uid = {}", u.user_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
Loading…
Reference in New Issue