Home | History | Annotate | Download | only in policy
      1 // Copyright 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 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_WILDCARD_LOGIN_CHECKER_H_
      6 #define CHROME_BROWSER_CHROMEOS_POLICY_WILDCARD_LOGIN_CHECKER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/callback.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/time/time.h"
     12 #include "components/policy/core/common/cloud/user_info_fetcher.h"
     13 #include "google_apis/gaia/google_service_auth_error.h"
     14 
     15 namespace net {
     16 class URLRequestContextGetter;
     17 }
     18 
     19 namespace policy {
     20 
     21 class PolicyOAuth2TokenFetcher;
     22 
     23 // Performs online verification whether wildcard login is allowed, i.e. whether
     24 // the user is a hosted user. This class performs an asynchronous check and
     25 // reports the result via a callback.
     26 class WildcardLoginChecker : public UserInfoFetcher::Delegate {
     27  public:
     28   // Indicates result of the wildcard login check.
     29   enum Result {
     30     RESULT_ALLOWED,  // Wildcard check succeeded, login allowed.
     31     RESULT_BLOCKED,  // Check completed, but user should be blocked.
     32     RESULT_FAILED,   // Failure due to network errors etc.
     33   };
     34 
     35   typedef base::Callback<void(Result)> StatusCallback;
     36 
     37   WildcardLoginChecker();
     38   virtual ~WildcardLoginChecker();
     39 
     40   // Starts checking. The result will be reported via |callback_|.
     41   void Start(scoped_refptr<net::URLRequestContextGetter> signin_context,
     42              const StatusCallback& callback);
     43 
     44   // Starts checking with a provided access token.
     45   void StartWithAccessToken(const std::string& access_token,
     46                             const StatusCallback& callback);
     47 
     48   // UserInfoFetcher::Delegate:
     49   virtual void OnGetUserInfoSuccess(const base::DictionaryValue* response)
     50       OVERRIDE;
     51   virtual void OnGetUserInfoFailure(const GoogleServiceAuthError& error)
     52       OVERRIDE;
     53 
     54  private:
     55   // Starts the check after successful token minting.
     56   void OnPolicyTokenFetched(const std::string& access_token,
     57                             const GoogleServiceAuthError& error);
     58 
     59   // Starts the user info fetcher.
     60   void StartUserInfoFetcher(const std::string& access_token);
     61 
     62   // Handles the response of the check and calls ReportResult().
     63   void OnCheckCompleted(Result result);
     64 
     65   StatusCallback callback_;
     66 
     67   scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
     68   scoped_ptr<UserInfoFetcher> user_info_fetcher_;
     69 
     70   base::Time start_timestamp_;
     71   base::Time token_available_timestamp_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(WildcardLoginChecker);
     74 };
     75 
     76 }  // namespace policy
     77 
     78 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_WILDCARD_LOGIN_CHECKER_H_
     79