1 // Copyright 2015 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 COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
6 #define COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
7 
8 #include <stddef.h>
9 
10 #include <functional>
11 #include <ostream>
12 #include <string>
13 
14 
15 enum class AccountType { UNKNOWN, GOOGLE, ACTIVE_DIRECTORY };
16 
17 // Type that contains enough information to identify user.
18 //
19 // TODO(alemate): we are in the process of moving away from std::string as a
20 // type for storing user identifier to AccountId. At this time GaiaId is mostly
21 // empty, so this type is used as a replacement for e-mail string.
22 // But in near future AccountId will become full feature user identifier.
23 // TODO(alemate): Rename functions and fields to reflect different types of
24 // accounts. (see crbug.com/672253)
25 class AccountId {
26  public:
27   // Creates an empty account id.
28   //
29   // Note: This constructor is public as it is required for mojo serialization
30   // To create an AccountId object, prefer using the static FromXXXX methods or
31   // the EmptyAccountId method when creating an empty account id.
32   AccountId();
33 
34   AccountId(const AccountId& other);
35 
36   // If any of the comparable AccountIds has AccountType == UNKNOWN then it
37   // compares emails.
38   // If both are not UNKNOWN and not equal then it returns false.
39   // If AccountType == GOOGLE then it checks if either ids or emails are equal.
40   // If AccountType == ACTIVE_DIRECTORY then it checks if ids and emails are
41   // equal.
42   bool operator==(const AccountId& other) const;
43   bool operator!=(const AccountId& other) const;
44   bool operator<(const AccountId& right) const;
45 
46   // empty() is deprecated. Use !is_valid() instead.
47   bool empty() const;
48   bool is_valid() const;
49   void clear();
50 
51   AccountType GetAccountType() const;
52   const std::string& GetGaiaId() const;
53   const std::string& GetObjGuid() const;
54   // Users of AccountId should make no assumptions on the format of email.
55   // I.e. it cannot be used as account identifier, because it is (in general)
56   // non-comparable.
57   const std::string& GetUserEmail() const;
58 
59   // Returns true if |GetAccountIdKey| would return valid key.
60   bool HasAccountIdKey() const;
61   // This returns prefixed some string that can be used as a storage key.
62   // You should make no assumptions on the format of this string.
63   const std::string GetAccountIdKey() const;
64 
65   void SetUserEmail(const std::string& email);
66 
67   // This method is to be used during transition period only.
68   // AccountId with UNKNOWN AccountType;
69   static AccountId FromUserEmail(const std::string& user_email);
70   // AccountId with GOOGLE AccountType;
71   // This method is to be used during transition period only.
72   static AccountId FromGaiaId(const std::string& gaia_id);
73   // This method is the preferred way to construct AccountId if you have
74   // full account information.
75   // AccountId with GOOGLE AccountType;
76   static AccountId FromUserEmailGaiaId(const std::string& user_email,
77                                        const std::string& gaia_id);
78   // These methods are used to construct Active Directory AccountIds.
79   // AccountId with ACTIVE_DIRECTORY AccountType;
80   static AccountId AdFromUserEmailObjGuid(const std::string& email,
81                                           const std::string& obj_guid);
82   // AccountId with ACTIVE_DIRECTORY AccountType;
83   static AccountId AdFromObjGuid(const std::string& obj_guid);
84 
85   // Translation functions between AccountType and std::string. Used for
86   // serialization.
87   static AccountType StringToAccountType(
88       const std::string& account_type_string);
89   static std::string AccountTypeToString(const AccountType& account_type);
90 
91   // These are (for now) unstable and cannot be used to store serialized data to
92   // persistent storage. Only in-memory storage is safe.
93   // Serialize() returns JSON dictionary,
94   // Deserialize() restores AccountId after serialization.
95   std::string Serialize() const;
96   static bool Deserialize(const std::string& serialized,
97                           AccountId* out_account_id);
98 
99  private:
100   friend std::ostream& operator<<(std::ostream&, const AccountId&);
101 
102   AccountId(const std::string& id,
103             const std::string& user_email,
104             const AccountType& account_type);
105 
106   std::string id_;
107   std::string user_email_;
108   AccountType account_type_ = AccountType::UNKNOWN;
109 };
110 
111 // Overload << operator to allow logging of AccountIds.
112 std::ostream& operator<<(std::ostream& stream, const AccountId& account_id);
113 
114 // Returns a reference to a singleton.
115 const AccountId& EmptyAccountId();
116 
117 namespace std {
118 
119 // Implement hashing of AccountId, so it can be used as a key in STL containers.
120 template <>
121 struct hash<AccountId> {
122   std::size_t operator()(const AccountId& user_id) const;
123 };
124 
125 }  // namespace std
126 
127 #endif  // COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
128