Kurlyk
Loading...
Searching...
No Matches
BearerTokenAuthProvider.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_HTTP_AUTH_BEARER_TOKEN_AUTH_PROVIDER_HPP_INCLUDED
3#define _KURLYK_HTTP_AUTH_BEARER_TOKEN_AUTH_PROVIDER_HPP_INCLUDED
4
7
8#include "IAuthProvider.hpp"
9#include <string>
10
11namespace kurlyk {
12namespace http {
13namespace auth {
14
18 public:
21 explicit BearerTokenAuthProvider(const std::string& token)
22 : m_token(token) {}
23
26 void set_token(const std::string& token) {
27 m_token = token;
28 }
29
30 bool authorize(HttpRequest& request) const override {
31 if (m_token.empty()) return false;
32 request.headers.erase("Authorization");
33 request.headers.emplace("Authorization", "Bearer " + m_token);
34 return true;
35 }
36
37 bool authorize(Headers& headers) const override {
38 if (m_token.empty()) return false;
39 headers.erase("Authorization");
40 headers.emplace("Authorization", "Bearer " + m_token);
41 return true;
42 }
43
44 private:
45 std::string m_token;
46 };
47
48} // namespace auth
49} // namespace http
50} // namespace kurlyk
51
52#endif // _KURLYK_HTTP_AUTH_BEARER_TOKEN_AUTH_PROVIDER_HPP_INCLUDED
Defines the IAuthProvider interface for HTTP authentication strategies.
Represents an HTTP request configuration.
Headers headers
HTTP request headers.
void set_token(const std::string &token)
Sets a new Bearer token, replacing any previous value.
BearerTokenAuthProvider(const std::string &token)
Constructs a provider with a Bearer token.
bool authorize(HttpRequest &request) const override
Modifies an HttpRequest in-place to include authentication credentials.
bool authorize(Headers &headers) const override
Modifies a header map in-place to include authentication credentials.
Interface for authentication providers that modify HTTP requests or headers.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.