Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/admin rpc #60

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 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 docker/node/orand.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN apt-get update && \
FROM debian:bookworm

ENV RUST_LOG="debug"
ENV RUST_BACKTRACE=full

COPY --from=builder /orochimaru/target/release/node /bin/orand
COPY --from=builder /orochimaru/target/release/cli /bin/orand-cli
Expand Down
2 changes: 2 additions & 0 deletions docker/node/pg-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ ALTER TABLE public.keyring OWNER TO orand;

create table public.receiver (
id bigserial not null,
keyring_id int8 not null,
"name" varchar not null,
address varchar not null,
network int8 not null,
nonce int8 not null,
created_date timestamp not null default CURRENT_TIMESTAMP,
constraint index_name unique (name),
constraint receiver_pkey primary key (id)
constraint link_receiver_to_keyring foreign key (keyring_id) references public.keyring(id),
);

ALTER TABLE public.receiver OWNER TO orand;
Expand Down
8 changes: 5 additions & 3 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ path = "src/lib.rs"
serde = { workspace = true }
serde_json = { workspace = true }
hex = { workspace = true }
revm = { git="https://github.com/bluealloy/revm.git", rev = "5e6546e" }
revm = { git = "https://github.com/bluealloy/revm.git", rev = "5e6546e" }
clap = "4.4.16"
libecvrf = { version = "^1.1.2", path = "../libecvrf" }
tiny-keccak = { version = "2.0.2", default-features = false, features = [
Expand All @@ -34,7 +34,7 @@ tiny-keccak = { version = "2.0.2", default-features = false, features = [
log = "0.4.20"
env_logger = "0.10.1"
tokio = { version = "1.35.1", features = ["full"] }
sea-orm = { version = "0.12.11", features = [
sea-orm = { version = "0.12.14", features = [
"sqlx-postgres",
"runtime-tokio-rustls",
"macros",
Expand All @@ -48,4 +48,6 @@ base64-url = "2.0.2"
sha2 = "0.10.8"
hyper = { version = "1.1.0", features = ["full"] }
http-body-util = "0.1.0"
hyper-util = { git = "https://github.com/hyperium/hyper-util.git", tag = "v0.1.2", features = ["tokio"]}
hyper-util = { git = "https://github.com/hyperium/hyper-util.git", tag = "v0.1.2", features = [
"tokio",
] }
12 changes: 12 additions & 0 deletions node/migration/src/m20221229_005309_create_table_receiver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use sea_orm_migration::prelude::*;

use crate::m20220101_000001_create_table_keyring::Keyring;

#[derive(DeriveMigrationName)]
pub struct Migration;

Expand All @@ -17,6 +19,7 @@ impl MigrationTrait for Migration {
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Receiver::KeyringId).big_integer().not_null())
.col(ColumnDef::new(Receiver::Name).string().not_null())
.col(ColumnDef::new(Receiver::Address).string().not_null())
.col(ColumnDef::new(Receiver::Network).big_unsigned().not_null())
Expand All @@ -27,6 +30,14 @@ impl MigrationTrait for Migration {
.extra("DEFAULT CURRENT_TIMESTAMP".to_string())
.not_null(),
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("link_receiver_to_keyring")
.from_tbl(Receiver::Table)
.from_col(Receiver::KeyringId)
.to_tbl(Keyring::Table)
.to_col(Keyring::Id),
)
.index(
Index::create()
.name("index_name")
Expand All @@ -50,6 +61,7 @@ impl MigrationTrait for Migration {
pub enum Receiver {
Table,
Id,
KeyringId,
Name,
Address,
Network,
Expand Down
19 changes: 17 additions & 2 deletions node/src/jwt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::SystemTime;

use crate::Error;
use base64_url;
use hex;
Expand All @@ -16,8 +18,10 @@ pub struct JWTPayload {
pub user: String,
/// Nonce
pub nonce: u32,
/// Unix timestamp
pub timestamp: u64,
/// Unix issue at timestamp
pub iat: u64,
/// Unix expired timestamp
pub exp: u64,
}

/// JWT
Expand All @@ -37,6 +41,10 @@ impl JWT {
/// Encode payload to JWT
pub fn decode_payload(json_web_token: &str) -> Result<JWTPayload, Error> {
let split_jwt: Vec<&str> = json_web_token.trim().split('.').collect();
let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Unable to get current time")
.as_secs();
if split_jwt.len() == 3 {
let decoded_payload = match base64_url::decode(&split_jwt[1]) {
Ok(payload) => payload,
Expand All @@ -47,6 +55,13 @@ impl JWT {
Ok(payload) => payload,
Err(_) => return Err(Error("INVALID_PAYLOAD", "Unable to deserialize payload")),
};
// Check if JWT is expired, iat < current_time < exp
if current_time > jwt_payload.exp
|| current_time < jwt_payload.iat
|| jwt_payload.iat > jwt_payload.exp
{
return Err(Error("EXPIRED_JWT", "JWT is expired"));
}
if regex_name.is_match(&jwt_payload.user) {
return Ok(jwt_payload);
} else {
Expand Down
Loading
Loading