Skip to content

Commit

Permalink
Follow best practice to improve coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Mar 12, 2024
1 parent 5ea21e1 commit 9c3f601
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
13 changes: 6 additions & 7 deletions src/squire/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub struct AuthToken {
/// # Returns
///
/// Returns a `Result` containing the extracted `Credentials` or an error message if extraction fails.
fn extract_credentials(authorization: Option<&HeaderValue>) -> Result<Credentials, &'static str> {
let header = authorization.unwrap().to_str().unwrap().to_string();
fn extract_credentials(authorization: &HeaderValue) -> Result<Credentials, &'static str> {
let header = authorization.to_str().unwrap().to_string();
// base64 encoded in JavaScript using inbuilt btoa function
let b64_decode_response = squire::secure::base64_decode(&header);
return match b64_decode_response {
Expand Down Expand Up @@ -84,17 +84,16 @@ pub fn verify_login(
config: &web::Data<Arc<squire::settings::Config>>,
session: &web::Data<Arc<constant::Session>>,
) -> Result<HashMap<&'static str, String>, String> {
let authorization = request.headers().get("authorization");
let err_response;
if authorization.is_some() {
if let Some(authorization) = request.headers().get("authorization") {
let extracted_credentials = extract_credentials(authorization);
match extracted_credentials {
Ok(credentials) => {
let password = config.authorization.get(&credentials.username);
if password.is_some() { // Check if the username is present in HashMap as key
// Check if the username is present in HashMap as key
if let Some(password) = config.authorization.get(&credentials.username) {
let message = format!("{}{}{}",
squire::secure::hex_encode(&credentials.username),
squire::secure::hex_encode(password.unwrap()),
squire::secure::hex_encode(password),
credentials.timestamp);
// Create a new signature with hex encoded username and password stored in config file as plain text
let expected_signature = squire::secure::calculate_hash(message);
Expand Down
8 changes: 4 additions & 4 deletions src/templates/landing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,16 @@ pub fn get_content() -> String {
</script>
<script>
function goHome() {
window.location.href = window.location.origin + "/home";
window.location.href = "/home";
}
function goSecure() {
window.location.href = window.location.origin + '/{{ secure_index }}';
window.location.href = '/{{ secure_index }}';
}
function logOut() {
window.location.href = window.location.origin + "/logout";
window.location.href = "/logout";
}
function upload() {
window.location.href = window.location.origin + "/upload";
window.location.href = "/upload";
}
function goBack() {
window.history.back();
Expand Down
8 changes: 4 additions & 4 deletions src/templates/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,16 @@ pub fn get_content() -> String {
<hr>
<script>
function goHome() {
window.location.href = window.location.origin + "/home";
window.location.href = "/home";
}
function goSecure() {
window.location.href = window.location.origin + '/{{ secure_index }}';
window.location.href = '/{{ secure_index }}';
}
function logOut() {
window.location.href = window.location.origin + "/logout";
window.location.href = "/logout";
}
function upload() {
window.location.href = window.location.origin + "/upload";
window.location.href = "/upload";
}
function goBack() {
window.history.back();
Expand Down
8 changes: 4 additions & 4 deletions src/templates/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,16 @@ pub fn get_content() -> String {
</script>
<script>
function goHome() {
window.location.href = window.location.origin + "/home";
window.location.href = "/home";
}
function goSecure() {
window.location.href = window.location.origin + '/{{ secure_index }}';
window.location.href = '/{{ secure_index }}';
}
function logOut() {
window.location.href = window.location.origin + "/logout";
window.location.href = "/logout";
}
function upload() {
window.location.href = window.location.origin + "/upload";
window.location.href = "/upload";
}
function goBack() {
window.history.back();
Expand Down

0 comments on commit 9c3f601

Please sign in to comment.