summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/policy_oauth_fetcher.cc
blob: deb93db0b4f261333fd21afd1c713c894fc377e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/login/policy_oauth_fetcher.h"

#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"

namespace chromeos {

namespace {
const char kServiceScopeChromeOSDeviceManagement[] =
    "https://www.googleapis.com/auth/chromeosdevicemanagement";
}  // namespace

PolicyOAuthFetcher::PolicyOAuthFetcher(Profile* profile,
                                       const std::string& oauth1_token,
                                       const std::string& oauth1_secret)
    : oauth_fetcher_(this,
                     profile->GetRequestContext(),
                     kServiceScopeChromeOSDeviceManagement),
      oauth1_token_(oauth1_token),
      oauth1_secret_(oauth1_secret) {
}

PolicyOAuthFetcher::PolicyOAuthFetcher(Profile* profile)
    : oauth_fetcher_(this,
                     profile->GetRequestContext(),
                     kServiceScopeChromeOSDeviceManagement) {
}

PolicyOAuthFetcher::~PolicyOAuthFetcher() {
}

void PolicyOAuthFetcher::Start() {
  oauth_fetcher_.SetAutoFetchLimit(
      GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN);

  if (oauth1_token_.empty()) {
    oauth_fetcher_.StartGetOAuthTokenRequest();
  } else {
    oauth_fetcher_.StartOAuthWrapBridge(
        oauth1_token_, oauth1_secret_, GaiaConstants::kGaiaOAuthDuration,
        std::string(kServiceScopeChromeOSDeviceManagement));
  }
}

void PolicyOAuthFetcher::OnGetOAuthTokenSuccess(
    const std::string& oauth_token) {
  VLOG(1) << "Got OAuth request token";
}

void PolicyOAuthFetcher::OnGetOAuthTokenFailure(
    const GoogleServiceAuthError& error) {
  LOG(WARNING) << "Failed to get OAuth request token, error: " << error.state();
  SetPolicyToken("");
}

void PolicyOAuthFetcher::OnOAuthGetAccessTokenSuccess(
    const std::string& token,
    const std::string& secret) {
  VLOG(1) << "Got OAuth access token";
  oauth1_token_ = token;
  oauth1_secret_ = secret;
}

void PolicyOAuthFetcher::OnOAuthGetAccessTokenFailure(
      const GoogleServiceAuthError& error) {
  LOG(WARNING) << "Failed to get OAuth access token, error: " << error.state();
  SetPolicyToken("");
}

void PolicyOAuthFetcher::OnOAuthWrapBridgeSuccess(
    const std::string& service_name,
    const std::string& token,
    const std::string& expires_in) {
  VLOG(1) << "Got OAuth access token for " << service_name;
  SetPolicyToken(token);
}

void PolicyOAuthFetcher::OnOAuthWrapBridgeFailure(
    const std::string& service_name,
    const GoogleServiceAuthError& error) {
  LOG(WARNING) << "Failed to get OAuth access token for " << service_name
               << ", error: " << error.state();
  SetPolicyToken("");
}

void PolicyOAuthFetcher::SetPolicyToken(const std::string& token) {
  policy_token_ = token;
  g_browser_process->browser_policy_connector()->RegisterForUserPolicy(token);
}

}  // namespace chromeos