Commit 9d7254970a3038c0f0fdc0342c92b3e99e4293f7

Authored by Grzegorz Jabłoński
1 parent 2b1e8a36

Added interceptor solution.

cpp11/Ice/auth_interceptor/Client.cpp 0 → 100644
  1 +//
  2 +// Copyright (c) ZeroC, Inc. All rights reserved.
  3 +//
  4 +
  5 +#include <Ice/Ice.h>
  6 +#include <Context.h>
  7 +
  8 +using namespace std;
  9 +using namespace Demo;
  10 +
  11 +int run(const shared_ptr<Ice::Communicator>&);
  12 +
  13 +int
  14 +main(int argc, char* argv[])
  15 +{
  16 +#ifdef ICE_STATIC_LIBS
  17 + Ice::registerIceSSL();
  18 +#endif
  19 +
  20 + int status = 0;
  21 +
  22 + try
  23 + {
  24 + //
  25 + // CommunicatorHolder's ctor initializes an Ice communicator,
  26 + // and its dtor destroys this communicator.
  27 + //
  28 + Ice::CommunicatorHolder ich(argc, argv, "config.client");
  29 +
  30 + //
  31 + // The communicator initialization removes all Ice-related arguments from argc/argv
  32 + //
  33 + if(argc > 1)
  34 + {
  35 + cerr << argv[0] << ": too many arguments" << endl;
  36 + status = 1;
  37 + }
  38 + else
  39 + {
  40 + status = run(ich.communicator());
  41 + }
  42 + }
  43 + catch(const std::exception& ex)
  44 + {
  45 + cerr << argv[0] << ": " << ex.what() << endl;
  46 + status = 1;
  47 + }
  48 +
  49 + return status;
  50 +}
  51 +
  52 +void menu();
  53 +
  54 +int run(const shared_ptr<Ice::Communicator>& communicator)
  55 +{
  56 + auto proxy = Ice::checkedCast<ContextPrx>(communicator->propertyToProxy("Context.Proxy"));
  57 + if(!proxy)
  58 + {
  59 + cerr << "invalid proxy" << endl;
  60 + return 1;
  61 + }
  62 +
  63 + string token = proxy->login("user", "password");
  64 +
  65 + try {
  66 + proxy->function();
  67 + }
  68 + catch(const std::exception& ex)
  69 + {
  70 + cerr << ex.what() << endl;
  71 + }
  72 +
  73 + Ice::Context ctx;
  74 + ctx["token"] = token;
  75 + auto proxy2 = proxy->ice_context(ctx);
  76 + proxy2->function();
  77 +
  78 + return 0;
  79 +}
... ...
cpp11/Ice/auth_interceptor/Context.ice 0 → 100644
  1 +//
  2 +// Copyright (c) ZeroC, Inc. All rights reserved.
  3 +//
  4 +
  5 +#pragma once
  6 +
  7 +module Demo
  8 +{
  9 + exception SecurityError {};
  10 + interface Context
  11 + {
  12 + string login(string name, string password);
  13 + void function();
  14 + void shutdown();
  15 + }
  16 +}
... ...
cpp11/Ice/auth_interceptor/ContextI.cpp 0 → 100644
  1 +//
  2 +// Copyright (c) ZeroC, Inc. All rights reserved.
  3 +//
  4 +
  5 +#include <Ice/Ice.h>
  6 +#include <ContextI.h>
  7 +
  8 +using namespace std;
  9 +
  10 +std::string
  11 +ContextI::login(std::string name, std::string passwd, const Ice::Current& /* c */)
  12 +{
  13 + secretToken = name + passwd + std::to_string(rand());
  14 + return secretToken;
  15 +}
  16 +
  17 +void
  18 +ContextI::function(const Ice::Current& /* c */)
  19 +{
  20 + cout << "In function" << endl;
  21 +}
  22 +
  23 +void
  24 +ContextI::shutdown(const Ice::Current& c)
  25 +{
  26 + cout << "Shutting down..." << endl;
  27 + c.adapter->getCommunicator()->shutdown();
  28 +}
