Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: examples/authentication_demo/google_authentication_demo.cc

Issue 1466733002: Google OAuth Device Flow support for FNL (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Removed data_unittest.py Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "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", &authentication_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 authentication_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 authentication_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 authentication_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 authentication_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 authentication_service_;
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 }
OLDNEW
« no previous file with comments | « examples/authentication_demo/BUILD.gn ('k') | mojo/dart/packages/mojo_services/lib/authentication/authentication.mojom.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698