Skip to content

Commit 872edbc

Browse files
committed
pr feedback
1 parent 5ec1e6f commit 872edbc

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

sentry/src/lib.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub mod event_reducer;
3939

4040
pub struct Application<A: Adapter> {
4141
adapter: A,
42-
logger: slog::Logger,
42+
logger: Logger,
4343
redis: SharedConnection,
4444
_clustered: bool,
4545
port: u16,
@@ -73,14 +73,21 @@ impl<A: Adapter + 'static> Application<A> {
7373
let make_service = make_service_fn(move |_| {
7474
let adapter_config = (self.adapter.clone(), self.config.clone());
7575
let redis = self.redis.clone();
76+
let logger = self.logger.clone();
7677
async move {
7778
Ok::<_, Error>(service_fn(move |req| {
7879
let adapter_config = adapter_config.clone();
7980
let redis = redis.clone();
81+
let logger = logger.clone();
8082
async move {
8183
Ok::<_, Error>(
82-
handle_routing(req, (&adapter_config.0, &adapter_config.1), redis)
83-
.await,
84+
handle_routing(
85+
req,
86+
(&adapter_config.0, &adapter_config.1),
87+
redis,
88+
&logger,
89+
)
90+
.await,
8491
)
8592
}
8693
}))
@@ -114,6 +121,7 @@ async fn handle_routing(
114121
req: Request<Body>,
115122
(adapter, config): (&impl Adapter, &Config),
116123
redis: SharedConnection,
124+
logger: &Logger,
117125
) -> Response<Body> {
118126
let headers = match cors(&req) {
119127
Some(Cors::Simple(headers)) => headers,
@@ -124,7 +132,11 @@ async fn handle_routing(
124132

125133
let req = match auth::for_request(req, adapter, redis.clone()).await {
126134
Ok(req) => req,
127-
Err(response_error) => return map_response_error(response_error),
135+
Err(error) => {
136+
error!(&logger, "{}", &error; "module" => "middleware-auth");
137+
138+
return map_response_error(ResponseError::BadRequest(error));
139+
}
128140
};
129141

130142
let mut response = match (req.uri().path(), req.method()) {
@@ -155,8 +167,8 @@ pub fn not_found() -> Response<Body> {
155167
response
156168
}
157169

158-
pub fn bad_request(error: Box<dyn std::error::Error>) -> Response<Body> {
159-
let body = Body::from(format!("Bad Request: {}", error));
170+
pub fn bad_request(_: Box<dyn std::error::Error>) -> Response<Body> {
171+
let body = Body::from("Bad Request: try again later");
160172
let mut response = Response::new(body);
161173
let status = response.status_mut();
162174
*status = StatusCode::BAD_REQUEST;

sentry/src/middleware/auth.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
use std::error;
2+
13
use hyper::header::AUTHORIZATION;
24
use hyper::{Body, Request};
35
use redis::aio::SharedConnection;
46

57
use primitives::adapter::{Adapter, Session as AdapterSession};
68

7-
use crate::{ResponseError, Session};
9+
use crate::Session;
810

911
/// Check `Authorization` header for `Bearer` scheme with `Adapter::session_from_token`.
1012
/// If the `Adapter` fails to create an `AdapterSession`, `ResponseError::BadRequest` will be returned.
1113
pub(crate) async fn for_request(
1214
mut req: Request<Body>,
1315
adapter: &impl Adapter,
1416
redis: SharedConnection,
15-
) -> Result<Request<Body>, ResponseError> {
17+
) -> Result<Request<Body>, Box<dyn error::Error>> {
1618
let authorization = req.headers().get(AUTHORIZATION);
1719

1820
let prefix = "Bearer ";
@@ -36,16 +38,8 @@ pub(crate) async fn for_request(
3638
.arg(token)
3739
.query_async::<_, Option<String>>(&mut redis.clone())
3840
.await?
39-
.and_then(|session_str| {
40-
match serde_json::from_str::<AdapterSession>(&session_str) {
41-
Ok(session) => Some(session),
42-
Err(serde_error) => {
43-
// log message instead
44-
println!("{}", serde_error);
45-
None
46-
}
47-
}
48-
}) {
41+
.and_then(|session_str| serde_json::from_str::<AdapterSession>(&session_str).ok())
42+
{
4943
Some(adapter_session) => adapter_session,
5044
None => {
5145
// If there was a problem with the Session or the Token, this will error
@@ -85,14 +79,17 @@ fn get_request_ip(req: &Request<Body>) -> Option<String> {
8579

8680
#[cfg(test)]
8781
mod test {
88-
use super::*;
89-
use crate::db::redis_connection;
90-
use adapter::DummyAdapter;
9182
use hyper::Request;
83+
84+
use adapter::DummyAdapter;
9285
use primitives::adapter::DummyAdapterOptions;
9386
use primitives::config::configuration;
9487
use primitives::util::tests::prep_db::{AUTH, IDS};
9588

89+
use crate::db::redis_connection;
90+
91+
use super::*;
92+
9693
async fn setup() -> (DummyAdapter, SharedConnection) {
9794
let adapter_options = DummyAdapterOptions {
9895
dummy_identity: IDS["leader"].clone(),
@@ -143,9 +140,9 @@ mod test {
143140
.body(Body::empty())
144141
.unwrap();
145142
match for_request(non_existent_token_req, &dummy_adapter, redis).await {
146-
Err(ResponseError::BadRequest(error)) => {
143+
Err(error) => {
147144
assert!(error.to_string().contains("no session token for this auth: wrong-token"), "Wrong error received");
148-
},
145+
}
149146
_ => panic!("We shouldn't get a success response nor a different Error than BadRequest for this call"),
150147
};
151148
}

0 commit comments

Comments
 (0)