use std::collections::HashMap;
use crate::core::types::ExchangeResult;
#[derive(Clone)]
pub struct Credentials {
pub api_key: String,
pub api_secret: String,
pub passphrase: Option<String>,
pub testnet: bool,
}
impl Credentials {
pub fn new(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
Self {
api_key: api_key.into(),
api_secret: api_secret.into(),
passphrase: None,
testnet: false,
}
}
pub fn with_passphrase(mut self, passphrase: impl Into<String>) -> Self {
self.passphrase = Some(passphrase.into());
self
}
pub fn with_testnet(mut self, testnet: bool) -> Self {
self.testnet = testnet;
self
}
}
pub struct AuthRequest<'a> {
pub method: &'a str,
pub path: &'a str,
pub query: Option<&'a str>,
pub body: Option<&'a str>,
pub headers: HashMap<String, String>,
pub query_params: HashMap<String, String>,
}
impl<'a> AuthRequest<'a> {
pub fn new(method: &'a str, path: &'a str) -> Self {
Self {
method,
path,
query: None,
body: None,
headers: HashMap::new(),
query_params: HashMap::new(),
}
}
pub fn with_query(mut self, query: &'a str) -> Self {
self.query = Some(query);
self
}
pub fn with_body(mut self, body: &'a str) -> Self {
self.body = Some(body);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignatureLocation {
Headers,
QueryParams,
}
pub trait ExchangeAuth: Send + Sync {
fn sign_request(
&self,
credentials: &Credentials,
req: &mut AuthRequest<'_>,
) -> ExchangeResult<()>;
fn signature_location(&self) -> SignatureLocation {
SignatureLocation::Headers
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CredentialKind {
HmacSha256,
HmacWithPassphrase,
HmacSha512,
HmacSha384,
JwtEs256,
JwtHmac,
OAuth2,
EthereumWallet,
SolanaKeypair,
StarkKey,
CosmosWallet,
}
pub trait Authenticated: Send + Sync {
fn set_credentials(&mut self, creds: crate::core::types::ExchangeCredentials);
fn is_authenticated(&self) -> bool;
fn credential_type(&self) -> Option<CredentialKind>;
}