Skip to content

Commit

Permalink
merge issue-auth, conflicts solved
Browse files Browse the repository at this point in the history
  • Loading branch information
vsilent committed Nov 22, 2023
2 parents 96a5538 + c7e0bdb commit 3a9fa05
Show file tree
Hide file tree
Showing 28 changed files with 697 additions and 150 deletions.
67 changes: 57 additions & 10 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ actix-cors = "0.6.4"
tracing-actix-web = "0.7.7"
regex = "1.10.2"
rand = "0.8.5"
futures-util = "0.3.29"
futures = "0.3.29"
tokio-stream = "0.1.14"
actix-http = "3.4.0"
hmac = "0.12.1"
sha2 = "0.10.8"

# dctypes
derive_builder = "0.12.0"
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ sqlx migrate revert
#### Deploy
```
curl -X POST -H "Content-Type: application/json" -d @custom-stack-payload-2.json http://127.0.0.1:8000/stack
```
```


#### Create API Client
curl -X POST http://localhost:8000/client --header 'Content-Type: application/json' -H "Authorization: Bearer $TD_BEARER"

test client deploy
http://localhost:8000/test/deploy
1 change: 1 addition & 0 deletions configuration.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#auth_url: http://127.0.0.1:8080/me
app_host: 127.0.0.1
app_port: 8000
auth_url: https://dev.try.direct/server/user/oauth_server/api/me
Expand Down
2 changes: 1 addition & 1 deletion migrations/20231028161917_client.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CREATE TABLE public.client (
id serial4 NOT NULL,
user_id varchar(50) NOT NULL,
secret varchar(255) NOT NULL,
secret varchar(255),
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT client_pkey PRIMARY KEY (id),
Expand Down
21 changes: 20 additions & 1 deletion src/helpers/client/generate_secret.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::helpers::client;
use rand::Rng;
use sqlx::PgPool;

pub fn generate_secret(len: usize) -> String {
fn make_secret(len: usize) -> String {
const CHARSET: &[u8] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789)(*&^%$#@!~";
let mut rng = rand::thread_rng();
Expand All @@ -12,3 +14,20 @@ pub fn generate_secret(len: usize) -> String {
})
.collect()
}

pub async fn generate_secret(pool: &PgPool, len: usize) -> Result<String, String> {
loop {
let secret = make_secret(len);
match client::is_secret_unique(pool, &secret).await {
Ok(is_unique) if is_unique => {
return Ok(secret);
}
Ok(_) => {
continue;
}
Err(e) => {
return Err(format!("Failed to execute query: {:?}", e));
}
}
}
}
28 changes: 28 additions & 0 deletions src/helpers/client/is_secret_unique.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use sqlx::PgPool;
use tracing::Instrument;

#[tracing::instrument(name = "Check if secret is unique.")]
pub async fn is_secret_unique(pool_ref: &PgPool, secret: &String) -> Result<bool, String> {
let query_span = tracing::info_span!("Looking for the secret in the client's table.");
match sqlx::query!(
r#"
SELECT
count(*) as found
FROM client c
WHERE c.secret = $1
LIMIT 1
"#,
secret,
)
.fetch_one(pool_ref)
.instrument(query_span)
.await
{
Ok(result) => {
return Ok(result.found < Some(1));
}
Err(e) => {
return Err(format!("{e:?}"));
}
};
}
2 changes: 2 additions & 0 deletions src/helpers/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod generate_secret;
mod is_secret_unique;

pub use generate_secret::*;
pub use is_secret_unique::*;
2 changes: 1 addition & 1 deletion src/helpers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where T: serde::Serialize + Default
// list: None,
// }
// }

//
// pub(crate) fn internal_error(message: &str) -> Self {
//
// let msg = if !message.trim().is_empty() {
Expand Down
1 change: 0 additions & 1 deletion src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod client;
pub(crate) mod json;
pub mod serialize_datetime;
pub(crate) mod stack;

pub use json::*;
21 changes: 0 additions & 21 deletions src/helpers/serialize_datetime.rs

This file was deleted.

7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::net::TcpListener;
use sqlx::PgPool;
use stacker::configuration::get_configuration;
use stacker::startup::run;
use stacker::telemetry::{get_subscriber, init_subscriber};

use std::net::TcpListener;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
Expand All @@ -18,8 +17,8 @@ async fn main() -> std::io::Result<()> {

let address = format!("{}:{}", settings.app_host, settings.app_port);
tracing::info!("Start server at {:?}", &address);
let listener = TcpListener::bind(address)
.expect(&format!("failed to bind to {}", settings.app_port));
let listener =
TcpListener::bind(address).expect(&format!("failed to bind to {}", settings.app_port));

run(listener, db_pool, settings).await?.await
}
3 changes: 0 additions & 3 deletions src/middleware/auth.rs

This file was deleted.

Loading

0 comments on commit 3a9fa05

Please sign in to comment.