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

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: Added unit tests and fixed bugs in accounts DB. Created 5 years 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 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
jln (very slow on Chromium) 2015/12/01 00:08:44 Nit: no blank line here.
ukode 2015/12/16 19:24:12 Done.
10 #include "mojo/public/c/system/main.h"
11 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/application/application_impl.h"
14 #include "mojo/public/cpp/application/application_runner.h"
15 #include "mojo/public/cpp/utility/run_loop.h"
16 #include "mojo/services/authentication/interfaces/authentication.mojom.h"
17
18 namespace authentication {
19 namespace examples {
20
21 class GoogleAuthenticationClientDelegate :
22 public mojo::ApplicationDelegate {
23 public:
24 void Initialize(mojo::ApplicationImpl* app) override {
25 app->ConnectToService("mojo:authentication", &auth_service_);
26
27 mojo::Array<mojo::String> scopes;
28 scopes.push_back("profile");
29 scopes.push_back("email");
30
31 LOG(INFO) << "Starting the device flow handshake...";
32 auth_service_->GetOAuth2DeviceCode(scopes.Pass(),
33 base::Bind(
34 &GoogleAuthenticationClientDelegate::OnDeviceCode,
35 base::Unretained(this)));
36 }
37
38 private:
39 void OnDeviceCode(const mojo::String& url, const mojo::String& device_code,
40 const mojo::String& user_code, const mojo::String& error) {
41 if (!error.is_null()) {
42 LOG(INFO) << "Error: " << error;
43 mojo::RunLoop::current()->Quit(); //All done!
44 return;
45 }
46 // Display the verification url and user code in system UI and ask the
47 // user to authorize in a companion device
48 LOG(INFO) << "Verification Url: " << url;
49 LOG(INFO) << "Device Code: " << device_code;
50 LOG(INFO) << "User Code: " << user_code;
51
52 //TODO: Replace this with polling logic or show some spinner in System UI
53 LOG(INFO) << "Waiting for user autorization on a secondary device...";
54 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(45000));
55
56 // Exchange the authorization to a long lived token
57 LOG(INFO) << "Check for user authorization...";
58 AddAccount(device_code);
59 }
60
61 // Exchange device code to a refresh token, and persist the grant.
62 void AddAccount(const std::string device_code) {
63 auth_service_->AddAccount(device_code,
64 base::Bind(
65 &GoogleAuthenticationClientDelegate::OnAddAccount,
66 base::Unretained(this)));
67 }
68
69 void OnAddAccount(const mojo::String& username, const mojo::String& error) {
70 if (!error.is_null() || username.is_null()) {
71 LOG(INFO) << "Missing username or Error: " << error;
72 mojo::RunLoop::current()->Quit(); //All done!
73 return;
74 }
75
76 LOG(INFO) << "Successfully registered user: " << username;
77 LOG(INFO) << "Fetching access token for user [" << username << "]...";
78 FetchOAuth2AccessToken(username);
79 }
80
81 // Fetch a new access token for an existing user grant.
82 void FetchOAuth2AccessToken(const mojo::String& username) {
83 mojo::Array<mojo::String> scopes;
84 scopes.push_back("profile");
85
86 auth_service_->GetOAuth2Token(username, scopes.Pass(),
87 base::Bind(
88 &GoogleAuthenticationClientDelegate::OnGetOAuth2Token,
89 base::Unretained(this)));
90 }
91
92 void OnGetOAuth2Token(const mojo::String& access_token,
93 const mojo::String& error) {
94 if (!error.is_null()) {
95 LOG(INFO) << "Error: " << error;
96 mojo::RunLoop::current()->Quit(); //All done!
97 return;
98 }
99
100 if (access_token.is_null()) {
101 LOG(INFO) << "Unable to fetch access token, exiting!";
102 } else {
103 LOG(INFO) << "Access Token: " << access_token;
104 }
105 mojo::RunLoop::current()->Quit(); //All done!
106 return;
107 }
108
109 AuthenticationServicePtr auth_service_;
110 };
111
112 } // namespace examples
113 } // namespace auth
114
115 MojoResult MojoMain(MojoHandle application_request) {
116 mojo::ApplicationRunner runner(
117 new authentication::examples::GoogleAuthenticationClientDelegate);
118 return runner.Run(application_request);
119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698