Skip to content

Instantly share code, notes, and snippets.

@trimoq
Created October 21, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trimoq/ea008739febd0bc734ca4abbb25df3ed to your computer and use it in GitHub Desktop.
Save trimoq/ea008739febd0bc734ca4abbb25df3ed to your computer and use it in GitHub Desktop.
impl<'a, 'r> FromRequest<'a, 'r> for AuthUser {
type Error = ApiError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<AuthUser, Self::Error> {
let con = request.guard::<DbConn>().unwrap();
request.cookies()
.get_private("user_id")
.and_then(|cookie| cookie.value().parse().ok())
.and_then(|id| ApiUserDAO::get_by_slug_enabled(&con,id))
.or_else( || {
request.headers().get_one("x-api-key")
.and_then(|header| TokenDAO::get_user_for_token_or_err(&con,header.to_string()).ok())
})
.into_outcome((Status::Unauthorized,ApiError::unauthorized("unauthorized")))
.map(|user| AuthUser(user))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment