OLD | NEW |
---|---|
(Empty) | |
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 #include "base/bind.h" | |
6 #include "base/logging.h" | |
7 #include "base/synchronization/waitable_event.h" | |
8 #include "base/threading/platform_thread.h" | |
9 #include "mojo/public/c/system/main.h" | |
10 #include "mojo/public/cpp/application/application_connection.h" | |
11 #include "mojo/public/cpp/application/application_delegate.h" | |
12 #include "mojo/public/cpp/application/application_impl.h" | |
13 #include "mojo/public/cpp/application/application_runner.h" | |
14 #include "mojo/public/cpp/utility/run_loop.h" | |
15 #include "mojo/services/authentication/interfaces/authentication.mojom.h" | |
16 | |
17 namespace mojo { | |
18 namespace examples { | |
19 namespace authentication { | |
20 | |
21 class GoogleAuthApp : public mojo::ApplicationDelegate { | |
viettrungluu
2016/01/11 21:23:44
nit: Here and below: You probably don't need "mojo
ukode
2016/02/09 00:20:17
Acknowledged. Removed it from mojo namespace. mojo
| |
22 public: | |
23 GoogleAuthApp() {} | |
24 | |
viettrungluu
2016/01/11 21:23:44
nit: Probably you should also explicitly declare a
ukode
2016/02/09 00:20:17
Done.
| |
25 void Initialize(mojo::ApplicationImpl* app) override { | |
26 LOG(INFO) << "Connecting to authentication service..."; | |
viettrungluu
2016/01/11 21:23:44
You may want to consider using DLOG(INFO), VLOG, o
ukode
2016/02/09 00:20:17
Acknowledged.
| |
27 app->ConnectToService("mojo:authentication", &auth_service_); | |
28 | |
29 mojo::Array<mojo::String> scopes; | |
30 scopes.push_back("profile"); | |
31 scopes.push_back("email"); | |
32 | |
33 LOG(INFO) << "Starting the device flow handshake..."; | |
34 auth_service_->GetOAuth2DeviceCode( | |
35 scopes.Pass(), | |
36 base::Bind(&GoogleAuthApp::OnDeviceCode, base::Unretained(this))); | |
37 } | |
38 | |
39 private: | |
40 void OnDeviceCode(const mojo::String& url, | |
41 const mojo::String& device_code, | |
42 const mojo::String& user_code, | |
43 const mojo::String& error) { | |
44 if (!error.is_null()) { | |
45 LOG(INFO) << "Error: " << error; | |
46 mojo::RunLoop::current()->Quit(); // All done! | |
47 return; | |
48 } | |
49 // Display the verification url and user code in system UI and ask the | |
50 // user to authorize in a companion device | |
51 LOG(INFO) << "Verification Url: " << url; | |
52 LOG(INFO) << "Device Code: " << device_code; | |
53 LOG(INFO) << "User Code: " << user_code; | |
54 | |
55 // TODO: Replace this with polling logic or show some spinner in System UI | |
viettrungluu
2016/01/11 21:23:44
nit: "TODO(ukode)" :)
qsr
2016/01/13 12:01:00
Should the polling part be handled in the authenti
ukode
2016/02/09 00:20:17
Moved the polling logic under AddAccount interface
ukode
2016/02/09 00:20:17
Done.
| |
56 LOG(INFO) << "Waiting for user autorization on a secondary device..."; | |
57 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(45000)); | |
58 | |
59 // Exchange the authorization to a long lived token | |
60 LOG(INFO) << "Check for user authorization..."; | |
61 AddAccount(device_code); | |
62 } | |
63 | |
64 // Exchange device code to a refresh token, and persist the grant. | |
65 void AddAccount(const std::string device_code) { | |
66 auth_service_->AddAccount( | |
67 device_code, | |
68 base::Bind(&GoogleAuthApp::OnAddAccount, base::Unretained(this))); | |
69 } | |
70 | |
71 void OnAddAccount(const mojo::String& username, const mojo::String& error) { | |
72 if (!error.is_null() || username.is_null()) { | |
73 LOG(INFO) << "Missing username or Error: " << error; | |
74 mojo::RunLoop::current()->Quit(); // All done! | |
75 return; | |
76 } | |
77 | |
78 LOG(INFO) << "Successfully registered user: " << username; | |
79 LOG(INFO) << "Fetching access token for user [" << username << "]..."; | |
80 FetchOAuth2AccessToken(username); | |
81 } | |
82 | |
83 // Fetch a new access token for an existing user grant. | |
84 void FetchOAuth2AccessToken(const mojo::String& username) { | |
85 mojo::Array<mojo::String> scopes; | |
86 scopes.push_back("profile"); | |
87 | |
88 auth_service_->GetOAuth2Token( | |
89 username, scopes.Pass(), | |
90 base::Bind(&GoogleAuthApp::OnGetOAuth2Token, base::Unretained(this))); | |
91 } | |
92 | |
93 void OnGetOAuth2Token(const mojo::String& access_token, | |
94 const mojo::String& error) { | |
95 if (!error.is_null()) { | |
96 LOG(INFO) << "Error: " << error; | |
97 mojo::RunLoop::current()->Quit(); // All done! | |
98 return; | |
99 } | |
100 | |
101 if (access_token.is_null()) { | |
102 LOG(INFO) << "Unable to fetch access token, exiting!"; | |
103 } else { | |
104 LOG(INFO) << "Access Token: " << access_token; | |
105 } | |
106 mojo::RunLoop::current()->Quit(); // All done! | |
107 return; | |
108 } | |
109 | |
110 ::authentication::AuthenticationServicePtr auth_service_; | |
111 }; | |
112 | |
113 } // namespace authentication | |
114 } // namespace examples | |
115 } // namespace mojo | |
116 | |
117 MojoResult MojoMain(MojoHandle application_request) { | |
118 mojo::ApplicationRunner runner( | |
119 std::unique_ptr<mojo::examples::authentication::GoogleAuthApp>( | |
120 new mojo::examples::authentication::GoogleAuthApp())); | |
121 return runner.Run(application_request); | |
122 } | |
OLD | NEW |