我正在CapnProto中实现一个服务。服务遵循以下步骤(大致):
我想做到以下几点:
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)。即:
发布于 2019-08-24 08:26:18
结果发现这很简单。
在代码中创建服务接口时,只需传递身份验证信息并将其保存在服务对象中,如下所示:
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);
//..
}
};https://stackoverflow.com/questions/57634035
复制相似问题