summaryrefslogtreecommitdiffstats
path: root/google_apis/gaia/google_service_auth_error.cc
blob: 76f4122a6aa1b55daeb2c0aa5e20d5908bed439c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// 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 "google_apis/gaia/google_service_auth_error.h"

#include <string>

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "net/base/net_errors.h"

GoogleServiceAuthError::Captcha::Captcha() : image_width(0), image_height(0) {
}

GoogleServiceAuthError::Captcha::Captcha(
    const std::string& token, const GURL& audio, const GURL& img,
    const GURL& unlock, int width, int height)
    : token(token), audio_url(audio), image_url(img), unlock_url(unlock),
      image_width(width), image_height(height) {
}

GoogleServiceAuthError::Captcha::~Captcha() {
}

bool GoogleServiceAuthError::Captcha::operator==(const Captcha& b) const {
  return (token == b.token &&
          audio_url == b.audio_url &&
          image_url == b.image_url &&
          unlock_url == b.unlock_url &&
          image_width == b.image_width &&
          image_height == b.image_height);
}

GoogleServiceAuthError::SecondFactor::SecondFactor() : field_length(0) {
}

GoogleServiceAuthError::SecondFactor::SecondFactor(
    const std::string& token, const std::string& prompt,
    const std::string& alternate, int length)
    : token(token), prompt_text(prompt), alternate_text(alternate),
      field_length(length) {
}

GoogleServiceAuthError::SecondFactor::~SecondFactor() {
}

bool GoogleServiceAuthError::SecondFactor::operator==(
    const SecondFactor& b) const {
  return (token == b.token &&
          prompt_text == b.prompt_text &&
          alternate_text == b.alternate_text &&
          field_length == b.field_length);
}

bool GoogleServiceAuthError::operator==(
    const GoogleServiceAuthError& b) const {
  return (state_ == b.state_ &&
          network_error_ == b.network_error_ &&
          captcha_ == b.captcha_ &&
          second_factor_ == b.second_factor_);
}

bool GoogleServiceAuthError::operator!=(
    const GoogleServiceAuthError& b) const {
  return !(*this == b);
}

GoogleServiceAuthError::GoogleServiceAuthError(State s)
    : state_(s),
      network_error_(0) {
  // If the caller has no idea, then we just set it to a generic failure.
  if (s == CONNECTION_FAILED) {
    network_error_ = net::ERR_FAILED;
  }
}

GoogleServiceAuthError::GoogleServiceAuthError(
    State state,
    const std::string& error_message)
    : state_(state),
      network_error_(0),
      error_message_(error_message) {
}

// static
GoogleServiceAuthError
    GoogleServiceAuthError::FromConnectionError(int error) {
  return GoogleServiceAuthError(CONNECTION_FAILED, error);
}

// static
GoogleServiceAuthError GoogleServiceAuthError::FromClientLoginCaptchaChallenge(
    const std::string& captcha_token,
    const GURL& captcha_image_url,
    const GURL& captcha_unlock_url) {
  return GoogleServiceAuthError(CAPTCHA_REQUIRED, captcha_token, GURL(),
                                captcha_image_url, captcha_unlock_url, 0, 0);
}

// static
GoogleServiceAuthError GoogleServiceAuthError::FromServiceError(
    const std::string& error_message) {
  return GoogleServiceAuthError(SERVICE_ERROR, error_message);
}

// static
GoogleServiceAuthError GoogleServiceAuthError::FromUnexpectedServiceResponse(
    const std::string& error_message) {
  return GoogleServiceAuthError(UNEXPECTED_SERVICE_RESPONSE, error_message);
}

// static
GoogleServiceAuthError GoogleServiceAuthError::AuthErrorNone() {
  return GoogleServiceAuthError(NONE);
}

GoogleServiceAuthError::State GoogleServiceAuthError::state() const {
  return state_;
}

const GoogleServiceAuthError::Captcha& GoogleServiceAuthError::captcha() const {
  return captcha_;
}

const GoogleServiceAuthError::SecondFactor&
GoogleServiceAuthError::second_factor() const {
  return second_factor_;
}

int GoogleServiceAuthError::network_error() const {
  return network_error_;
}

const std::string& GoogleServiceAuthError::token() const {
  switch (state_) {
    case CAPTCHA_REQUIRED:
      return captcha_.token;
      break;
    case TWO_FACTOR:
      return second_factor_.token;
      break;
    default:
      NOTREACHED();
  }
  return base::EmptyString();
}

const std::string& GoogleServiceAuthError::error_message() const {
  return error_message_;
}

