Skip to content

Commit 49a42a7

Browse files
committed
perf: Set auth header for UI in a conventional way
1 parent 8892757 commit 49a42a7

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/templates/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@
563563
const errorMsg = document.getElementById('loginError');
564564
const username = document.getElementById('username').value.trim();
565565
const password = document.getElementById('password').value;
566-
const authHeaderValue = username + ',' + password;
567566

568567
btn.disabled = true;
569568
btn.textContent = 'Authenticating…';
@@ -574,7 +573,7 @@
574573
method: 'POST',
575574
headers: {
576575
'Content-Type': 'application/json',
577-
'Authorization': btoa(authHeaderValue)
576+
'Authorization': 'Basic ' + btoa(username + ':' + password)
578577
},
579578
});
580579

src/ui.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,34 @@ pub async fn authenticator(
7171
return HttpResponse::Unauthorized().finish()
7272
},
7373
};
74-
let encoded = match auth_header.to_str() {
74+
let auth_header = match auth_header.to_str() {
7575
Ok(header) => header,
7676
Err(err) => {
7777
log::warn!("{}", err);
7878
return HttpResponse::Unauthorized().finish()
7979
},
8080
};
81+
let encoded = match auth_header.strip_prefix("Basic ") {
82+
Some(value) => value,
83+
None => {
84+
log::warn!("Authorization header missing Basic prefix");
85+
return HttpResponse::Unauthorized().finish()
86+
},
87+
};
8188
let decoded = match base64_decode(encoded) {
8289
Ok(decoded_) => decoded_,
8390
Err(err) => {
8491
log::warn!("{}", err);
8592
return HttpResponse::Unauthorized().finish()
8693
},
8794
};
88-
let auth_header = decoded.split(",").collect::<Vec<&str>>();
89-
if auth_header.len() != 2 {
95+
let auth_parts = decoded.splitn(2, ':').collect::<Vec<&str>>();
96+
if auth_parts.len() != 2 {
9097
log::warn!("Expected two Authorization headers, received {:?}", auth_header);
9198
return HttpResponse::Unauthorized().finish();
9299
}
93-
let username = auth_header.first().unwrap().to_string();
94-
let password = auth_header.last().unwrap().to_string();
100+
let username = auth_parts[0].to_string();
101+
let password = auth_parts[1].to_string();
95102
if username == config.username && password == config.password {
96103
return HttpResponse::Ok().json(json!({ "apikey": config.apikey }))
97104
}

0 commit comments

Comments
 (0)