Home | History | Annotate | Download | only in signin
      1 // Copyright (c) 2012 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_SIGNIN_SIGNIN_INTERNALS_UTIL_H_
      6 #define CHROME_BROWSER_SIGNIN_SIGNIN_INTERNALS_UTIL_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/values.h"
     13 
     14 namespace signin_internals_util {
     15 
     16 // Preference prefixes for signin and token values.
     17 extern const char kSigninPrefPrefix[];
     18 extern const char kTokenPrefPrefix[];
     19 
     20 // The length of strings returned by GetTruncatedHash() below.
     21 const size_t kTruncateTokenStringLength = 6;
     22 
     23 // Helper enums to access fields from SigninStatus (declared below).
     24 enum {
     25   SIGNIN_FIELDS_BEGIN = 0,
     26   UNTIMED_FIELDS_BEGIN = SIGNIN_FIELDS_BEGIN
     27 };
     28 
     29 enum UntimedSigninStatusField {
     30   USERNAME = UNTIMED_FIELDS_BEGIN,
     31   SID,
     32   LSID,
     33   UNTIMED_FIELDS_END
     34 };
     35 
     36 enum {
     37   UNTIMED_FIELDS_COUNT = UNTIMED_FIELDS_END - UNTIMED_FIELDS_BEGIN,
     38   TIMED_FIELDS_BEGIN = UNTIMED_FIELDS_END
     39 };
     40 
     41 enum TimedSigninStatusField {
     42   SIGNIN_TYPE = TIMED_FIELDS_BEGIN,
     43   CLIENT_LOGIN_STATUS,
     44   OAUTH_LOGIN_STATUS,
     45   GET_USER_INFO_STATUS,
     46   UBER_TOKEN_STATUS,
     47   MERGE_SESSION_STATUS,
     48   TIMED_FIELDS_END
     49 };
     50 
     51 enum {
     52   TIMED_FIELDS_COUNT = TIMED_FIELDS_END - TIMED_FIELDS_BEGIN,
     53   SIGNIN_FIELDS_END = TIMED_FIELDS_END,
     54   SIGNIN_FIELDS_COUNT = SIGNIN_FIELDS_END - SIGNIN_FIELDS_BEGIN
     55 };
     56 
     57 // Encapsulates diagnostic information about tokens for different services.
     58 // Note that although SigninStatus contains a map of service names to token
     59 // values, we replicate the service name within this struct for a cleaner
     60 // serialization (with ToValue()).
     61 struct TokenInfo {
     62   std::string truncated_token;  // The hashed and truncated token.
     63   std::string status;  // Status of the last token fetch.
     64   std::string time;  // Timestamp of the last token fetch
     65   int64 time_internal;  // Same as |time|, but in base::Time internal format.
     66   std::string service;  // The service that this token is for.
     67 
     68   TokenInfo(const std::string& truncated_token,
     69             const std::string& status,
     70             const std::string& time,
     71             const int64& time_internal,
     72             const std::string& service);
     73   TokenInfo();
     74   ~TokenInfo();
     75 
     76   DictionaryValue* ToValue();
     77 };
     78 
     79 // Associates a service name with its token information.
     80 typedef std::map<std::string, TokenInfo> TokenInfoMap;
     81 
     82 // Returns the root preference path for the service. The path should be
     83 // qualified with one of .value, .status or .time to get the respective
     84 // full preference path names.
     85 std::string TokenPrefPath(const std::string& service_name);
     86 
     87 // Many values in SigninStatus are also associated with a timestamp.
     88 // This makes it easier to keep values and their associated times together.
     89 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
     90 
     91 // Returns the name of a SigninStatus field.
     92 std::string SigninStatusFieldToString(UntimedSigninStatusField field);
     93 std::string SigninStatusFieldToString(TimedSigninStatusField field);
     94 
     95 // Encapsulates both authentication and token related information. Used
     96 // by SigninInternals to maintain information that needs to be shown in
     97 // the about:signin-internals page.
     98 struct SigninStatus {
     99   std::vector<std::string> untimed_signin_fields;
    100   std::vector<TimedSigninStatusValue> timed_signin_fields;
    101   TokenInfoMap token_info_map;
    102 
    103   SigninStatus();
    104   ~SigninStatus();
    105 
    106   // Returns a dictionary with the following form:
    107   // { "signin_info" :
    108   //     [ {"title": "Basic Information",
    109   //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
    110   //       },
    111   //       { "title": "Detailed Information",
    112   //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
    113   //       }],
    114   //   "token_info" :
    115   //     [ List of {"name": "foo-name", "token" : "foo-token",
    116   //                 "status": "foo_stat", "time" : "foo_time"} elems]
    117   //  }
    118   scoped_ptr<DictionaryValue> ToValue();
    119 };
    120 
    121 // An Observer class for authentication and token diagnostic information.
    122 class SigninDiagnosticsObserver {
    123  public:
    124   // Credentials and signin related changes.
    125   virtual void NotifySigninValueChanged(const UntimedSigninStatusField& field,
    126                                         const std::string& value) {}
    127   virtual void NotifySigninValueChanged(const TimedSigninStatusField& field,
    128                                         const std::string& value) {}
    129   // OAuth tokens related changes.
    130   virtual void NotifyTokenReceivedSuccess(const std::string& token_name,
    131                                           const std::string& token,
    132                                           bool update_time) {}
    133   virtual void NotifyTokenReceivedFailure(const std::string& token_name,
    134                                           const std::string& error) {}
    135   virtual void NotifyClearStoredToken(const std::string& token_name) {}};
    136 
    137 // Gets the first 6 hex characters of the SHA256 hash of the passed in string.
    138 // These are enough to perform equality checks across a single users tokens,
    139 // while preventing outsiders from reverse-engineering the actual token from
    140 // the displayed value.
    141 // Note that for readability (in about:signin-internals), an empty string
    142 // is not hashed, but simply returned as an empty string.
    143 std::string GetTruncatedHash(const std::string& str);
    144 
    145 } // namespace signin_internals_util
    146 
    147 #endif  // CHROME_BROWSER_SIGNIN_SIGNIN_INTERNALS_UTIL_H_
    148