Kurlyk
Loading...
Searching...
No Matches
ApiKeyAuthProvider.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_HTTP_AUTH_API_KEY_AUTH_PROVIDER_HPP_INCLUDED
3#define _KURLYK_HTTP_AUTH_API_KEY_AUTH_PROVIDER_HPP_INCLUDED
4
7
8#include "IAuthProvider.hpp"
10#include <string>
11
12namespace kurlyk {
13namespace http {
14namespace auth {
15
18 enum class ApiKeyPlacement {
21 };
22
26 public:
32 const std::string& key_name,
33 const std::string& key_value,
35 : m_key_name(key_name)
36 , m_key_value(key_value)
37 , m_placement(placement) {}
38
41 void set_key_value(const std::string& key_value) {
42 m_key_value = key_value;
43 }
44
45 bool authorize(HttpRequest& request) const override {
46 if (m_key_name.empty() || m_key_value.empty()) return false;
48 request.headers.erase(m_key_name);
49 request.headers.emplace(m_key_name, m_key_value);
50 return true;
51 }
52
53 // QUERY placement
54 const std::string encoded_key = utils::percent_encode(m_key_name);
55 const std::string encoded_value = utils::percent_encode(m_key_value);
56 const std::string param = encoded_key + "=" + encoded_value;
57
58 if (request.url.find('?') == std::string::npos) {
59 request.url += "?" + param;
60 } else {
61 request.url += "&" + param;
62 }
63 return true;
64 }
65
66 bool authorize(Headers& headers) const override {
67 if (m_key_name.empty() || m_key_value.empty()) return false;
69 return false;
70 }
71 headers.erase(m_key_name);
72 headers.emplace(m_key_name, m_key_value);
73 return true;
74 }
75
76 private:
77 std::string m_key_name;
78 std::string m_key_value;
80 };
81
82} // namespace auth
83} // namespace http
84} // namespace kurlyk
85
86#endif // _KURLYK_HTTP_AUTH_API_KEY_AUTH_PROVIDER_HPP_INCLUDED
Defines the IAuthProvider interface for HTTP authentication strategies.
Represents an HTTP request configuration.
std::string url
Full request URL.
Headers headers
HTTP request headers.
bool authorize(HttpRequest &request) const override
Modifies an HttpRequest in-place to include authentication credentials.
void set_key_value(const std::string &key_value)
Updates the stored API key value.
bool authorize(Headers &headers) const override
Modifies a header map in-place to include authentication credentials.
ApiKeyAuthProvider(const std::string &key_name, const std::string &key_value, ApiKeyPlacement placement=ApiKeyPlacement::HEADER)
Constructs a provider with a key name, value, and placement.
Interface for authentication providers that modify HTTP requests or headers.
ApiKeyPlacement
Determines where the API key is attached.
@ QUERY
Appended as a query parameter to the request URL.
@ HEADER
Sent as a custom HTTP header.
std::string percent_encode(const std::string &value) noexcept
Encodes a string using Percent Encoding according to RFC 3986.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
Provides functions for percent-encoding and decoding strings.