Commit 174b27dd authored by achuith's avatar achuith Committed by Commit bot

Fetch policy with refresh token.

WildcardLoginChecker and UserCloudPolicyManagerChromeOS now use this mechanism to fetch policy.

BUG=478432,470984,462036,480447
TEST=manual

Review URL: https://codereview.chromium.org/1108983002

Cr-Commit-Position: refs/heads/master@{#327375}
parent b59e330a
...@@ -94,6 +94,7 @@ bool ChromeLoginPerformer::IsUserWhitelisted(const std::string& user_id, ...@@ -94,6 +94,7 @@ bool ChromeLoginPerformer::IsUserWhitelisted(const std::string& user_id,
void ChromeLoginPerformer::RunOnlineWhitelistCheck( void ChromeLoginPerformer::RunOnlineWhitelistCheck(
const std::string& user_id, const std::string& user_id,
bool wildcard_match, bool wildcard_match,
const std::string& refresh_token,
const base::Closure& success_callback, const base::Closure& success_callback,
const base::Closure& failure_callback) { const base::Closure& failure_callback) {
// On enterprise devices, reconfirm login permission with the server. // On enterprise devices, reconfirm login permission with the server.
...@@ -102,12 +103,19 @@ void ChromeLoginPerformer::RunOnlineWhitelistCheck( ...@@ -102,12 +103,19 @@ void ChromeLoginPerformer::RunOnlineWhitelistCheck(
if (connector->IsEnterpriseManaged() && wildcard_match && if (connector->IsEnterpriseManaged() && wildcard_match &&
!connector->IsNonEnterpriseUser(user_id)) { !connector->IsNonEnterpriseUser(user_id)) {
wildcard_login_checker_.reset(new policy::WildcardLoginChecker()); wildcard_login_checker_.reset(new policy::WildcardLoginChecker());
wildcard_login_checker_->Start( if (refresh_token.empty()) {
ProfileHelper::GetSigninProfile()->GetRequestContext(), wildcard_login_checker_->Start(
base::Bind(&ChromeLoginPerformer::OnlineWildcardLoginCheckCompleted, ProfileHelper::GetSigninProfile()->GetRequestContext(),
weak_factory_.GetWeakPtr(), base::Bind(&ChromeLoginPerformer::OnlineWildcardLoginCheckCompleted,
success_callback, weak_factory_.GetWeakPtr(), success_callback,
failure_callback)); failure_callback));
} else {
wildcard_login_checker_->StartWithRefreshToken(
refresh_token,
base::Bind(&ChromeLoginPerformer::OnlineWildcardLoginCheckCompleted,
weak_factory_.GetWeakPtr(), success_callback,
failure_callback));
}
} else { } else {
success_callback.Run(); success_callback.Run();
} }
......
...@@ -42,6 +42,7 @@ class ChromeLoginPerformer : public LoginPerformer { ...@@ -42,6 +42,7 @@ class ChromeLoginPerformer : public LoginPerformer {
void RunOnlineWhitelistCheck(const std::string& user_id, void RunOnlineWhitelistCheck(const std::string& user_id,
bool wildcard_match, bool wildcard_match,
const std::string& refresh_token,
const base::Closure& success_callback, const base::Closure& success_callback,
const base::Closure& failure_callback) override; const base::Closure& failure_callback) override;
bool AreSupervisedUsersAllowed() override; bool AreSupervisedUsersAllowed() override;
......
...@@ -230,6 +230,7 @@ class UserSessionManager ...@@ -230,6 +230,7 @@ class UserSessionManager
// Removes a profile from the per-user input methods states map. // Removes a profile from the per-user input methods states map.
void RemoveProfileForTesting(Profile* profile); void RemoveProfileForTesting(Profile* profile);
const UserContext& user_context() const { return user_context_; }
bool has_auth_cookies() const { return has_auth_cookies_; } bool has_auth_cookies() const { return has_auth_cookies_; }
private: private:
......
...@@ -37,8 +37,6 @@ PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher( ...@@ -37,8 +37,6 @@ PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher(
const TokenCallback& callback) const TokenCallback& callback)
: auth_context_getter_(auth_context_getter), : auth_context_getter_(auth_context_getter),
system_context_getter_(system_context_getter), system_context_getter_(system_context_getter),
retry_count_(0),
failed_(false),
callback_(callback) {} callback_(callback) {}
PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher( PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher(
...@@ -47,8 +45,6 @@ PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher( ...@@ -47,8 +45,6 @@ PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher(
const TokenCallback& callback) const TokenCallback& callback)
: auth_code_(auth_code), : auth_code_(auth_code),
system_context_getter_(system_context_getter), system_context_getter_(system_context_getter),
retry_count_(0),
failed_(false),
callback_(callback) { callback_(callback) {
} }
...@@ -59,6 +55,13 @@ void PolicyOAuth2TokenFetcher::Start() { ...@@ -59,6 +55,13 @@ void PolicyOAuth2TokenFetcher::Start() {
StartFetchingRefreshToken(); StartFetchingRefreshToken();
} }
void PolicyOAuth2TokenFetcher::StartWithRefreshToken(
const std::string& oauth2_refresh_token) {
retry_count_ = 0;
oauth2_refresh_token_ = oauth2_refresh_token;
StartFetchingAccessToken();
}
void PolicyOAuth2TokenFetcher::StartFetchingRefreshToken() { void PolicyOAuth2TokenFetcher::StartFetchingRefreshToken() {
if (auth_code_.empty()) { if (auth_code_.empty()) {
refresh_token_fetcher_.reset(new GaiaAuthFetcher( refresh_token_fetcher_.reset(new GaiaAuthFetcher(
......
...@@ -53,6 +53,10 @@ class PolicyOAuth2TokenFetcher ...@@ -53,6 +53,10 @@ class PolicyOAuth2TokenFetcher
// Starts process of minting device management service OAuth2 access token. // Starts process of minting device management service OAuth2 access token.
void Start(); void Start();
// Starts minting device management service OAuth2 access token with the given
// |oauth2_refresh_token|.
void StartWithRefreshToken(const std::string& oauth2_refresh_token);
// Returns true if we have previously attempted to fetch tokens with this // Returns true if we have previously attempted to fetch tokens with this
// class and failed. // class and failed.
bool failed() const { bool failed() const {
...@@ -110,10 +114,10 @@ class PolicyOAuth2TokenFetcher ...@@ -110,10 +114,10 @@ class PolicyOAuth2TokenFetcher
std::string oauth2_access_token_; std::string oauth2_access_token_;
// The retry counter. Increment this only when failure happened. // The retry counter. Increment this only when failure happened.
int retry_count_; int retry_count_ = 0;
// True if we have already failed to fetch the policy. // True if we have already failed to fetch the policy.
bool failed_; bool failed_ = false;
// The callback to invoke when done. // The callback to invoke when done.
TokenCallback callback_; TokenCallback callback_;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/login/helper.h" #include "chrome/browser/chromeos/login/helper.h"
#include "chrome/browser/chromeos/login/session/user_session_manager.h"
#include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h" #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
#include "chrome/browser/chromeos/policy/wildcard_login_checker.h" #include "chrome/browser/chromeos/policy/wildcard_login_checker.h"
...@@ -212,7 +213,7 @@ void UserCloudPolicyManagerChromeOS::OnInitializationCompleted( ...@@ -212,7 +213,7 @@ void UserCloudPolicyManagerChromeOS::OnInitializationCompleted(
// access token is already available. // access token is already available.
if (!client()->is_registered()) { if (!client()->is_registered()) {
if (wait_for_policy_fetch_) { if (wait_for_policy_fetch_) {
FetchPolicyOAuthTokenUsingSigninContext(); FetchPolicyOAuthToken();
} else if (!access_token_.empty()) { } else if (!access_token_.empty()) {
OnAccessTokenAvailable(access_token_); OnAccessTokenAvailable(access_token_);
} }
...@@ -286,7 +287,19 @@ void UserCloudPolicyManagerChromeOS::GetChromePolicy(PolicyMap* policy_map) { ...@@ -286,7 +287,19 @@ void UserCloudPolicyManagerChromeOS::GetChromePolicy(PolicyMap* policy_map) {
SetEnterpriseUsersDefaults(policy_map); SetEnterpriseUsersDefaults(policy_map);
} }
void UserCloudPolicyManagerChromeOS::FetchPolicyOAuthTokenUsingSigninContext() { void UserCloudPolicyManagerChromeOS::FetchPolicyOAuthToken() {
const std::string& refresh_token = chromeos::UserSessionManager::GetInstance()
->user_context()
.GetRefreshToken();
if (!refresh_token.empty()) {
token_fetcher_.reset(new PolicyOAuth2TokenFetcher(
std::string(), g_browser_process->system_request_context(),
base::Bind(&UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched,
base::Unretained(this))));
token_fetcher_->StartWithRefreshToken(refresh_token);
return;
}
scoped_refptr<net::URLRequestContextGetter> signin_context = scoped_refptr<net::URLRequestContextGetter> signin_context =
chromeos::login::GetSigninContext(); chromeos::login::GetSigninContext();
if (!signin_context.get()) { if (!signin_context.get()) {
......
...@@ -109,9 +109,10 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager, ...@@ -109,9 +109,10 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager,
void GetChromePolicy(PolicyMap* policy_map) override; void GetChromePolicy(PolicyMap* policy_map) override;
private: private:
// Fetches a policy token using the authentication context of the signin // Fetches a policy token using the refresh token if available, or the
// context, and calls back to OnOAuth2PolicyTokenFetched when done. // authentication context of the signin context, and calls back
void FetchPolicyOAuthTokenUsingSigninContext(); // OnOAuth2PolicyTokenFetched when done.
void FetchPolicyOAuthToken();
// Called once the policy access token is available, and starts the // Called once the policy access token is available, and starts the
// registration with the policy server if the token was successfully fetched. // registration with the policy server if the token was successfully fetched.
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h" #include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/prefs/browser_prefs.h"
...@@ -36,6 +37,7 @@ ...@@ -36,6 +37,7 @@
#include "components/policy/core/common/schema_registry.h" #include "components/policy/core/common/schema_registry.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h" #include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
#include "components/user_manager/fake_user_manager.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/gaia/gaia_auth_consumer.h" #include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/gaia_constants.h"
...@@ -80,7 +82,9 @@ class UserCloudPolicyManagerChromeOSTest : public testing::Test { ...@@ -80,7 +82,9 @@ class UserCloudPolicyManagerChromeOSTest : public testing::Test {
external_data_manager_(NULL), external_data_manager_(NULL),
task_runner_(new base::TestSimpleTaskRunner()), task_runner_(new base::TestSimpleTaskRunner()),
profile_(NULL), profile_(NULL),
signin_profile_(NULL) {} signin_profile_(NULL),
user_manager_(new user_manager::FakeUserManager()),
user_manager_enabler_(user_manager_) {}
void SetUp() override { void SetUp() override {
// The initialization path that blocks on the initial policy fetch requires // The initialization path that blocks on the initial policy fetch requires
...@@ -342,6 +346,9 @@ class UserCloudPolicyManagerChromeOSTest : public testing::Test { ...@@ -342,6 +346,9 @@ class UserCloudPolicyManagerChromeOSTest : public testing::Test {
TestingProfile* profile_; TestingProfile* profile_;
TestingProfile* signin_profile_; TestingProfile* signin_profile_;
user_manager::FakeUserManager* user_manager_;
chromeos::ScopedUserManagerEnabler user_manager_enabler_;
private: private:
DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest); DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest);
}; };
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/login/session/user_session_manager.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "chrome/browser/chromeos/policy/user_cloud_external_data_manager.h" #include "chrome/browser/chromeos/policy/user_cloud_external_data_manager.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
...@@ -149,10 +148,8 @@ scoped_ptr<UserCloudPolicyManagerChromeOS> ...@@ -149,10 +148,8 @@ scoped_ptr<UserCloudPolicyManagerChromeOS>
const bool is_affiliated_user = affiliation == USER_AFFILIATION_MANAGED; const bool is_affiliated_user = affiliation == USER_AFFILIATION_MANAGED;
const bool is_browser_restart = const bool is_browser_restart =
command_line->HasSwitch(chromeos::switches::kLoginUser); command_line->HasSwitch(chromeos::switches::kLoginUser);
// TODO(xiyuan): Update the code below after http://crbug.com/462036.
const bool wait_for_initial_policy = const bool wait_for_initial_policy =
!is_browser_restart && !is_browser_restart &&
chromeos::UserSessionManager::GetInstance()->has_auth_cookies() &&
(user_manager::UserManager::Get()->IsCurrentUserNew() || (user_manager::UserManager::Get()->IsCurrentUserNew() ||
is_affiliated_user); is_affiliated_user);
...@@ -204,14 +201,11 @@ scoped_ptr<UserCloudPolicyManagerChromeOS> ...@@ -204,14 +201,11 @@ scoped_ptr<UserCloudPolicyManagerChromeOS>
content::BrowserThread::FILE); content::BrowserThread::FILE);
scoped_ptr<UserCloudPolicyManagerChromeOS> manager( scoped_ptr<UserCloudPolicyManagerChromeOS> manager(
new UserCloudPolicyManagerChromeOS(store.Pass(), new UserCloudPolicyManagerChromeOS(
external_data_manager.Pass(), store.Pass(), external_data_manager.Pass(),
component_policy_cache_dir, component_policy_cache_dir, wait_for_initial_policy,
wait_for_initial_policy, initial_policy_fetch_timeout, base::MessageLoopProxy::current(),
initial_policy_fetch_timeout, file_task_runner, io_task_runner));
base::MessageLoopProxy::current(),
file_task_runner,
io_task_runner));
bool wildcard_match = false; bool wildcard_match = false;
if (connector->IsEnterpriseManaged() && if (connector->IsEnterpriseManaged() &&
......
...@@ -51,6 +51,22 @@ void WildcardLoginChecker::Start( ...@@ -51,6 +51,22 @@ void WildcardLoginChecker::Start(
token_fetcher_->Start(); token_fetcher_->Start();
} }
void WildcardLoginChecker::StartWithRefreshToken(
const std::string& refresh_token,
const StatusCallback& callback) {
CHECK(!token_fetcher_);
CHECK(!user_info_fetcher_);
start_timestamp_ = base::Time::Now();
callback_ = callback;
token_fetcher_.reset(new PolicyOAuth2TokenFetcher(
std::string(), g_browser_process->system_request_context(),
base::Bind(&WildcardLoginChecker::OnPolicyTokenFetched,
base::Unretained(this))));
token_fetcher_->StartWithRefreshToken(refresh_token);
}
void WildcardLoginChecker::StartWithAccessToken( void WildcardLoginChecker::StartWithAccessToken(
const std::string& access_token, const std::string& access_token,
const StatusCallback& callback) { const StatusCallback& callback) {
......
...@@ -41,6 +41,10 @@ class WildcardLoginChecker : public UserInfoFetcher::Delegate { ...@@ -41,6 +41,10 @@ class WildcardLoginChecker : public UserInfoFetcher::Delegate {
void Start(scoped_refptr<net::URLRequestContextGetter> signin_context, void Start(scoped_refptr<net::URLRequestContextGetter> signin_context,
const StatusCallback& callback); const StatusCallback& callback);
// Starts checking with a provided refresh token.
void StartWithRefreshToken(const std::string& refresh_token,
const StatusCallback& callback);
// Starts checking with a provided access token. // Starts checking with a provided access token.
void StartWithAccessToken(const std::string& access_token, void StartWithAccessToken(const std::string& access_token,
const StatusCallback& callback); const StatusCallback& callback);
......
...@@ -157,8 +157,7 @@ void LoginPerformer::DoPerformLogin(const UserContext& user_context, ...@@ -157,8 +157,7 @@ void LoginPerformer::DoPerformLogin(const UserContext& user_context,
switch (auth_mode_) { switch (auth_mode_) {
case AUTH_MODE_EXTENSION: { case AUTH_MODE_EXTENSION: {
RunOnlineWhitelistCheck( RunOnlineWhitelistCheck(
email, email, wildcard_match, user_context.GetRefreshToken(),
wildcard_match,
base::Bind(&LoginPerformer::StartLoginCompletion, base::Bind(&LoginPerformer::StartLoginCompletion,
weak_factory_.GetWeakPtr()), weak_factory_.GetWeakPtr()),
base::Bind(&LoginPerformer::NotifyWhitelistCheckFailure, base::Bind(&LoginPerformer::NotifyWhitelistCheckFailure,
......
...@@ -140,6 +140,7 @@ class CHROMEOS_EXPORT LoginPerformer : public AuthStatusConsumer, ...@@ -140,6 +140,7 @@ class CHROMEOS_EXPORT LoginPerformer : public AuthStatusConsumer,
virtual void RunOnlineWhitelistCheck( virtual void RunOnlineWhitelistCheck(
const std::string& user_id, const std::string& user_id,
bool wildcard_match, bool wildcard_match,
const std::string& refresh_token,
const base::Closure& success_callback, const base::Closure& success_callback,
const base::Closure& failure_callback) = 0; const base::Closure& failure_callback) = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment