Skip to content

Hold onto authorization header with ApiToken source #1599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into api-token-auth-he…
…ader
  • Loading branch information
carols10cents committed Oct 4, 2019
commit dec4383f4241e33de58ed0b9b612a14c46c7267f
13 changes: 9 additions & 4 deletions src/middleware/current_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,21 @@ impl Middleware for CurrentUser {
} else {
// Otherwise, look for an `Authorization` header on the request
// and try to find a user in the database with a matching API token
let user_auth = req.headers().find("Authorization").and_then(|headers| {
let user_auth = if let Some(headers) = req.headers().find("Authorization") {
let auth_header = headers[0].to_string();

User::find_by_api_token(&conn, &auth_header)
.ok()
.map(|user| (AuthenticationSource::ApiToken { auth_header }, user))
});
.optional()
.map_err(|e| return Box::new(e) as Box<dyn Error + Send>)?
} else {
None
};

drop(conn);

if let Some((api_token, user)) = user_auth {
// Attach the `User` model from the database to the request
// Attach the `User` model from the database and the API token to the request
req.mut_extensions().insert(user);
req.mut_extensions().insert(api_token);
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.