Skip to content

Fix auth #2167

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
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 0 additions & 11 deletions src/controllers/user/me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ use crate::views::{EncodableMe, EncodableVersion, OwnedCrate};

/// Handles the `GET /me` route.
pub fn me(req: &mut dyn Request) -> AppResult<Response> {
// Changed to getting User information from database because in
// src/tests/user.rs, when testing put and get on updating email,
// request seems to be somehow 'cached'. When we try to get a
// request from the /me route with the just updated user (call
// this function) the user is the same as the initial GET request
// and does not seem to get the updated user information from the
// database
// This change is not preferable, we'd rather fix the request,
// perhaps adding `req.mut_extensions().insert(user)` to the
// update_user route, however this somehow does not seem to work

let conn = req.db_conn()?;
let user_id = req.authenticate(&conn)?.user_id();

Expand Down
11 changes: 8 additions & 3 deletions src/controllers/user/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use conduit_cookie::RequestSession;
use failure::Fail;
use oauth2::{prelude::*, AuthorizationCode, TokenResponse};

use crate::middleware::current_user::TrustedUserId;
use crate::models::{NewUser, User};
use crate::schema::users;
use crate::util::errors::ReadOnlyMode;
Expand Down Expand Up @@ -88,8 +89,7 @@ pub fn authorize(req: &mut dyn Request) -> AppResult<Response> {
}
}

// Fetch the access token from github using the code we just got

// Fetch the access token from GitHub using the code we just got
let code = AuthorizationCode::new(code);
let token = req
.app()
Expand All @@ -98,11 +98,16 @@ pub fn authorize(req: &mut dyn Request) -> AppResult<Response> {
.map_err(|e| e.compat())
.chain_error(|| server_error("Error obtaining token"))?;
let token = token.access_token();

// Fetch the user info from GitHub using the access token we just got and create a user record
let ghuser = github::github_api::<GithubUser>(req.app(), "/user", token)?;
let user = ghuser.save_to_database(&token.secret(), &*req.db_conn()?)?;

// Log in by setting a cookie and the middleware authentication
req.session()
.insert("user_id".to_string(), user.id.to_string());
req.mut_extensions().insert(user);
req.mut_extensions().insert(TrustedUserId(user.id));

super::me::me(req)
}

Expand Down