Skip to content

Commit

Permalink
Added Polaris_AuthenticateTo() to allow use of an alternate API server.
Browse files Browse the repository at this point in the history
Merge pull request #44.
  • Loading branch information
adamshapiro0 committed Sep 17, 2021
2 parents c4fe192 + 6fd6919 commit cb6ee72
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 8 deletions.
14 changes: 10 additions & 4 deletions c/src/point_one/polaris/polaris.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ void Polaris_SetLogLevel(int log_level) {
/******************************************************************************/
int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
const char* unique_id) {
return Polaris_AuthenticateTo(context, api_key, unique_id, POLARIS_API_URL);
}

/******************************************************************************/
int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key,
const char* unique_id, const char* api_url) {
// Sanity check the inputs.
if (strlen(api_key) == 0) {
P1_Print("API key must not be empty.\n");
Expand Down Expand Up @@ -210,16 +216,16 @@ int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
return POLARIS_NOT_ENOUGH_SPACE;
}

P1_DebugPrint("Sending auth request. [api_key=%s, unique_id=%s]\n", api_key,
unique_id);
P1_DebugPrint("Sending auth request. [api_key=%s, unique_id=%s, url=%s]\n",
api_key, unique_id, api_url);
context->auth_token[0] = '\0';
#ifdef POLARIS_USE_TLS
int status_code =
SendPOSTRequest(context, POLARIS_API_URL, 443, "/api/v1/auth/token",
SendPOSTRequest(context, api_url, 443, "/api/v1/auth/token",
context->recv_buffer, (size_t)content_size);
#else
int status_code =
SendPOSTRequest(context, POLARIS_API_URL, 80, "/api/v1/auth/token",
SendPOSTRequest(context, api_url, 80, "/api/v1/auth/token",
context->recv_buffer, (size_t)content_size);
#endif
if (status_code < 0) {
Expand Down
12 changes: 12 additions & 0 deletions c/src/point_one/polaris/polaris.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "point_one/polaris/socket.h"

#define POLARIS_API_URL "api.pointonenav.com"

#define POLARIS_ENDPOINT_URL "polaris.pointonenav.com"
#define POLARIS_ENDPOINT_PORT 8088
#define POLARIS_ENDPOINT_TLS_PORT 8090
Expand Down Expand Up @@ -203,6 +205,16 @@ void Polaris_SetLogLevel(int log_level);
int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
const char* unique_id);

/**
* @brief Authenticate with the specified Polaris API server.
*
* @copydetails Polaris_Authenticate()
*
* @param api_url The URL of the desired API authentication server.
*/
int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key,
const char* unique_id, const char* api_url);

/**
* @brief Use an existing authentication token to connect to Polaris.
*
Expand Down
2 changes: 0 additions & 2 deletions c/src/point_one/polaris/polaris_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#define POLARIS_ID_BEACON 0x05
#define POLARIS_ID_UNIQUE_ID 0x06

#define POLARIS_API_URL "api.pointonenav.com"

// Enforce 4-byte alignment and packing of all data structures and values so
// that values larger than 1 B are aligned on platforms that require it.
#pragma pack(push, 4)
Expand Down
17 changes: 15 additions & 2 deletions src/point_one/polaris/polaris_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ PolarisClient::PolarisClient(const std::string& api_key,
Polaris_SetLogLevel(POLARIS_LOG_LEVEL_TRACE);
}

SetPolarisAuthenticationServer();
SetPolarisEndpoint();

polaris_.SetRTCMCallback([&](const uint8_t* buffer, size_t size_bytes) {
Expand Down Expand Up @@ -114,6 +115,17 @@ void PolarisClient::SetNoAuthID(const std::string& unique_id) {
no_auth_ = true;
}

/******************************************************************************/
void PolarisClient::SetPolarisAuthenticationServer(const std::string& api_url) {
std::unique_lock<std::recursive_mutex> lock(mutex_);
if (api_url.empty()) {
api_url_ = POLARIS_API_URL;
}
else {
api_url_ = api_url;
}
}

/******************************************************************************/
void PolarisClient::SetPolarisEndpoint(const std::string& endpoint_url,
int endpoint_port) {
Expand Down Expand Up @@ -200,8 +212,9 @@ void PolarisClient::Run(double timeout_sec) {
// Retrieve an access token using the specified API key.
if (!auth_valid_ && !no_auth_) {
VLOG(1) << "Authenticating with Polaris service. [unique_id="
<< (unique_id_.empty() ? "<not specified>" : unique_id_) << "]";
int ret = polaris_.Authenticate(api_key_, unique_id_);
<< (unique_id_.empty() ? "<not specified>" : unique_id_)
<< ", api_url=" << api_url_ << "]";
int ret = polaris_.AuthenticateTo(api_key_, unique_id_, api_url_);
if (ret == POLARIS_FORBIDDEN) {
LOG(ERROR) << "Authentication rejected. Is your API key valid?";
running_ = false;
Expand Down
10 changes: 10 additions & 0 deletions src/point_one/polaris/polaris_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ class PolarisClient {
*/
void SetNoAuthID(const std::string& unique_id);

/**
* @brief Specify an alternate URL to use when authenticating with the Polaris
* corrections service.
*
* @param api_url The desired endpoint URL.
*/
void SetPolarisAuthenticationServer(const std::string& api_url = "");

/**
* @brief Specify an alternate URL to use when connecting to the Polaris
* corrections endpoint.
Expand Down Expand Up @@ -252,6 +260,8 @@ class PolarisClient {

std::function<void(const uint8_t* buffer, size_t size_bytes)> callback_;

std::string api_url_;

std::string endpoint_url_;
int endpoint_port_ = 0;

Expand Down
8 changes: 8 additions & 0 deletions src/point_one/polaris/polaris_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ int PolarisInterface::Authenticate(const std::string& api_key,
return Polaris_Authenticate(&context_, api_key.c_str(), unique_id.c_str());
}

/******************************************************************************/
int PolarisInterface::AuthenticateTo(const std::string& api_key,
const std::string& unique_id,
const std::string& api_url) {
return Polaris_AuthenticateTo(&context_, api_key.c_str(), unique_id.c_str(),
api_url.c_str());
}

/******************************************************************************/
int PolarisInterface::SetAuthToken(const std::string& auth_token) {
return Polaris_SetAuthToken(&context_, auth_token.c_str());
Expand Down
12 changes: 12 additions & 0 deletions src/point_one/polaris/polaris_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ class PolarisInterface {
*/
int Authenticate(const std::string& api_key, const std::string& unique_id);

/**
* @brief Authenticate with the specified Polaris API server.
*
* @copydetails Authenticate()
*
* See also @ref Polaris_AuthenticateTo().
*
* @param api_url The URL of the desired API authentication server.
*/
int AuthenticateTo(const std::string& api_key, const std::string& unique_id,
const std::string& api_url);

/**
* @brief Use an existing authentication token to connect to Polaris.
*
Expand Down

0 comments on commit cb6ee72

Please sign in to comment.