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 GoogleAuthenticationClientDelegate : public mojo::ApplicationDelegate { |
| 22 public: |
| 23 void Initialize(mojo::ApplicationImpl* app) override { |
| 24 app->ConnectToService("mojo:authentication", &auth_service_); |
| 25 |
| 26 mojo::Array<mojo::String> scopes; |
| 27 scopes.push_back("profile"); |
| 28 scopes.push_back("email"); |
| 29 |
| 30 LOG(INFO) << "Starting the device flow handshake..."; |
| 31 auth_service_->GetOAuth2DeviceCode( |
| 32 scopes.Pass(), |
| 33 base::Bind(&GoogleAuthenticationClientDelegate::OnDeviceCode, |
| 34 base::Unretained(this))); |
| 35 } |
| 36 |
| 37 private: |
| 38 void OnDeviceCode(const mojo::String& url, |
| 39 const mojo::String& device_code, |
| 40 const mojo::String& user_code, |
| 41 const mojo::String& error) { |
| 42 if (!error.is_null()) { |
| 43 LOG(INFO) << "Error: " << error; |
| 44 mojo::RunLoop::current()->Quit(); // All done! |
| 45 return; |
| 46 } |
| 47 // Display the verification url and user code in system UI and ask the |
| 48 // user to authorize in a companion device |
| 49 LOG(INFO) << "Verification Url: " << url; |
| 50 LOG(INFO) << "Device Code: " << device_code; |
| 51 LOG(INFO) << "User Code: " << user_code; |
| 52 |
| 53 // TODO: Replace this with polling logic or show some spinner in System UI |
| 54 LOG(INFO) << "Waiting for user autorization on a secondary device..."; |
| 55 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(45000)); |
| 56 |
| 57 // Exchange the authorization to a long lived token |
| 58 LOG(INFO) << "Check for user authorization..."; |
| 59 AddAccount(device_code); |
| 60 } |
| 61 |
| 62 // Exchange device code to a refresh token, and persist the grant. |
| 63 void AddAccount(const std::string device_code) { |
| 64 auth_service_->AddAccount( |
| 65 device_code, |
| 66 base::Bind(&GoogleAuthenticationClientDelegate::OnAddAccount, |
| 67 base::Unretained(this))); |
| 68 } |
| 69 |
| 70 void OnAddAccount(const mojo::String& username, const mojo::String& error) { |
| 71 if (!error.is_null() || username.is_null()) { |
| 72 LOG(INFO) << "Missing username or Error: " << error; |
| 73 mojo::RunLoop::current()->Quit(); // All done! |
| 74 return; |
| 75 } |
| 76 |
| 77 LOG(INFO) << "Successfully registered user: " << username; |
| 78 LOG(INFO) << "Fetching access token for user [" << username << "]..."; |
| 79 FetchOAuth2AccessToken(username); |
| 80 } |
| 81 |
| 82 // Fetch a new access token for an existing user grant. |
| 83 void FetchOAuth2AccessToken(const mojo::String& username) { |
| 84 mojo::Array<mojo::String> scopes; |
| 85 scopes.push_back("profile"); |
| 86 |
| 87 auth_service_->GetOAuth2Token( |
| 88 username, scopes.Pass(), |
| 89 base::Bind(&GoogleAuthenticationClientDelegate::OnGetOAuth2Token, |
| 90 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 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 new authentication::examples::GoogleAuthenticationClientDelegate); |
| 120 return runner.Run(application_request); |
| 121 } |
OLD | NEW |