base::DictionaryValue* GoogleServiceAuthError::ToValue() const {
  base::DictionaryValue* value = new base::DictionaryValue();
  std::string state_str;
  switch (state_) {
#define STATE_CASE(x) case x: state_str = #x; break
    STATE_CASE(NONE);
    STATE_CASE(INVALID_GAIA_CREDENTIALS);
    STATE_CASE(USER_NOT_SIGNED_UP);
    STATE_CASE(CONNECTION_FAILED);
    STATE_CASE(CAPTCHA_REQUIRED);
    STATE_CASE(ACCOUNT_DELETED);
    STATE_CASE(ACCOUNT_DISABLED);
    STATE_CASE(SERVICE_UNAVAILABLE);
    STATE_CASE(TWO_FACTOR);
    STATE_CASE(REQUEST_CANCELED);
    STATE_CASE(HOSTED_NOT_ALLOWED);
    STATE_CASE(UNEXPECTED_SERVICE_RESPONSE);
    STATE_CASE(SERVICE_ERROR);
    STATE_CASE(WEB_LOGIN_REQUIRED);
#undef STATE_CASE
    default:
      NOTREACHED();
      break;
  }
  value->SetString("state", state_str);
  if (!error_message_.empty()) {
    value->SetString("errorMessage", error_message_);
  }
  if (state_ == CAPTCHA_REQUIRED) {
    base::DictionaryValue* captcha_value = new base::DictionaryValue();
    value->Set("captcha", captcha_value);
    captcha_value->SetString("token", captcha_.token);
    captcha_value->SetString("audioUrl", captcha_.audio_url.spec());
    captcha_value->SetString("imageUrl", captcha_.image_url.spec());
    captcha_value->SetString("unlockUrl", captcha_.unlock_url.spec());
    captcha_value->SetInteger("imageWidth", captcha_.image_width);
    captcha_value->SetInteger("imageHeight", captcha_.image_height);
  } else if (state_ == CONNECTION_FAILED) {
    value->SetString("networkError", net::ErrorToString(network_error_));
  } else if (state_ == TWO_FACTOR) {
    base::DictionaryValue* two_factor_value = new base::DictionaryValue();
    value->Set("two_factor", two_factor_value);
    two_factor_value->SetString("token", second_factor_.token);
    two_factor_value->SetString("promptText", second_factor_.prompt_text);
    two_factor_value->SetString("alternateText", second_factor_.alternate_text);
    two_factor_value->SetInteger("fieldLength", second_factor_.field_length);
  }
  return value;
}

std::string GoogleServiceAuthError::ToString() const {
  switch (state_) {
    case NONE:
      return std::string();
    case INVALID_GAIA_CREDENTIALS:
      return "Invalid credentials.";
    case USER_NOT_SIGNED_UP:
      return "Not authorized.";
    case CONNECTION_FAILED:
      return base::StringPrintf("Connection failed (%d).", network_error_);
    case CAPTCHA_REQUIRED:
      return base::StringPrintf("CAPTCHA required (%s).",
                                captcha_.token.c_str());
    case ACCOUNT_DELETED:
      return "Account deleted.";
    case ACCOUNT_DISABLED:
      return "Account disabled.";
    case SERVICE_UNAVAILABLE:
      return "Service unavailable; try again later.";
    case TWO_FACTOR:
      return base::StringPrintf("2-step verification required (%s).",
                                second_factor_.token.c_str());
    case REQUEST_CANCELED:
      return "Request canceled.";
    case HOSTED_NOT_ALLOWED:
      return "Google account required.";
    case UNEXPECTED_SERVICE_RESPONSE:
      return base::StringPrintf("Unexpected service response (%s)",
                                error_message_.c_str());
    case SERVICE_ERROR:
      return base::StringPrintf("Service responded with error: '%s'",
                                error_message_.c_str());
    case WEB_LOGIN_REQUIRED:
      return "Less secure apps may not authenticate with this account. "
             "Please visit: "
             "https://www.google.com/settings/security/lesssecureapps";
    default:
      NOTREACHED();
      return std::string();
  }
}

bool GoogleServiceAuthError::IsPersistentError() const {
  if (state_ == GoogleServiceAuthError::NONE) return false;
  return !IsTransientError();
}

bool GoogleServiceAuthError::IsTransientError() const {
  switch (state_) {
  // These are failures that are likely to succeed if tried again.
  case GoogleServiceAuthError::CONNECTION_FAILED:
  case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
  case GoogleServiceAuthError::REQUEST_CANCELED:
    return true;
  // Everything else will have the same result.
  default:
    return false;
  }
}

GoogleServiceAuthError::GoogleServiceAuthError(State s, int error)
    : state_(s),
      network_error_(error) {
}

GoogleServiceAuthError::GoogleServiceAuthError(
    State s,
    const std::string& captcha_token,
    const GURL& captcha_audio_url,
    const GURL& captcha_image_url,
    const GURL& captcha_unlock_url,
    int image_width,
    int image_height)
    : state_(s),
      captcha_(captcha_token, captcha_audio_url, captcha_image_url,
               captcha_unlock_url, image_width, image_height),
      network_error_(0) {
}