Home | History | Annotate | Download | only in gaia
      1 // Copyright (c) 2011 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 // A GoogleServiceAuthError is immutable, plain old data representing an
      6 // error from an attempt to authenticate with a Google service.
      7 // It could be from Google Accounts itself, or any service using Google
      8 // Accounts (e.g expired credentials).  It may contain additional data such as
      9 // captcha challenges.
     10 
     11 // A GoogleServiceAuthError without additional data is just a State, defined
     12 // below. A case could be made to have this relation implicit, to allow raising
     13 // error events concisely by doing OnAuthError(GoogleServiceAuthError::NONE),
     14 // for example. But the truth is this class is ever so slightly more than a
     15 // transparent wrapper around 'State' due to additional Captcha data
     16 // (e.g consider operator=), and this would violate the style guide. Thus,
     17 // you must explicitly use the constructor when all you have is a State.
     18 // The good news is the implementation nests the enum inside a class, so you
     19 // may forward declare and typedef GoogleServiceAuthError to something shorter
     20 // in the comfort of your own translation unit.
     21 
     22 #ifndef CHROME_COMMON_NET_GAIA_GOOGLE_SERVICE_AUTH_ERROR_H_
     23 #define CHROME_COMMON_NET_GAIA_GOOGLE_SERVICE_AUTH_ERROR_H_
     24 #pragma once
     25 
     26 #include <string>
     27 
     28 #include "googleurl/src/gurl.h"
     29 
     30 class DictionaryValue;
     31 
     32 class GoogleServiceAuthError {
     33  public:
     34   //
     35   // These enumerations are referenced by integer value in HTML login code.
     36   // Do not change the numeric values.
     37   //
     38   enum State {
     39     // The user is authenticated.
     40     NONE = 0,
     41 
     42     // The credentials supplied to GAIA were either invalid, or the locally
     43     // cached credentials have expired.
     44     INVALID_GAIA_CREDENTIALS = 1,
     45 
     46     // The GAIA user is not authorized to use the service.
     47     USER_NOT_SIGNED_UP = 2,
     48 
     49     // Could not connect to server to verify credentials. This could be in
     50     // response to either failure to connect to GAIA or failure to connect to
     51     // the service needing GAIA tokens during authentication.
     52     CONNECTION_FAILED = 3,
     53 
     54     // The user needs to satisfy a CAPTCHA challenge to unlock their account.
     55     // If no other information is available, this can be resolved by visiting
     56     // https://www.google.com/accounts/DisplayUnlockCaptcha. Otherwise,
     57     // captcha() will provide details about the associated challenge.
     58     CAPTCHA_REQUIRED = 4,
     59 
     60     // The user account has been deleted.
     61     ACCOUNT_DELETED = 5,
     62 
     63     // The user account has been disabled.
     64     ACCOUNT_DISABLED = 6,
     65 
     66     // The service is not available; try again later.
     67     SERVICE_UNAVAILABLE = 7,
     68 
     69     // The password is valid but we need two factor to get a token.
     70     TWO_FACTOR = 8,
     71 
     72     // The requestor of the authentication step cancelled the request
     73     // prior to completion.
     74     REQUEST_CANCELED = 9,
     75 
     76     // The user has provided a HOSTED account, when this service requires
     77     // a GOOGLE account.
     78     HOSTED_NOT_ALLOWED = 10,
     79   };
     80 
     81   // Additional data for CAPTCHA_REQUIRED errors.
     82   struct Captcha {
     83     Captcha(const std::string& t, const GURL& img, const GURL& unlock);
     84     std::string token;  // Globally identifies the specific CAPTCHA challenge.
     85     GURL image_url;     // The CAPTCHA image to show the user.
     86     GURL unlock_url;    // Pretty unlock page containing above captcha.
     87   };
     88 
     89   // For test only.
     90   bool operator==(const GoogleServiceAuthError &b) const;
     91 
     92   // Construct a GoogleServiceAuthError from a State with no additional data.
     93   explicit GoogleServiceAuthError(State s);
     94 
     95   // Construct a GoogleServiceAuthError from a network error.
     96   // It will be created with CONNECTION_FAILED set.
     97   static GoogleServiceAuthError FromConnectionError(int error);
     98 
     99   // Construct a CAPTCHA_REQUIRED error with CAPTCHA challenge data.
    100   static GoogleServiceAuthError FromCaptchaChallenge(
    101       const std::string& captcha_token,
    102       const GURL& captcha_image_url,
    103       const GURL& captcha_unlock_url);
    104 
    105   // Provided for convenience for clients needing to reset an instance to NONE.
    106   // (avoids err_ = GoogleServiceAuthError(GoogleServiceAuthError::NONE), due
    107   // to explicit class and State enum relation. Note: shouldn't be inlined!
    108   static GoogleServiceAuthError None();
    109 
    110   // The error information.
    111   const State& state() const;
    112   const Captcha& captcha() const;
    113   int network_error() const;
    114 
    115   // Returns info about this object in a dictionary.  Caller takes
    116   // ownership of returned dictionary.
    117   DictionaryValue* ToValue() const;
    118 
    119  private:
    120   GoogleServiceAuthError(State s, int error);
    121 
    122   GoogleServiceAuthError(State s, const std::string& captcha_token,
    123                          const GURL& captcha_image_url,
    124                          const GURL& captcha_unlock_url);
    125 
    126   State state_;
    127   Captcha captcha_;
    128   int network_error_;
    129 };
    130 
    131 #endif  // CHROME_COMMON_NET_GAIA_GOOGLE_SERVICE_AUTH_ERROR_H_
    132