mirror of
https://github.com/zerotier/ratpack.git
synced 2026-05-22 16:27:23 -07:00
@@ -49,22 +49,12 @@ async fn validate_authtoken(
|
||||
app: App<State, AuthedState>,
|
||||
mut authstate: AuthedState,
|
||||
) -> HTTPResult<AuthedState> {
|
||||
let token = req.headers().get("X-AuthToken");
|
||||
if token.is_none() {
|
||||
return Err(Error::StatusCode(StatusCode::UNAUTHORIZED));
|
||||
if let (Some(token), Some(state)) = (req.headers().get("X-AuthToken"), app.state().await) {
|
||||
authstate.authed = Some(state.clone().lock().await.authtoken == token);
|
||||
Ok((req, resp, authstate))
|
||||
} else {
|
||||
Err(Error::StatusCode(StatusCode::UNAUTHORIZED))
|
||||
}
|
||||
|
||||
let token = token.unwrap();
|
||||
|
||||
let state = app.state().await;
|
||||
if state.is_none() {
|
||||
return Err(Error::StatusCode(StatusCode::UNAUTHORIZED));
|
||||
}
|
||||
|
||||
let state = state.unwrap();
|
||||
authstate.authed = Some(state.clone().lock().await.authtoken == token);
|
||||
|
||||
return Ok((req, resp, authstate));
|
||||
}
|
||||
|
||||
// our `hello` responder; it simply echoes the `name` parameter provided in the
|
||||
@@ -76,18 +66,26 @@ async fn hello(
|
||||
_app: App<State, AuthedState>,
|
||||
authstate: AuthedState,
|
||||
) -> HTTPResult<AuthedState> {
|
||||
if authstate.authed.is_some() && !authstate.authed.unwrap() {
|
||||
return Err(Error::StatusCode(StatusCode::UNAUTHORIZED));
|
||||
}
|
||||
|
||||
let name = params.get("name").unwrap();
|
||||
let bytes = Body::from(format!("hello, {}!\n", name));
|
||||
|
||||
return Ok((
|
||||
req,
|
||||
Some(Response::builder().status(200).body(bytes).unwrap()),
|
||||
authstate,
|
||||
));
|
||||
if let Some(authed) = authstate.authed {
|
||||
if authed {
|
||||
return Ok((
|
||||
req,
|
||||
Some(Response::builder().status(200).body(bytes).unwrap()),
|
||||
authstate,
|
||||
));
|
||||
}
|
||||
} else if authstate.authed.is_none() {
|
||||
return Ok((
|
||||
req,
|
||||
Some(Response::builder().status(200).body(bytes).unwrap()),
|
||||
authstate,
|
||||
));
|
||||
}
|
||||
|
||||
Err(Error::StatusCode(StatusCode::UNAUTHORIZED))
|
||||
}
|
||||
|
||||
// Our global application state; must be `Clone`.
|
||||
|
||||
Reference in New Issue
Block a user