... ...
cpp11/Ice/auth_interceptor/ContextI.h 0 → 100644
  1 +//
  2 +// Copyright (c) ZeroC, Inc. All rights reserved.
  3 +//
  4 +
  5 +#ifndef CONTEXT_I_H
  6 +#define CONTEXT_I_H
  7 +
  8 +#include <Context.h>
  9 +
  10 +class ContextI : public Demo::Context
  11 +{
  12 + std::string secretToken;
  13 +public:
  14 +
  15 + std::string login(std::string name, std::string passwd, const Ice::Current& c) override;
  16 + void function(const Ice::Current& c) override;
  17 + virtual void shutdown(const Ice::Current&) override;
  18 + std::string getSecretToken() { return secretToken; };
  19 +};
  20 +
  21 +#endif
... ...
cpp11/Ice/auth_interceptor/README.md 0 → 100644
  1 +This demo illustrates how to use [request contexts][1].
  2 +
  3 +To run the demo, first start the server:
  4 +
  5 +```
  6 +server
  7 +```
  8 +
  9 +In a separate window, start the client:
  10 +
  11 +```
  12 +client
  13 +```
  14 +
  15 +[1]: https://doc.zeroc.com/display/Ice37/Request+Contexts
... ...
cpp11/Ice/auth_interceptor/Server.cpp 0 → 100644
  1 +//
  2 +// Copyright (c) ZeroC, Inc. All rights reserved.
  3 +//
  4 +
  5 +#include <Ice/Ice.h>
  6 +#include <ContextI.h>
  7 +
  8 +using namespace std;
  9 +
  10 +
  11 +
  12 +class InterceptorI : public Ice::DispatchInterceptor
  13 +{
  14 +public:
  15 + InterceptorI(std::shared_ptr<ContextI> servant) :
  16 + _servant(std::move(servant))
  17 + {
  18 + }
  19 +
  20 + virtual bool dispatch(Ice::Request& request) override
  21 + {
  22 + auto c = request.getCurrent();
  23 + const auto p = c.ctx.find("token");
  24 +
  25 + cout << "Operation: " << c.operation << endl;
  26 +
  27 + if (c.operation == "function")
  28 + if ( (p == c.ctx.end()) || (p->second != _servant->getSecretToken()) )
  29 + throw Ice::OperationNotExistException(__FILE__, __LINE__);
  30 + return _servant->ice_dispatch(request);
  31 + }
  32 +
  33 + std::shared_ptr<ContextI> _servant;
  34 +};
  35 +
  36 +
  37 +int main(int argc, char* argv[])
  38 +{
  39 +#ifdef ICE_STATIC_LIBS
  40 + Ice::registerIceSSL();
  41 +#endif
  42 +
  43 + int status = 0;
  44 +
  45 + try
  46 + {
  47 + //
  48 + // CtrlCHandler must be created before the communicator or any other threads are started
  49 + //
  50 + Ice::CtrlCHandler ctrlCHandler;
  51 +
  52 + //
  53 + // CommunicatorHolder's ctor initializes an Ice communicator,
  54 + // and its dtor destroys this communicator.
  55 + //
  56 + Ice::CommunicatorHolder ich(argc, argv, "config.server");
  57 + auto communicator = ich.communicator();
  58 +
  59 + ctrlCHandler.setCallback(
  60 + [communicator](int)
  61 + {
  62 + communicator->shutdown();
  63 + });
  64 +
  65 + //
  66 + // The communicator initialization removes all Ice-related arguments from argc/argv
  67 + //
  68 + if(argc > 1)
  69 + {
  70 + cerr << argv[0] << ": too many arguments" << endl;
  71 + status = 1;
  72 + }
  73 + else
  74 + {
  75 + auto adapter = communicator->createObjectAdapter("Context");
  76 +
  77 + auto servant = make_shared<ContextI>();
  78 +
  79 + auto interceptor = make_shared<InterceptorI>(servant);
  80 +
  81 + adapter->add(interceptor, Ice::stringToIdentity("context"));
  82 + adapter->activate();
  83 +
  84 + communicator->waitForShutdown();
  85 + }
  86 + }
  87 + catch(const std::exception& ex)
  88 + {
  89 + cerr << ex.what() << endl;
  90 + status = 1;
  91 + }
  92 +
  93 + return status;
  94 +}
