Pass device grant test.

This commit is contained in:
James Pace 2022-03-26 01:32:10 +00:00
parent 3b4be67de3
commit fa238f650d
3 changed files with 35 additions and 27 deletions

View File

@ -25,7 +25,7 @@ std::optional<std::string> getKey(const std::string &user, const YAML::Node &key
// Class implementation.
Authorizer::Authorizer(const std::string &keyFilePath, const std::string &aclFilePath) :
_keyFile{keyFilePath}, _aclFile{aclFilePath}
_keyFile{YAML::LoadFile(keyFilePath)}, _aclFile{YAML::LoadFile(aclFilePath)}
{
}
@ -98,6 +98,7 @@ bool Authorizer::is_unknown(const std::string &username)
std::tuple<bool, bool> checkACL(const std::string &user, const YAML::Node &aclFile)
{
// TODO: Make sure default exists.
YAML::Node userDict;
if (aclFile[user])
{

View File

@ -61,6 +61,8 @@ bool validate(const std::string &token, const std::string &username, const std::
}
auto claims = decoded_token.get_payload_claims();
try
{
// Check username matches.
if (not claims.contains("upn"))
{
@ -79,7 +81,7 @@ bool validate(const std::string &token, const std::string &username, const std::
std::cerr << "Missing mqtt claim." << std::endl;
return false;
}
if (not(claims["mqtt"].as_string() == "true"))
if (not(claims["mqtt"].as_bool()))
{
std::cerr << "Not claiming can do mqtt." << std::endl;
return false;
@ -91,6 +93,12 @@ bool validate(const std::string &token, const std::string &username, const std::
std::cerr << "Missing expiration time claim." << std::endl;
return false;
}
}
catch(const std::bad_cast& exception)
{
std::cerr << "Failed to parse claims. Reason: " << exception.what() << std::endl;
return false;
}
return true;
}
@ -105,7 +113,7 @@ std::string gen_token(
const auto token = jwt::create()
.set_type("JWT")
.set_payload_claim("upn", jwt::claim(username))
.set_payload_claim("mqtt", jwt::claim(std::string("true")))
.set_payload_claim("mqtt", jwt::claim(picojson::value(true)))
.set_issued_at(issue_time)
.set_expires_at(expr_time)
.sign(jwt::algorithm::rs256(pub_key, priv_key, "", ""));

View File

@ -13,7 +13,6 @@
// limitations under the License.
#include <j7s-plugin/utils.h>
#include <ctime>
#include <iostream>
#include "gtest/gtest.h"