C++ APISession methods

authorize(license)

Authorizes the current initialized session using a license key.

authorize(std::string license) binds the current initialized session to a license and returns the public blaze::license result object.

Function variants

blaze::license authorize(std::string license);
blaze::license authorize(std::string license, std::error_code& ec) noexcept;
void async_authorize(std::string license, std::function<void(std::error_code, blaze::license)> callback);
blaze::coro::awaitable_result<blaze::license> co_authorize(std::string license);

Status vs error

license.status is the API result. std::error_code and std::system_error are used for transport/runtime failures only.

Parameters

Prop

Type

Result

struct license {
  std::string key;
  std::vector<std::string> levels;
  std::string comment;
  std::string ip_address;
  std::chrono::seconds time_left{};
  blaze::status status{};

  bool good() const noexcept;
  bool paused() const noexcept;
  bool expired() const noexcept;
  bool blacklisted() const noexcept;
  bool already_authorized() const noexcept;
  std::string comment_or(std::string value) const;
};

Prop

Type

license.good() is equivalent to status == blaze::status::ok.

Checking returned fields

Missing fields are returned as default values. If license.status != blaze::status::ok, treat metadata fields as optional in practice and check them explicitly.

FieldPractical check
license.levelslicense.levels.empty()
license.commentlicense.comment.empty()
license.ip_addresslicense.ip_address.empty()
license.time_leftlicense.time_left.count() == 0

About license.key

license.key can also be empty when the library rejects the input locally before sending a request, for example on invalid license length. Use license.key.empty() if you need to check it explicitly.

Behavior notes

  • Call authorize only after successful connect and initialize.
  • License length is validated locally before the request is sent. On local validation failure, transport error is empty and license.status contains the returned API status.
  • If the server omits levels, comment, ip_address, or time_left, the library returns default values instead of std::optional, because it targets C++14 compatibility.
  • In current server behavior, time_left has two meanings:
    • newly activated license: unix expiration timestamp;
    • already activated license: remaining seconds.
  • If the server does not include time metadata for the current status, time_left remains 0s.

Example

Compilation note

Synchronous and Asynchronous examples compile in the same form from C++14 to C++26.

#include "blazeauth/api/api.hpp"

#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

std::string read_line(const std::string& label) {
  std::string value;
  std::cout << label << ": ";
  std::getline(std::cin, value);
  return value;
}

void print_status(const std::string& context, blaze::status status) {
  std::cout << context << ": "
            << static_cast<unsigned int>(status)
            << " (" << blaze::to_string(status) << ")\n";
}

int main() {
  const std::string websocket_api_key = read_line("Websocket API key");
  const std::string client_id = read_line("Client ID");
  const std::string license_key = read_line("License");

  blaze::session session;

  try {
    session.connect();

    const blaze::application app = session.initialize(websocket_api_key, client_id);
    if (!app.good()) {
      print_status("Initialize returned status", app.status);
      return 1;
    }

    const blaze::license license = session.authorize(license_key);
    if (!license.good()) {
      print_status("Authorize returned status", license.status);
      return 1;
    }

    std::cout << "Authorized license: " << license.key << '\n';
    std::cout << "Levels count: " << license.levels.size() << '\n';
    std::cout << "Comment: " << license.comment_or("<empty>") << '\n';
    std::cout << "IP address: " << (license.ip_address.empty() ? "<empty>" : license.ip_address) << '\n';
    std::cout << "Time left/raw time value: " << license.time_left.count() << '\n';

    session.shutdown();
  } catch (const std::system_error& e) {
    print_error_code("Authorize failed", e.code());
    return 1;
  }

  return 0;
}
#include "blazeauth/api/api.hpp"

#include <future>
#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

std::string read_line(const std::string& label) {
  std::string value;
  std::cout << label << ": ";
  std::getline(std::cin, value);
  return value;
}

void print_status(const std::string& context, blaze::status status) {
  std::cout << context << ": "
            << static_cast<unsigned int>(status)
            << " (" << blaze::to_string(status) << ")\n";
}

