crossbario / autobahn-cpp

WAMP for C++ in Boost/Asio
https://crossbar.io/autobahn
Boost Software License 1.0
251 stars 104 forks source link

Dynamic authenticator example #141

Open xhh opened 7 years ago

xhh commented 7 years ago

As described in Corssbar WAMP-CRA dynamic authentication docs:

The return value must be a dictionary with two mandatory attributes:

  • secret: The secret shared with the client (possibly after salting)
  • role: The authrole to assign to the client if successfully authenticated

The authentication procedure I registered is defined as this (the result is a std::map):

void authenticate(autobahn::wamp_invocation invocation) {
  auto authid = invocation->argument<std::string>(1);
  std::map<std::string, std::string> result {
    { std::string("secret"), std::string("abc") },
    { std::string("role"), std::string("user") }
  };
  invocation->result(result);
}

And I got this error with the client trying to authenticate via the WAMP-CRA authentication:

RX message: abort [{"message":"got invalid return type \"<class 'autobahn.wamp.types.CallResult'>\" from dynamic authenticator"}, "wamp.error.authentication_failed"]

How shall I return the secret and role in the authenticate procedure here? Can't find any example code, please allow me asking here.

fin-ger commented 7 years ago

I figured out that you have to return a std::tuple of a std::map:

std::map<std::string, std::string> result;
result["secret"] = secret;
result["role"] = role;
result["salt"] = salt;
invocation->result (std::make_tuple (result));
oberstet commented 7 years ago

Yeah, a dynamic authenticator is supposed to return a single positional value (hence the tuple of length 1), and that value needs to be a map. Sorry for leaving you in the rain and guessing ..

We should add an example. We have one for authenticating a client (https://github.com/crossbario/autobahn-cpp/blob/master/examples/wampcra.cpp), but we don't have one for a dynamic authenticator in C++