... ...
cpp11/Ice/auth_interceptor/config.client 0 → 100644
  1 +Ice.Override.Secure=1
  2 +
  3 +#
  4 +# The client reads this property to create the reference to the
  5 +# "hello" object in the server.
  6 +#
  7 +Context.Proxy=context:ssl -p 10000
  8 +
  9 +#
  10 +# Enable implicit context on the communicator
  11 +#
  12 +Ice.ImplicitContext=Shared
  13 +
  14 +#
  15 +# Warn about connection exceptions
  16 +#
  17 +Ice.Warn.Connections=1
  18 +
  19 +#
  20 +# Network Tracing
  21 +#
  22 +# 0 = no network tracing
  23 +# 1 = trace connection establishment and closure
  24 +# 2 = like 1, but more detailed
  25 +# 3 = like 2, but also trace data transfer
  26 +#
  27 +Ice.Trace.Network=1
  28 +
  29 +#
  30 +# Protocol Tracing
  31 +#
  32 +# 0 = no protocol tracing
  33 +# 1 = trace protocol messages
  34 +#
  35 +Ice.Trace.Protocol=1
  36 +
  37 +#
  38 +# Security Tracing
  39 +#
  40 +# 0 = no security tracing
  41 +# 1 = trace messages
  42 +#
  43 +IceSSL.Trace.Security=1
  44 +
  45 +#
  46 +# SSL Configuration
  47 +#
  48 +Ice.Plugin.IceSSL=IceSSL:createIceSSL
  49 +
  50 +IceSSL.DefaultDir=../../../certs
  51 +IceSSL.CAs=cacert.pem
  52 +#IceSSL.CertFile=client.p12
  53 +#IceSSL.Password=password
  54 +#IceSSL.Keychain=../../../certs/client.keychain
  55 +#IceSSL.KeychainPassword=password
  56 +
  57 +IceSSL.VerifyPeer=1
0 58 \ No newline at end of file
... ...
cpp11/Ice/auth_interceptor/config.server 0 → 100644
  1 +Ice.Override.Secure=1
  2 +#
  3 +# The server creates one single object adapter with the name
  4 +# "Context". The following line sets the endpoints for this
  5 +# adapter.
  6 +#
  7 +Context.Endpoints=ssl -p 10000
  8 +
  9 +#
  10 +# Warn about connection exceptions
  11 +#
  12 +Ice.Warn.Connections=1
  13 +
  14 +#
  15 +# Network Tracing
  16 +#
  17 +# 0 = no network tracing
  18 +# 1 = trace connection establishment and closure
  19 +# 2 = like 1, but more detailed
  20 +# 3 = like 2, but also trace data transfer
  21 +#
  22 +#Ice.Trace.Network=1
  23 +
  24 +#
  25 +# Protocol Tracing
  26 +#
  27 +# 0 = no protocol tracing
  28 +# 1 = trace protocol messages
  29 +#
  30 +#Ice.Trace.Protocol=1
  31 +
  32 +#
  33 +# Security Tracing
  34 +#
  35 +# 0 = no security tracing
  36 +# 1 = trace messages
  37 +#
  38 +IceSSL.Trace.Security=1
  39 +
  40 +#
  41 +# SSL Configuration
  42 +#
  43 +Ice.Plugin.IceSSL=IceSSL:createIceSSL
  44 +
  45 +IceSSL.DefaultDir=../../../certs
  46 +IceSSL.CAs=cacert.pem
  47 +IceSSL.CertFile=server.p12
  48 +IceSSL.Password=password
  49 +IceSSL.Keychain=../../../certs/server.keychain
  50 +IceSSL.KeychainPassword=password
  51 +
  52 +IceSSL.VerifyPeer=0
0 53 \ No newline at end of file
... ...