Skip to content

Commit f282a2a

Browse files
Thom van Kalkerenjoepio
authored andcommitted
#525 authenticate headers over cookies
1 parent f0f309c commit f282a2a

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

server/src/helpers.rs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use actix_web::cookie::Cookie;
44
use actix_web::http::header::{HeaderMap, HeaderValue};
55
use atomic_lib::authentication::AuthValues;
66
use percent_encoding::percent_decode_str;
7+
use std::str::FromStr;
78

89
use crate::errors::{AppErrorType, AtomicServerError};
910
use crate::{appstate::AppState, content_types::ContentType, errors::AtomicServerResult};
@@ -62,39 +63,60 @@ pub fn get_auth_headers(
6263
pub fn get_auth_from_cookie(
6364
map: &HeaderMap,
6465
requested_subject: &String,
65-
) -> Option<AtomicServerResult<Option<AuthValues>>> {
66-
let encoded_session = session_cookie_from_header(map.get("Cookie")?)?;
66+
) -> AtomicServerResult<Option<AuthValues>> {
67+
let encoded_session = match map.get("Cookie") {
68+
Some(cookies) => session_cookie_from_header(cookies),
69+
None => return Ok(None),
70+
};
6771

68-
let session = base64::decode(encoded_session).ok()?;
69-
let session_str = std::str::from_utf8(&session).ok()?;
70-
let values: Result<AuthValues, AtomicServerError> =
71-
serde_json::from_str(session_str).map_err(|_| AtomicServerError {
72+
let session = match encoded_session {
73+
Some(s) => base64::decode(s).map_err(|_| AtomicServerError {
7274
message: "Malformed authentication resource".to_string(),
7375
error_type: AppErrorType::Unauthorized,
7476
error_resource: None,
75-
});
77+
}),
78+
None => return Ok(None),
79+
}?;
7680

77-
if let Ok(auth_values) = values {
78-
if auth_values.requested_subject.eq(requested_subject) {
79-
return Some(Err(AtomicServerError {
80-
message: "Wrong requested subject".to_string(),
81-
error_type: AppErrorType::Unauthorized,
82-
error_resource: None,
83-
}));
84-
}
81+
let session_str = std::str::from_utf8(&session).map_err(|_| AtomicServerError {
82+
message: "Malformed authentication resource".to_string(),
83+
error_type: AppErrorType::Unauthorized,
84+
error_resource: None,
85+
})?;
86+
let auth_values: AuthValues =
87+
serde_json::from_str(session_str).map_err(|_| AtomicServerError {
88+
message: "Malformed authentication resource".to_string(),
89+
error_type: AppErrorType::Unauthorized,
90+
error_resource: None,
91+
})?;
8592

86-
Some(Ok(Some(auth_values)))
87-
} else {
88-
Some(Err(values.err().unwrap()))
93+
if auth_values.requested_subject.ne(requested_subject) {
94+
return Err(AtomicServerError {
95+
message: format!(
96+
"Wrong requested subject, expected {} was {}",
97+
requested_subject, auth_values.requested_subject
98+
),
99+
error_type: AppErrorType::Unauthorized,
100+
error_resource: None,
101+
});
89102
}
103+
104+
Ok(Some(auth_values))
90105
}
91106

92107
pub fn get_auth(
93108
map: &HeaderMap,
94109
requested_subject: String,
95110
) -> AtomicServerResult<Option<AuthValues>> {
96-
let cookie_result = get_auth_from_cookie(map, &requested_subject);
97-
cookie_result.unwrap_or_else(|| get_auth_headers(map, requested_subject))
111+
let from_header = match get_auth_headers(map, requested_subject.clone()) {
112+
Ok(res) => res,
113+
Err(err) => return Err(err),
114+
};
115+
116+
match from_header {
117+
Some(v) => Ok(Some(v)),
118+
None => get_auth_from_cookie(map, &requested_subject),
119+
}
98120
}
99121

100122
/// Checks for authentication headers and returns Some agent's subject if everything is well.

0 commit comments

Comments
 (0)