OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
qsr
2016/02/16 14:17:05
2016
ukode
2016/02/26 21:35:49
Done.
| |
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 "mojo/public/c/system/main.h" | |
8 #include "mojo/public/cpp/application/application_connection.h" | |
9 #include "mojo/public/cpp/application/application_delegate.h" | |
10 #include "mojo/public/cpp/application/application_impl.h" | |
11 #include "mojo/public/cpp/application/application_runner.h" | |
12 #include "mojo/public/cpp/utility/run_loop.h" | |
13 #include "mojo/services/authentication/interfaces/authentication.mojom.h" | |
14 | |
15 namespace examples { | |
16 namespace authentication { | |
17 | |
18 class GoogleAuthApp : public mojo::ApplicationDelegate { | |
19 public: | |
20 GoogleAuthApp() {} | |
21 | |
22 ~GoogleAuthApp() override {} | |
23 | |
24 void Initialize(mojo::ApplicationImpl* app) override { | |
25 DLOG(INFO) << "Connecting to authentication service..."; | |
26 app->ConnectToService("mojo:authentication", &auth_service_); | |
27 | |
28 mojo::Array<mojo::String> scopes; | |
29 scopes.push_back("profile"); | |
30 scopes.push_back("email"); | |
31 | |
32 DLOG(INFO) << "Starting the device flow handshake..."; | |
33 auth_service_->GetOAuth2DeviceCode( | |
34 scopes.Pass(), | |
35 base::Bind(&GoogleAuthApp::OnDeviceCode, base::Unretained(this))); | |
36 } | |
37 | |
38 private: | |
39 void OnDeviceCode(const mojo::String& url, | |
40 const mojo::String& device_code, | |
41 const mojo::String& user_code, | |
42 const mojo::String& error) { | |
43 if (!error.is_null()) { | |
44 DLOG(INFO) << "Error: " << error; | |
45 mojo::RunLoop::current()->Quit(); // All done! | |
46 return; | |
47 } | |
48 // Display the verification url and user code in system UI and ask the | |
49 // user to authorize in a companion device | |
50 DLOG(INFO) << "Verification Url: " << url; | |
51 DLOG(INFO) << "Device Code: " << device_code; | |
52 DLOG(INFO) << "User Code: " << user_code; | |
53 DLOG(INFO) << "Waiting for user autorization on a secondary device..."; | |
54 | |
55 // Poll and exchange the user authorization to a long lived token | |
56 DLOG(INFO) << "..."; | |
57 AddAccount(device_code); | |
58 } | |
59 | |
60 // Exchange device code to a refresh token, and persist the grant. | |
61 void AddAccount(const std::string device_code) { | |
62 auth_service_->AddAccount( | |
63 device_code, | |
64 base::Bind(&GoogleAuthApp::OnAddAccount, base::Unretained(this))); | |
65 } | |
66 | |
67 void OnAddAccount(const mojo::String& username, const mojo::String& error) { | |
68 if (!error.is_null() || username.is_null()) { | |
69 DLOG(INFO) << "Missing username or Error: " << error; | |
70 mojo::RunLoop::current()->Quit(); // All done! | |
71 return; | |
72 } | |
73 | |
74 DLOG(INFO) << "Successfully registered user: " << username; | |
75 DLOG(INFO) << "Fetching access token for user [" << username << "]..."; | |
76 FetchOAuth2AccessToken(); | |
77 } | |
78 | |
79 // Fetch a new access token for an existing user grant. | |
80 void FetchOAuth2AccessToken() { | |
81 auth_service_->SelectAccount( | |
82 true, | |
83 base::Bind(&GoogleAuthApp::OnSelectAccount, base::Unretained(this))); | |
84 } | |
85 | |
86 // Selects an existing user account for this app based on previous | |
87 // authorization. | |
88 void OnSelectAccount(const mojo::String& username, | |
89 const mojo::String& error) { | |
90 if (!error.is_null()) { | |
91 DLOG(INFO) << "Error: " << error; | |
92 mojo::RunLoop::current()->Quit(); // All done! | |
93 return; | |
94 } | |
95 | |
96 DLOG(INFO) << "Selected <user> for this app: " << username; | |
97 | |
98 mojo::Array<mojo::String> scopes; | |
99 scopes.push_back("profile"); | |
100 | |
101 auth_service_->GetOAuth2Token( | |
102 username, scopes.Pass(), | |
103 base::Bind(&GoogleAuthApp::OnGetOAuth2Token, base::Unretained(this))); | |
104 } | |
105 | |
106 void OnGetOAuth2Token(const mojo::String& access_token, | |
107 const mojo::String& error) { | |
108 if (!error.is_null()) { | |
109 DLOG(INFO) << "Error: " << error; | |
110 mojo::RunLoop::current()->Quit(); // All done! | |
111 return; | |
112 } | |
113 | |
114 if (access_token.is_null()) { | |
115 DLOG(INFO) << "Unable to fetch access token, exiting!"; | |
116 } else { | |
117 DLOG(INFO) << "Access Token: " << access_token; | |
118 } | |
119 mojo::RunLoop::current()->Quit(); // All done! | |
120 return; | |
121 } | |
122 | |
123 ::authentication::AuthenticationServicePtr auth_service_; | |
qsr
2016/02/16 14:17:06
authentication_service_ (https://google.github.io/
ukode
2016/02/26 21:35:49
Done.
| |
124 }; | |
125 | |
126 } // namespace authentication | |
127 } // namespace examples | |
128 | |
129 MojoResult MojoMain(MojoHandle application_request) { | |
130 mojo::ApplicationRunner runner( | |
131 std::unique_ptr<examples::authentication::GoogleAuthApp>( | |
132 new examples::authentication::GoogleAuthApp())); | |
133 return runner.Run(application_request); | |
134 } | |
OLD | NEW |