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 "examples/bank_app/bank.mojom.h" |
| 6 #include "mojo/common/binding_set.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/application/interface_factory.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 #include "mojo/public/cpp/utility/run_loop.h" |
| 15 #include "mojo/services/vanadium/security/public/interfaces/principal.mojom.h" |
| 16 |
| 17 namespace examples { |
| 18 |
| 19 using vanadium::PrincipalServicePtr; |
| 20 |
| 21 class BankImpl : public Bank { |
| 22 public: |
| 23 BankImpl() : balance_(0) {} |
| 24 void Deposit(int32_t usd) override { balance_ += usd; } |
| 25 void Withdraw(int32_t usd) override { balance_ -= usd; } |
| 26 void GetBalance(const GetBalanceCallback& callback) override { |
| 27 callback.Run(balance_); |
| 28 } |
| 29 private: |
| 30 int32_t balance_; |
| 31 }; |
| 32 |
| 33 class BankUser { |
| 34 public: |
| 35 explicit BankUser(std::string* user) : user_(user) { } |
| 36 void Run(const vanadium::BlessingPtr& b) const { |
| 37 user_->clear(); |
| 38 if (b) { |
| 39 for (size_t i = 0; i < b->chain.size(); i++) { |
| 40 user_->append(vanadium::ChainSeparator); |
| 41 user_->append(b->chain[i]->extension); |
| 42 } |
| 43 } |
| 44 } |
| 45 private: |
| 46 std::string *user_; |
| 47 }; |
| 48 |
| 49 class BankApp : public mojo::ApplicationDelegate, |
| 50 public mojo::InterfaceFactory<Bank> { |
| 51 public: |
| 52 BankApp() { |
| 53 } |
| 54 void Initialize(mojo::ApplicationImpl* app) override { |
| 55 app->ConnectToService("mojo:principal_service", &login_service_); |
| 56 } |
| 57 |
| 58 // From ApplicationDelegate |
| 59 bool ConfigureIncomingConnection( |
| 60 mojo::ApplicationConnection* connection) override { |
| 61 std::string url = connection->GetRemoteApplicationURL(); |
| 62 if (url.length() > 0) { |
| 63 vanadium::AppInstanceNamePtr app(vanadium::AppInstanceName::New()); |
| 64 app->url = url; |
| 65 std::string user; |
| 66 login_service_->GetUserBlessing(app.Pass(), BankUser(&user)); |
| 67 // Check and see whether we got a valid user blessing. |
| 68 if (!login_service_.WaitForIncomingResponse()) { |
| 69 MOJO_LOG(INFO) << "Failed to get a valid user blessing"; |
| 70 return false; |
| 71 } |
| 72 // Record user access to the bank and reject customers that |
| 73 // don't have a user identity. |
| 74 if (user.empty()) { |
| 75 MOJO_LOG(INFO) << "Rejecting customer without a user identity"; |
| 76 return false; |
| 77 } |
| 78 MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; |
| 79 } |
| 80 connection->AddService(this); |
| 81 return true; |
| 82 } |
| 83 |
| 84 void Create(mojo::ApplicationConnection* connection, |
| 85 mojo::InterfaceRequest<Bank> request) override { |
| 86 bindings_.AddBinding(&bank_impl_, request.Pass()); |
| 87 } |
| 88 |
| 89 private: |
| 90 BankImpl bank_impl_; |
| 91 mojo::BindingSet<Bank> bindings_; |
| 92 vanadium::PrincipalServicePtr login_service_; |
| 93 }; |
| 94 |
| 95 } // namespace examples |
| 96 |
| 97 MojoResult MojoMain(MojoHandle application_request) { |
| 98 mojo::ApplicationRunner runner(new examples::BankApp()); |
| 99 return runner.Run(application_request); |
| 100 } |
OLD | NEW |