首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在capnproto RPC中识别调用方

在capnproto RPC中识别调用方
EN

Stack Overflow用户
提问于 2019-08-23 23:32:39
回答 1查看 118关注 0票数 1

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

  1. 服务器上的身份验证
  2. 一旦通过服务接口(对象-功能)进行验证,就可以执行操作。

我想做到以下几点:

代码语言:javascript
复制
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中,我可以识别使用该功能的用户(我想知道她的userName)。即:

  • 我解决的问题是,使用服务功能的用户已知具有该权限(因为服务是调用登录的结果)
  • 问题是,我有很多这样的用户,对于每个用户,我希望在第一步中在服务功能的用户和她的登录之间进行匹配。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-24 08:26:18

结果发现这很简单。

在代码中创建服务接口时,只需传递身份验证信息并将其保存在服务对象中,如下所示:

代码语言:javascript
复制
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);     
       //..
   }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57634035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档