Skip to content

Commit

Permalink
Merge pull request #110 from L2-Technology/kill-root-node
Browse files Browse the repository at this point in the history
remove root node as concept, restructure auth/admin
  • Loading branch information
johncantrell97 authored Aug 10, 2022
2 parents 7f1c797 + ac84092 commit bb197d0
Show file tree
Hide file tree
Showing 40 changed files with 722 additions and 599 deletions.
50 changes: 46 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod peer;
pub mod peer_address;
pub mod script_pubkey;
pub mod transaction;
pub mod user;
pub mod utxo;

pub mod seaql_migrations;
Expand Down
12 changes: 2 additions & 10 deletions entity/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ impl From<NodeStatus> for i16 {
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
pub enum NodeRole {
#[sea_orm(num_value = 0)]
Root,
#[sea_orm(num_value = 1)]
Default,
}

impl From<NodeRole> for i16 {
fn from(role: NodeRole) -> i16 {
match role {
NodeRole::Root => 0,
NodeRole::Default => 1,
NodeRole::Default => 0,
}
}
}
Expand Down Expand Up @@ -64,14 +61,9 @@ pub struct Model {
}

impl Model {
pub fn is_root(&self) -> bool {
self.role == 0
}

pub fn get_role(&self) -> NodeRole {
match self.role {
0 => NodeRole::Root,
1 => NodeRole::Default,
0 => NodeRole::Default,
_ => panic!("invalid role"),
}
}
Expand Down
111 changes: 111 additions & 0 deletions entity/src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use sea_orm::{entity::prelude::*, ActiveValue};
use serde::{Deserialize, Serialize};

use crate::seconds_since_epoch;

#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
pub enum UserRole {
#[sea_orm(num_value = 0)]
Default,
}

impl From<UserRole> for i16 {
fn from(role: UserRole) -> i16 {
match role {
UserRole::Default => 0,
}
}
}

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

impl EntityName for Entity {
fn table_name(&self) -> &str {
"user"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Serialize, Deserialize)]
pub struct Model {
pub id: String,
pub role: i16,
pub username: String,
pub hashed_password: String,
pub created_at: i64,
pub updated_at: i64,
}

impl Model {
pub fn get_role(&self) -> UserRole {
match self.role {
0 => UserRole::Default,
_ => panic!("invalid role"),
}
}
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Role,
Username,
HashedPassword,
CreatedAt,
UpdatedAt,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}

impl PrimaryKeyTrait for PrimaryKey {
type ValueType = String;
fn auto_increment() -> bool {
false
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::String(None).def().unique(),
Self::Role => ColumnType::SmallInteger.def(),
Self::Username => ColumnType::String(None).def().unique(),
Self::HashedPassword => ColumnType::String(None).def(),
Self::CreatedAt => ColumnType::BigInteger.def(),
Self::UpdatedAt => ColumnType::BigInteger.def(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {
fn new() -> Self {
Self {
id: ActiveValue::Set(Uuid::new_v4().to_string()),
role: ActiveValue::Set(UserRole::Default.into()),
..<Self as ActiveModelTrait>::default()
}
}

fn before_save(mut self, insert: bool) -> Result<Self, DbErr> {
let now: i64 = seconds_since_epoch();
self.updated_at = ActiveValue::Set(now);
if insert {
self.created_at = ActiveValue::Set(now);
}
Ok(self)
}
}
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod m20220428_000003_create_transactions_table;
mod m20220428_000004_create_keychains_table;
mod m20220616_000001_create_peers_table;
mod m20220701_000001_create_peer_addresses_table;
mod m20220808_000001_create_users_table;

pub struct Migrator;

Expand All @@ -30,6 +31,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220428_000004_create_keychains_table::Migration),
Box::new(m20220616_000001_create_peers_table::Migration),
Box::new(m20220701_000001_create_peer_addresses_table::Migration),
Box::new(m20220808_000001_create_users_table::Migration),
]
}
}
50 changes: 50 additions & 0 deletions migration/src/m20220808_000001_create_users_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use sea_schema::migration::prelude::*;
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220808_000001_create_users_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(User::Table)
.if_not_exists()
.col(ColumnDef::new(User::Id).string().not_null().primary_key())
.col(ColumnDef::new(User::Role).small_integer().not_null())
.col(
ColumnDef::new(User::Username)
.string()
.unique_key()
.not_null(),
)
.col(ColumnDef::new(User::HashedPassword).string().not_null())
.col(ColumnDef::new(User::CreatedAt).big_integer().not_null())
.col(ColumnDef::new(User::UpdatedAt).big_integer().not_null())
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let mut stmt = Table::drop();
stmt.table(User::Table);
manager.drop_table(stmt).await
}
}

#[derive(Iden)]
enum User {
Table,
Id,
Role,
Username,
HashedPassword,
CreatedAt,
UpdatedAt,
}
17 changes: 5 additions & 12 deletions proto/sensei.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sensei;

service Admin {
rpc CreateAdmin (CreateAdminRequest) returns (CreateAdminResponse);
rpc StartAdmin (StartAdminRequest) returns (StartAdminResponse);
rpc ListNodes (ListNodesRequest) returns (ListNodesResponse);
rpc CreateNode (CreateNodeRequest) returns (CreateNodeResponse);
rpc DeleteNode (DeleteNodeRequest) returns (DeleteNodeResponse);
Expand Down Expand Up @@ -103,17 +102,11 @@ message ListTokensResponse {

message CreateAdminRequest {
string username = 1;
string alias = 2;
string passphrase = 3;
bool start = 4;
string passphrase = 2;
}

message CreateAdminResponse {
string pubkey = 1;
string macaroon = 2;
string id = 3;
uint32 role = 4;
string token = 5;
string token = 1;
}

message CreateNodeRequest {
Expand Down Expand Up @@ -163,9 +156,9 @@ message GetStatusResponse {
optional string pubkey = 3;
optional string username = 4;
optional uint32 role = 5;
bool created = 6;
bool running = 7;
bool authenticated = 8;
bool setup = 6;
bool authenticated_admin = 7;
bool authenticated_node = 8;
}

message StartNodeRequest {
Expand Down
1 change: 1 addition & 0 deletions senseicore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bitcoincore-rpc = "0.15"
bdk = "0.19"
pin-project = "1.0"
hyper = "0.14"
bcrypt = "0.13.0"
tindercrypt = { version = "0.3.2", default-features = false }
uuid = { version = "0.8", features = ["serde", "v4"] }
macaroon = "0.2"
Expand Down
Loading

0 comments on commit bb197d0

Please sign in to comment.