Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/policy/wildcard_login_checker.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "chrome/browser/browser_process.h"
     10 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
     11 #include "chrome/browser/policy/browser_policy_connector.h"
     12 #include "net/url_request/url_request_context_getter.h"
     13 
     14 namespace policy {
     15 
     16 namespace {
     17 
     18 // Presence of this key in the userinfo response indicates whether the user is
     19 // on a hosted domain.
     20 const char kHostedDomainKey[] = "hd";
     21 
     22 }  // namespace
     23 
     24 WildcardLoginChecker::WildcardLoginChecker() {}
     25 
     26 WildcardLoginChecker::~WildcardLoginChecker() {}
     27 
     28 void WildcardLoginChecker::Start(
     29     scoped_refptr<net::URLRequestContextGetter> signin_context,
     30     const StatusCallback& callback) {
     31   CHECK(!token_fetcher_);
     32   CHECK(!user_info_fetcher_);
     33   callback_ = callback;
     34   token_fetcher_.reset(new PolicyOAuth2TokenFetcher(
     35       signin_context,
     36       g_browser_process->system_request_context(),
     37       base::Bind(&WildcardLoginChecker::OnPolicyTokenFetched,
     38                  base::Unretained(this))));
     39   token_fetcher_->Start();
     40 }
     41 
     42 void WildcardLoginChecker::StartWithAccessToken(
     43     const std::string& access_token,
     44     const StatusCallback& callback) {
     45   CHECK(!token_fetcher_);
     46   CHECK(!user_info_fetcher_);
     47   callback_ = callback;
     48 
     49   StartUserInfoFetcher(access_token);
     50 }
     51 
     52 void WildcardLoginChecker::OnGetUserInfoSuccess(
     53     const base::DictionaryValue* response) {
     54   OnCheckCompleted(response->HasKey(kHostedDomainKey));
     55 }
     56 
     57 void WildcardLoginChecker::OnGetUserInfoFailure(
     58     const GoogleServiceAuthError& error) {
     59   LOG(ERROR) << "Failed to fetch user info " << error.ToString();
     60   OnCheckCompleted(false);
     61 }
     62 
     63 void WildcardLoginChecker::OnPolicyTokenFetched(
     64     const std::string& access_token,
     65     const GoogleServiceAuthError& error) {
     66   if (error.state() != GoogleServiceAuthError::NONE) {
     67     LOG(ERROR) << "Failed to fetch policy token " << error.ToString();
     68     OnCheckCompleted(false);
     69     return;
     70   }
     71 
     72   token_fetcher_.reset();
     73   StartUserInfoFetcher(access_token);
     74 }
     75 
     76 void WildcardLoginChecker::StartUserInfoFetcher(
     77     const std::string& access_token) {
     78   user_info_fetcher_.reset(
     79       new UserInfoFetcher(this, g_browser_process->system_request_context()));
     80   user_info_fetcher_->Start(access_token);
     81 }
     82 
     83 void WildcardLoginChecker::OnCheckCompleted(bool result) {
     84   if (!callback_.is_null())
     85     callback_.Run(result);
     86 }
     87 
     88 }  // namespace policy
     89