-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from near/serhii/OAuth
OAuth mock
- Loading branch information
Showing
8 changed files
with
534 additions
and
93 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod oauth; | ||
|
||
use clap::Parser; | ||
use threshold_crypto::{serde_impl::SerdeSecret, PublicKeySet, SecretKeyShare}; | ||
|
||
|
This file contains 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 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,115 @@ | ||
#[async_trait::async_trait] | ||
pub trait OAuthTokenVerifier { | ||
async fn verify_token(token: &str) -> Option<&str>; | ||
} | ||
|
||
pub enum SupportedTokenVerifiers { | ||
GoogleTokenVerifier, | ||
TestTokenVerifier, | ||
} | ||
|
||
/* Universal token verifier */ | ||
pub struct UniversalTokenVerifier {} | ||
|
||
#[async_trait::async_trait] | ||
impl OAuthTokenVerifier for UniversalTokenVerifier { | ||
async fn verify_token(token: &str) -> Option<&str> { | ||
// TODO: here we assume that verifier type can be determined from the token | ||
match get_token_verifier_type(token) { | ||
SupportedTokenVerifiers::GoogleTokenVerifier => { | ||
return GoogleTokenVerifier::verify_token(token).await; | ||
} | ||
SupportedTokenVerifiers::TestTokenVerifier => { | ||
return TestTokenVerifier::verify_token(token).await; | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn get_token_verifier_type(token: &str) -> SupportedTokenVerifiers { | ||
match token.len() { | ||
// TODO: add real token type detection | ||
0 => { | ||
tracing::info!("Using GoogleTokenVerifier"); | ||
SupportedTokenVerifiers::GoogleTokenVerifier | ||
} | ||
_ => { | ||
tracing::info!("Using TestTokenVerifier"); | ||
SupportedTokenVerifiers::TestTokenVerifier | ||
} | ||
} | ||
} | ||
|
||
/* Google verifier */ | ||
pub struct GoogleTokenVerifier {} | ||
|
||
#[async_trait::async_trait] | ||
impl OAuthTokenVerifier for GoogleTokenVerifier { | ||
// TODO: replace with real implementation | ||
async fn verify_token(token: &str) -> Option<&str> { | ||
match token { | ||
"validToken" => { | ||
tracing::info!("GoogleTokenVerifier: access token is valid"); | ||
Some("testAccountId") | ||
} | ||
_ => { | ||
tracing::info!("GoogleTokenVerifier: access token verification failed"); | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* Test verifier */ | ||
pub struct TestTokenVerifier {} | ||
|
||
#[async_trait::async_trait] | ||
impl OAuthTokenVerifier for TestTokenVerifier { | ||
async fn verify_token(token: &str) -> Option<&str> { | ||
match token { | ||
"validToken" => { | ||
tracing::info!("TestTokenVerifier: access token is valid"); | ||
Some("testAccountId") | ||
} | ||
_ => { | ||
tracing::info!("TestTokenVerifier: access token verification failed"); | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_verify_token_valid() { | ||
let token = "validToken"; | ||
let account_id = TestTokenVerifier::verify_token(token).await.unwrap(); | ||
assert_eq!(account_id, "testAccountId"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_verify_token_invalid_with_test_verifier() { | ||
let token = "invalid"; | ||
let account_id = TestTokenVerifier::verify_token(token).await; | ||
assert_eq!(account_id, None); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_verify_token_valid_with_test_verifier() { | ||
let token = "validToken"; | ||
let account_id = TestTokenVerifier::verify_token(token).await.unwrap(); | ||
assert_eq!(account_id, "testAccountId"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_verify_token_invalid_with_universal_verifier() { | ||
let token = "invalid"; | ||
let account_id = UniversalTokenVerifier::verify_token(token).await; | ||
assert_eq!(account_id, None); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_verify_token_valid_with_universal_verifier() { | ||
let token = "validToken"; | ||
let account_id = UniversalTokenVerifier::verify_token(token).await.unwrap(); | ||
assert_eq!(account_id, "testAccountId"); | ||
} |
This file contains 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