【问题标题】:identifying caller in capnproto RPC在 capnproto RPC 中识别调用者
【发布时间】:2019-08-23 23:32:39
【问题描述】:

我正在 CapnProto 中实施一项服务。该服务遵循以下步骤(大致):

  1. 在服务器上进行身份验证
  2. 通过身份验证后通过服务接口(对象能力)执行操作。

我想实现以下目标:

interface Authorization {
   login @0 (userName :User) -> (result :Service);
}

interface Service {
   # doOperation needs userName in order to operate, as an implicit
   # parameter, when the RPC arrives. I do not want to use an explicit 
   # userName parameter. Otherwise, a user could claim to
   # be someone else in a function parameter. To achieve this I need
   # to know who is the userName that holds this capability from inside
   # doOperation. I want to avoid token authentication 
   # for each operation.
   # Thus, the RPC I am implementing is stateful.
   doOperation @0 (param1 :A); 
   #...
}

我想要的是,从 doOperation 中,我可以识别正在使用该功能的用户(我想知道她的用户名)。即:

  • 我解决的是已知使用Service能力的用户拥有该权限(因为Service是调用login的结果)

  • 问题是我有很多这样的用户,并且,对于每个用户,我想在第一步中做服务能力的用户和她的登录之间的匹配。

【问题讨论】:

    标签: c++ serialization rpc capnproto


    【解决方案1】:

    原来这很简单。

    在代码中创建Service接口时,只需将认证信息传递并保存在Service对象中,如下所示:

    class ServiceImpl : public Service::Server {
       string userId_;
    public:
       explicit ServiceImpl(string userId) : userId_(move(userId)) {}
    protected:
       kj::Promise<void> doOperatoration(DoOperationContext ctx) override {
            //use userId_ here
    
       }
    };
    
    class AuthorizationImpl : public Authorization::Server {
    protected:
       kj::Promise<void> login(LoginContext ctx) override {
           std::string user = ctx.getParams().getUserName();
           //Here I forward the authentication state to the service
           ctx.getResults().setService(kj::heap<ServiceImpl>(user);     
           //..
       }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多