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,
/// Storage path.
pub upload_path: PathBuf,
/// Authentication token.
pub auth_token: Option<String>,
}
/// Paste configuration.

View file

@ -89,7 +89,17 @@ async fn upload(
) -> Result<HttpResponse, Error> {
let connection = request.connection_info().clone();
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 mut urls: Vec<String> = Vec::new();
while let Some(item) = payload.next().await {