-
Notifications
You must be signed in to change notification settings - Fork 393
dynamodb and sqlite are both supported as distinct features. #50
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
Open
Fusion
wants to merge
4
commits into
agrinman:master
Choose a base branch
from
Fusion:feature-sqlite-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75637f0
dynamodb and sqlite are both supported as distinct features.
Fusion 3c63607
Updating per feedback.
Fusion 6d3aec4
Merge branch 'master' into feature-sqlite-support
Fusion d04b298
Merge branch 'agrinman:master' into feature-sqlite-support
Fusion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| use rusqlite::{params, NO_PARAMS, Connection}; | ||
|
|
||
| use super::AuthResult; | ||
| use super::super::CONFIG; | ||
| use crate::auth::AuthService; | ||
| use async_trait::async_trait; | ||
| use sha2::Digest; | ||
| use std::str::FromStr; | ||
| use thiserror::Error; | ||
| use uuid::Uuid; | ||
| use std::sync::Mutex; | ||
|
|
||
| mod domain_db { | ||
| pub const TABLE_NAME: &'static str = "tunnelto_domains"; | ||
| pub const PRIMARY_KEY: &'static str = "subdomain"; | ||
| pub const ACCOUNT_ID: &'static str = "account_id"; | ||
| } | ||
|
|
||
| mod key_db { | ||
| pub const TABLE_NAME: &'static str = "tunnelto_auth"; | ||
| pub const PRIMARY_KEY: &'static str = "auth_key_hash"; | ||
| pub const ACCOUNT_ID: &'static str = "account_id"; | ||
| } | ||
|
|
||
| mod record_db { | ||
| pub const TABLE_NAME: &'static str = "tunnelto_record"; | ||
| pub const PRIMARY_KEY: &'static str = "account_id"; | ||
| pub const SUBSCRIPTION_ID: &'static str = "subscription_id"; | ||
| } | ||
|
|
||
| pub struct AuthDbService { | ||
| connection: Mutex<Connection>, | ||
| } | ||
|
|
||
| impl AuthDbService { | ||
| pub fn new() -> Result<Self, Box<dyn std::error::Error>> { | ||
| let conn = Connection::open(&CONFIG.db_connection_string)?; | ||
| conn.execute( | ||
| &format!("CREATE TABLE IF NOT EXISTS {} ( | ||
| {} TEXT NOT NULL, | ||
| {} TEXT NOT NULL | ||
| )", | ||
| domain_db::TABLE_NAME, | ||
| domain_db::PRIMARY_KEY, | ||
| domain_db::ACCOUNT_ID | ||
| ), | ||
| NO_PARAMS, | ||
| )?; | ||
| conn.execute( | ||
| &format!("CREATE TABLE IF NOT EXISTS {} ( | ||
| {} TEXT NOT NULL, | ||
| {} TEXT NOT NULL | ||
| )", | ||
| key_db::TABLE_NAME, | ||
| key_db::PRIMARY_KEY, | ||
| key_db::ACCOUNT_ID | ||
| ), | ||
| NO_PARAMS, | ||
| )?; | ||
| conn.execute( | ||
| &format!("CREATE TABLE IF NOT EXISTS {} ( | ||
| {} TEXT NOT NULL, | ||
| {} TEXT NOT NULL | ||
| )", | ||
| record_db::TABLE_NAME, | ||
| record_db::PRIMARY_KEY, | ||
| record_db::SUBSCRIPTION_ID | ||
| ), | ||
| NO_PARAMS, | ||
| )?; | ||
| Ok( Self{connection: Mutex::new(conn)} ) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for AuthDbService { | ||
| fn drop(&mut self) { | ||
| let c = &*self.connection.lock().unwrap(); | ||
| drop(c); | ||
| } | ||
| } | ||
|
|
||
| fn key_id(auth_key: &str) -> String { | ||
| let hash = sha2::Sha256::digest(auth_key.as_bytes()).to_vec(); | ||
| base64::encode_config(&hash, base64::URL_SAFE_NO_PAD) | ||
| } | ||
|
|
||
| #[derive(Error, Debug)] | ||
| pub enum Error { | ||
| #[error("The authentication key is invalid")] | ||
| AccountNotFound, | ||
|
|
||
| #[error("The authentication key is invalid")] | ||
| InvalidAccountId(#[from] uuid::Error), | ||
|
|
||
| #[error("The subdomain is not authorized")] | ||
| SubdomainNotAuthorized, | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl AuthService for AuthDbService { | ||
| type Error = Error; | ||
| type AuthKey = String; | ||
|
|
||
| async fn auth_sub_domain( | ||
| &self, | ||
| auth_key: &String, | ||
Fusion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| subdomain: &str, | ||
| ) -> Result<AuthResult, Error> { | ||
| let authenticated_account_id = self.get_account_id_for_auth_key(auth_key).await?; | ||
| let is_pro_account = self | ||
| .is_account_in_good_standing(authenticated_account_id) | ||
| .await?; | ||
|
|
||
| tracing::info!(account=%authenticated_account_id.to_string(), requested_subdomain=%subdomain, is_pro=%is_pro_account, "authenticated client"); | ||
|
|
||
| if let Some(account_id) = self.get_account_id_for_subdomain(subdomain).await? { | ||
| // check you reserved it | ||
| if authenticated_account_id != account_id { | ||
| tracing::info!(account=%authenticated_account_id.to_string(), "reserved by other"); | ||
| return Ok(AuthResult::ReservedByOther); | ||
| } | ||
|
|
||
| // next ensure that the account is in good standing | ||
| if !is_pro_account { | ||
| tracing::warn!(account=%authenticated_account_id.to_string(), "delinquent"); | ||
| return Ok(AuthResult::ReservedByYouButDelinquent); | ||
| } | ||
|
|
||
| return Ok(AuthResult::ReservedByYou); | ||
| } | ||
|
|
||
| if is_pro_account { | ||
| Ok(AuthResult::Available) | ||
| } else { | ||
| Ok(AuthResult::PaymentRequired) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl AuthDbService { | ||
| async fn get_account_id_for_auth_key(&self, auth_key: &str) -> Result<Uuid, Error> { | ||
| let auth_key_hash = key_id(auth_key); | ||
|
|
||
| let conn:&Connection = &*self.connection.lock().unwrap(); | ||
| let row: Result<String, _> = conn.query_row( | ||
| &format!("SELECT {} FROM {} WHERE {}=?", | ||
agrinman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| key_db::ACCOUNT_ID, | ||
| key_db::TABLE_NAME, | ||
| key_db::PRIMARY_KEY | ||
| ), | ||
| params![auth_key_hash,], | ||
| |row| row.get(0) | ||
| ); | ||
| Ok(Uuid::from_str(&row.map_err(|_| Error::AccountNotFound)?)?) | ||
| } | ||
|
|
||
| async fn is_account_in_good_standing(&self, account_id: Uuid) -> Result<bool, Error> { | ||
| let conn:&Connection = &*self.connection.lock().unwrap(); | ||
| let row: Result<String, _> = conn.query_row( | ||
| &format!("SELECT {} FROM {} WHERE {}=?", | ||
| record_db::SUBSCRIPTION_ID, | ||
| record_db::TABLE_NAME, | ||
| record_db::PRIMARY_KEY | ||
| ), | ||
| params![account_id.to_string(),], | ||
| |row| row.get(0) | ||
| ); | ||
| Ok(row.map_or_else(|_| false, |_| true)) | ||
| } | ||
|
|
||
| async fn get_account_id_for_subdomain(&self, subdomain: &str) -> Result<Option<Uuid>, Error> { | ||
| let conn:&Connection = &*self.connection.lock().unwrap(); | ||
| let row: Result<String, _> = conn.query_row( | ||
| &format!("SELECT {} FROM {} WHERE {}=?", | ||
| domain_db::ACCOUNT_ID, | ||
| domain_db::TABLE_NAME, | ||
| domain_db::PRIMARY_KEY | ||
| ), | ||
| params![subdomain,], | ||
| |row| row.get(0) | ||
| ); | ||
| let account_str = row.map_or_else(|_| None, |v| Some(v)); | ||
|
|
||
| if let Some(account_str) = account_str { | ||
| Ok(Some(Uuid::from_str(&account_str)?)) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.