Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / managed / managed_user_creation_controller_new.h
1 // Copyright 2014 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_CHROMEOS_LOGIN_MANAGED_MANAGED_USER_CREATION_CONTROLLER_NEW_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_MANAGED_USER_CREATION_CONTROLLER_NEW_H_
7
8 #include <string>
9
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/timer/timer.h"
15 #include "base/values.h"
16 #include "chrome/browser/chromeos/login/extended_authenticator.h"
17 #include "chrome/browser/chromeos/login/managed/managed_user_creation_controller.h"
18 #include "chrome/browser/managed_mode/managed_user_registration_utility.h"
19
20 class Profile;
21
22 namespace chromeos {
23
24 // LMU Creation process:
25 // 0. Manager is logged in
26 // 1. Generate ID for new LMU
27 // 2. Start "transaction" in Local State.
28 // 3, Generate keys for user : master key, salt, encryption and signature keys.
29 // 4. Create local cryptohome (errors could arise)
30 // 5. Create user in cloud (errors could arise)
31 // 6. Store cloud token in cryptohome (actually, error could arise).
32 // 7. Mark "transaction" as completed.
33 // 8. End manager session.
34 class ManagedUserCreationControllerNew
35     : public ManagedUserCreationController,
36       public ExtendedAuthenticator::AuthStatusConsumer {
37  public:
38   // All UI initialization is deferred till Init() call.
39   // |Consumer| is not owned by controller, and it is expected that it wouldn't
40   // be deleted before ManagedUserCreationControllerNew.
41   ManagedUserCreationControllerNew(StatusConsumer* consumer,
42                                    const std::string& manager_id);
43   virtual ~ManagedUserCreationControllerNew();
44
45   // Returns the current locally managed user controller if it has been created.
46   static ManagedUserCreationControllerNew* current_controller() {
47     return current_controller_;
48   }
49
50   // Set up controller for creating new supervised user with |display_name|,
51   // |password| and avatar indexed by |avatar_index|. StartCreation() have to
52   // be called to actually start creating user.
53   virtual void StartCreation(const base::string16& display_name,
54                              const std::string& password,
55                              int avatar_index) OVERRIDE;
56
57   // Starts import of the supervised users created prior to M35. They lack
58   // information about password.
59   // Configures and initiates importing existing supervised user to this device.
60   // Existing user is identified by |sync_id|, has |display_name|, |password|,
61   // |avatar_index|. The master key for cryptohome is a |master_key|.
62   virtual void StartImport(const base::string16& display_name,
63                            const std::string& password,
64                            int avatar_index,
65                            const std::string& sync_id,
66                            const std::string& master_key) OVERRIDE;
67
68   // Configures and initiates importing existing supervised user to this device.
69   // Existing user is identified by |sync_id|, has |display_name|, |password|,
70   // |avatar_index|. The master key for cryptohome is a |master_key|.
71   // The user has password specified in |password_data| and |encryption_key|/
72   // |signature_key| for cryptohome.
73   virtual void StartImport(const base::string16& display_name,
74                            const std::string& password,
75                            int avatar_index,
76                            const std::string& sync_id,
77                            const std::string& master_key,
78                            const base::DictionaryValue* password_data,
79                            const std::string& encryption_key,
80                            const std::string& signature_key);
81
82   virtual void SetManagerProfile(Profile* manager_profile) OVERRIDE;
83   virtual void CancelCreation() OVERRIDE;
84   virtual void FinishCreation() OVERRIDE;
85   virtual std::string GetManagedUserId() OVERRIDE;
86
87  private:
88   enum Stage {
89     // Just initial stage.
90     STAGE_INITIAL,
91
92     // Creation attempt is recoreded to allow cleanup in case of failure.
93     TRANSACTION_STARTED,
94     // Different keys are generated and public ones are stored in LocalState.
95     KEYS_GENERATED,
96     // Home directory is created with all necessary passwords.
97     CRYPTOHOME_CREATED,
98     // All user-related information is confirmed to exist on server.
99     DASHBOARD_CREATED,
100     // Managed user's sync token is written.
101     TOKEN_WRITTEN,
102     // Managed user is succesfully created.
103     TRANSACTION_COMMITTED,
104     // Some error happened while creating supervised user.
105     STAGE_ERROR,
106   };
107
108   // Indicates if we create new user, or import an existing one.
109   enum CreationType { NEW_USER, USER_IMPORT_OLD, USER_IMPORT_NEW, };
110
111   // Contains information necessary for new user creation.
112   struct UserCreationContext {
113     UserCreationContext();
114     ~UserCreationContext();
115
116     base::string16 display_name;
117     int avatar_index;
118
119     std::string manager_id;
120
121     std::string local_user_id;  // Used to identify cryptohome.
122     std::string sync_user_id;   // Used to identify user in manager's sync data.
123
124     // Keys:
125     std::string master_key;       // Random string
126     std::string signature_key;    // 256 bit HMAC key
127     std::string encryption_key;   // 256 bit HMAC key
128     std::string salted_password;  // Hash(salt + Hash(password))
129
130     std::string password;
131
132     std::string salted_master_key;  // Hash(system salt + master key)
133     std::string mount_hash;
134
135     std::string token;
136
137     CreationType creation_type;
138
139     base::DictionaryValue password_data;
140
141     Profile* manager_profile;
142     scoped_ptr<ManagedUserRegistrationUtility> registration_utility;
143   };
144
145   // ManagedUserAuthenticator::StatusConsumer overrides.
146   virtual void OnAuthenticationFailure(ExtendedAuthenticator::AuthState error)
147       OVERRIDE;
148
149   // Authenticator success callbacks.
150   void OnMountSuccess(const std::string& mount_hash);
151   void OnPasswordHashingSuccess(const std::string& password_hash);
152
153   void StartCreationImpl();
154
155   // Guard timer callback.
156   void CreationTimedOut();
157   // ManagedUserRegistrationUtility callback.
158   void RegistrationCallback(const GoogleServiceAuthError& error,
159                             const std::string& token);
160
161   // Completion callback for StoreManagedUserFiles method.
162   // Called on the UI thread.
163   void OnManagedUserFilesStored(bool success);
164
165   // Pointer to the current instance of the controller to be used by
166   // automation tests.
167   static ManagedUserCreationControllerNew* current_controller_;
168
169   // Current stage of user creation.
170   Stage stage_;
171
172   // Authenticator used for user creation.
173   scoped_refptr<ExtendedAuthenticator> authenticator_;
174
175   // Creation context. Not null while creating new LMU.
176   scoped_ptr<UserCreationContext> creation_context_;
177
178   // Timer for showing warning if creation process takes too long.
179   base::OneShotTimer<ManagedUserCreationControllerNew> timeout_timer_;
180
181   // Factory of callbacks.
182   base::WeakPtrFactory<ManagedUserCreationControllerNew> weak_factory_;
183
184   DISALLOW_COPY_AND_ASSIGN(ManagedUserCreationControllerNew);
185 };
186
187 }  // namespace chromeos
188
189 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_MANAGED_USER_CREATION_CONTROLLER_NEW_H_