From 6fd6919cc9bcebe9f5f5006cb0b53ca0ce3fa216 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Thu, 16 Sep 2021 15:48:57 -0400 Subject: [PATCH] Added Polaris_AuthenticateTo() to allow use of an alternate API server. --- c/src/point_one/polaris/polaris.c | 14 ++++++++++---- c/src/point_one/polaris/polaris.h | 12 ++++++++++++ c/src/point_one/polaris/polaris_internal.h | 2 -- src/point_one/polaris/polaris_client.cc | 17 +++++++++++++++-- src/point_one/polaris/polaris_client.h | 10 ++++++++++ src/point_one/polaris/polaris_interface.cc | 8 ++++++++ src/point_one/polaris/polaris_interface.h | 12 ++++++++++++ 7 files changed, 67 insertions(+), 8 deletions(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index 017be59..2e69a6c 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -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"); @@ -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) { diff --git a/c/src/point_one/polaris/polaris.h b/c/src/point_one/polaris/polaris.h index ac0c76c..92cc030 100644 --- a/c/src/point_one/polaris/polaris.h +++ b/c/src/point_one/polaris/polaris.h @@ -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 @@ -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. * diff --git a/c/src/point_one/polaris/polaris_internal.h b/c/src/point_one/polaris/polaris_internal.h index 02ca881..c6f4aaf 100644 --- a/c/src/point_one/polaris/polaris_internal.h +++ b/c/src/point_one/polaris/polaris_internal.h @@ -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) diff --git a/src/point_one/polaris/polaris_client.cc b/src/point_one/polaris/polaris_client.cc index 30ea4ee..6b97071 100644 --- a/src/point_one/polaris/polaris_client.cc +++ b/src/point_one/polaris/polaris_client.cc @@ -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) { @@ -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 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) { @@ -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() ? "" : unique_id_) << "]"; - int ret = polaris_.Authenticate(api_key_, unique_id_); + << (unique_id_.empty() ? "" : 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; diff --git a/src/point_one/polaris/polaris_client.h b/src/point_one/polaris/polaris_client.h index b4205ca..ba3a339 100644 --- a/src/point_one/polaris/polaris_client.h +++ b/src/point_one/polaris/polaris_client.h @@ -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. @@ -252,6 +260,8 @@ class PolarisClient { std::function callback_; + std::string api_url_; + std::string endpoint_url_; int endpoint_port_ = 0; diff --git a/src/point_one/polaris/polaris_interface.cc b/src/point_one/polaris/polaris_interface.cc index 8d39c61..e53fd38 100644 --- a/src/point_one/polaris/polaris_interface.cc +++ b/src/point_one/polaris/polaris_interface.cc @@ -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()); diff --git a/src/point_one/polaris/polaris_interface.h b/src/point_one/polaris/polaris_interface.h index c53e1d0..71ef6f2 100644 --- a/src/point_one/polaris/polaris_interface.h +++ b/src/point_one/polaris/polaris_interface.h @@ -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. *