feat(config): support setting the auth token in config

This commit is contained in:
Orhun Parmaksız 2022-02-24 23:51:47 +03:00
parent 53686c6c93
commit 3cc40deca0
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
2 changed files with 13 additions and 1 deletions

View File

@ -24,6 +24,8 @@ pub struct ServerConfig {
pub max_content_length: Byte, pub max_content_length: Byte,
/// Storage path. /// Storage path.
pub upload_path: PathBuf, pub upload_path: PathBuf,
/// Authentication token.
pub auth_token: Option<String>,
} }
/// Paste configuration. /// Paste configuration.

View File

@ -89,7 +89,17 @@ async fn upload(
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let connection = request.connection_info().clone(); let connection = request.connection_info().clone();
let host = connection.remote_addr().unwrap_or("unknown host"); let host = connection.remote_addr().unwrap_or("unknown host");
auth::check(host, request.headers(), env::var("AUTH_TOKEN").ok())?; auth::check(
host,
request.headers(),
env::var("AUTH_TOKEN").ok().or(config
.read()
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?
.server
.auth_token
.as_ref()
.cloned()),
)?;
let expiry_date = header::parse_expiry_date(request.headers())?; let expiry_date = header::parse_expiry_date(request.headers())?;
let mut urls: Vec<String> = Vec::new(); let mut urls: Vec<String> = Vec::new();
while let Some(item) = payload.next().await { while let Some(item) = payload.next().await {