int main() {
  const std::string websocket_api_key = read_line("Websocket API key");
  const std::string client_id = read_line("Client ID");
  const std::string license_key = read_line("License");

  blaze::session session;

  std::promise<int> completion;
  std::future<int> result = completion.get_future();

  session.async_connect(
    [&session, &completion, websocket_api_key, client_id, license_key](std::error_code connect_ec, blaze::api_server) {
      if (connect_ec) {
        print_error_code("Connect failed", connect_ec);
        completion.set_value(1);
        return;
      }

      session.async_initialize(websocket_api_key, client_id,
        [&session, &completion, license_key](std::error_code init_ec, blaze::application app) {
          if (init_ec) {
            print_error_code("Initialize failed", init_ec);
            completion.set_value(1);
            return;
          }

          if (!app.good()) {
            print_status("Initialize returned status", app.status);
            completion.set_value(1);
            return;
          }

          session.async_authorize(license_key,
            [&session, &completion](std::error_code auth_ec, blaze::license license) {
              if (auth_ec) {
                print_error_code("Authorize failed", auth_ec);
                completion.set_value(1);
                return;
              }

              if (!license.good()) {
                print_status("Authorize returned status", license.status);
                completion.set_value(1);
                return;
              }

              std::cout << "Authorized license: " << license.key << '\n';
              std::cout << "Levels count: " << license.levels.size() << '\n';
              std::cout << "Comment: " << license.comment_or("<empty>") << '\n';
              std::cout << "IP address: " << (license.ip_address.empty() ? "<empty>" : license.ip_address) << '\n';
              std::cout << "Time left/raw time value: " << license.time_left.count() << '\n';

              session.async_shutdown([&completion](std::error_code shutdown_ec) {
                if (shutdown_ec) {
                  print_error_code("Shutdown failed", shutdown_ec);
                  completion.set_value(1);
                  return;
                }

                completion.set_value(0);
              });
            });
        });
    });

  return result.get();
}

Project must be built with C++20 support and with coroutine support available both in the language mode and in the standard library implementation for this example to compile correctly.

#include "blazeauth/api/api.hpp"

#include <future>
#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

std::string read_line(const std::string& label) {
  std::string value;
  std::cout << label << ": ";
  std::getline(std::cin, value);
  return value;
}

#if BLAZEAUTH_HAS_COROUTINES

blaze::coro::task<std::error_code> run_example(
  blaze::session& session,
  const std::string& websocket_api_key,
  const std::string& client_id,
  const std::string& license_key
) {
  const auto [connect_ec, server] = co_await session.co_connect();
  if (connect_ec) {
    co_return connect_ec;
  }

  const auto [init_ec, app] = co_await session.co_initialize(websocket_api_key, client_id);
  if (init_ec) {
    co_return init_ec;
  }

  if (!app.good()) {
    co_return blaze::make_error_code(app.status);
  }

  const auto [auth_ec, license] = co_await session.co_authorize(license_key);
  if (auth_ec) {
    co_return auth_ec;
  }

  if (!license.good()) {
    co_return blaze::make_error_code(license.status);
  }

  std::cout << "Authorized license: " << license.key << '\n';
  std::cout << "Levels count: " << license.levels.size() << '\n';
  std::cout << "Comment: " << license.comment_or("<empty>") << '\n';
  std::cout << "IP address: " << (license.ip_address.empty() ? "<empty>" : license.ip_address) << '\n';
  std::cout << "Time left/raw time value: " << license.time_left.count() << '\n';

  const auto [shutdown_ec] = co_await session.co_shutdown();
  co_return shutdown_ec;
}

int main() {
  const std::string websocket_api_key = read_line("Websocket API key");
  const std::string client_id = read_line("Client ID");
  const std::string license_key = read_line("License");

  blaze::session session;

  std::promise<std::error_code> completion;
  std::future<std::error_code> result = completion.get_future();

  blaze::coro::co_spawn([&session, &completion, websocket_api_key, client_id, license_key]() -> blaze::coro::task<void> {
    const std::error_code ec = co_await run_example(session, websocket_api_key, client_id, license_key);
    completion.set_value(ec);
    co_return;
  }());

  const std::error_code ec = result.get();
  if (ec) {
    print_error_code("Authorize failed", ec);
    return 1;
  }

  return 0;
}

#else

int main() {
  std::cout << "This example requires coroutine support in the Blazeauth library build.\n";
  return 0;
}

#endif

Authorize statuses

ValueStatusReturned when
1okLicense authorization succeeded.
6internal_server_errorServer could not complete license authorization because of an internal error.
7already_authorizedCurrent session is already authorized.
100license_not_foundLicense does not exist in the current application.
101license_too_shortlicense is shorter than 6 characters. This is rejected locally before the request is sent.
102license_too_longlicense is longer than 64 characters. This is rejected locally before the request is sent.
103license_expiredLicense exists but is expired.
104license_pausedLicense exists but is paused.
105license_blacklistedLicense exists but is blacklisted.
502client_id_mismatchLicense is already bound to another client identifier.
602app_sessions_quota_exceededApplication authorized session quota was reached.

Connection-level shutdowns

Normal authorization results are returned in license.status. The websocket may still be closed by session-level guards:

Close codeMeaningWhen it can happen
4200not_initializedauthorize was called before a successful initialize.
4201rate_limitedSession or IP rate limiting closed the websocket around this operation.

On this page