Unverified Commit 24a10f95 authored by Nugine's avatar Nugine
Browse files

s3s: auth: refactor

parent 51540bcd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,8 +4,8 @@
use s3s_fs::FileSystem;
use s3s_fs::Result;

use s3s::auth::SimpleAuth;
use s3s::service::S3Service;
use s3s::SimpleAuth;

use std::net::TcpListener;
use std::path::PathBuf;
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ fn config() -> &'static SdkConfig {
            fs::create_dir_all(FS_ROOT).unwrap();
            let fs = s3s_fs::FileSystem::new(FS_ROOT).unwrap();

            let auth = s3s::SimpleAuth::from_single(cred.access_key_id(), cred.secret_access_key());
            let auth = s3s::auth::SimpleAuth::from_single(cred.access_key_id(), cred.secret_access_key());

            let mut service = S3Service::new(Box::new(fs));
            service.set_auth(Box::new(auth));
+1 −1
Original line number Diff line number Diff line
#![forbid(unsafe_code)]
#![deny(clippy::all)]

use s3s::auth::SimpleAuth;
use s3s::service::S3Service;
use s3s::SimpleAuth;

use std::error::Error;
use std::net::TcpListener;
+1 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ time = { version = "0.3.20", features = ["formatting", "parsing", "macros"] }
tracing = "0.1.37"
transform-stream = "0.3.0"
urlencoding = "2.1.2"
zeroize = "1.5.7"

[dev-dependencies]
tokio = { version = "1.26.0", features = ["full"] }
+8 −46
Original line number Diff line number Diff line
//! S3 Authentication

use crate::error::S3Result;
mod secret_key;
pub use self::secret_key::SecretKey;

mod simple_auth;
pub use self::simple_auth::SimpleAuth;

use std::collections::HashMap;
use crate::error::S3Result;

/// S3 Authentication Provider
#[async_trait::async_trait]
pub trait S3Auth: Send + Sync + 'static {
    /// lookup `secret_access` by `access_key`
    async fn get_secret_key(&self, access_key: &str) -> S3Result<String>;
}

/// A simple authentication provider
#[derive(Debug, Default)]
pub struct SimpleAuth {
    /// key map
    map: HashMap<String, String>,
}

impl SimpleAuth {
    /// Constructs a new `SimpleAuth`
    #[must_use]
    pub fn new() -> Self {
        Self { map: HashMap::new() }
    }

    #[must_use]
    pub fn from_single(access_key: impl Into<String>, secret_key: impl Into<String>) -> Self {
        let map = [(access_key.into(), secret_key.into())].into_iter().collect();
        Self { map }
    }

    /// register a credential
    pub fn register(&mut self, access_key: String, secret_key: String) -> Option<String> {
        self.map.insert(access_key, secret_key)
    }

    /// lookup a credential
    #[must_use]
    pub fn lookup(&self, access_key: &str) -> Option<&str> {
        Some(self.map.get(access_key)?.as_str())
    }
}

#[async_trait::async_trait]
impl S3Auth for SimpleAuth {
    async fn get_secret_key(&self, access_key: &str) -> S3Result<String> {
        match self.lookup(access_key) {
            None => Err(s3_error!(NotSignedUp, "Your account is not signed up")),
            Some(s) => Ok(s.to_owned()),
        }
    }
    /// lookup `secret_key` by `access_key`
    async fn get_secret_key(&self, access_key: &str) -> S3Result<SecretKey>;
}
Loading