Kurlyk
Loading...
Searching...
No Matches
AuthResult.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_HTTP_AUTH_DATA_AUTH_RESULT_HPP_INCLUDED
3#define _KURLYK_HTTP_AUTH_DATA_AUTH_RESULT_HPP_INCLUDED
4
7
8#include <string>
9#include "OAuthToken.hpp"
10
11#if KURLYK_JSON_SUPPORT
12# include <nlohmann/json.hpp>
13#endif
14
15namespace kurlyk {
16
28
38
39} // namespace kurlyk
40
41#if KURLYK_JSON_SUPPORT
42
43namespace kurlyk {
44
45 inline void to_json(nlohmann::json& j, const AuthError& e) {
46 switch (e) {
47 case AuthError::None: j = "None"; break;
48 case AuthError::InvalidConfig: j = "InvalidConfig"; break;
49 case AuthError::HttpError: j = "HttpError"; break;
50 case AuthError::InvalidResponse: j = "InvalidResponse"; break;
51 case AuthError::TokenExpired: j = "TokenExpired"; break;
52 case AuthError::UnsupportedFlow: j = "UnsupportedFlow"; break;
53 case AuthError::StateMismatch: j = "StateMismatch"; break;
54 default: j = "Unknown"; break;
55 }
56 }
57
58 inline void from_json(const nlohmann::json& j, AuthError& e) {
59 const std::string s = j.get<std::string>();
60 if (s == "None") e = AuthError::None;
61 else if (s == "InvalidConfig") e = AuthError::InvalidConfig;
62 else if (s == "HttpError") e = AuthError::HttpError;
63 else if (s == "InvalidResponse") e = AuthError::InvalidResponse;
64 else if (s == "TokenExpired") e = AuthError::TokenExpired;
65 else if (s == "UnsupportedFlow") e = AuthError::UnsupportedFlow;
66 else if (s == "StateMismatch") e = AuthError::StateMismatch;
67 else e = AuthError::None;
68 }
69
70 inline void to_json(nlohmann::json& j, const AuthResult& r) {
71 j = nlohmann::json{
72 {"success", r.success},
73 {"error", r.error},
74 {"error_message", r.error_message},
75 {"token", r.token},
76 {"raw_response", r.raw_response}
77 };
78 }
79
80 inline void from_json(const nlohmann::json& j, AuthResult& r) {
81 if (j.contains("success")) r.success = j["success"].get<bool>();
82 if (j.contains("error")) r.error = j["error"].get<AuthError>();
83 if (j.contains("error_message")) r.error_message = j["error_message"].get<std::string>();
84 if (j.contains("token")) r.token = j["token"].get<OAuthToken>();
85 if (j.contains("raw_response")) r.raw_response = j["raw_response"].get<std::string>();
86 }
87
88} // namespace kurlyk
89
90#endif // KURLYK_JSON_SUPPORT
91
92#endif // _KURLYK_HTTP_AUTH_DATA_AUTH_RESULT_HPP_INCLUDED
Defines the OAuthToken structure for storing OAuth2 token data.
void to_json(nlohmann::json &j, const ProxyConfig &config)
Serializes ProxyConfig to JSON.
void from_json(const nlohmann::json &j, ProxyConfig &config)
Deserializes ProxyConfig from JSON.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
AuthError
Classification of authentication errors.
@ HttpError
HTTP request failed (non-2xx status or transport error).
@ InvalidResponse
Server response could not be parsed.
@ InvalidConfig
Missing or invalid client configuration.
@ None
No error; operation succeeded.
@ UnsupportedFlow
The requested OAuth flow is not supported.
@ StateMismatch
Returned state parameter does not match expected value.
@ TokenExpired
The token has expired and refresh failed.
Encapsulates the outcome of an authentication operation.
std::string error_message
Human-readable error description (if any).
AuthError error
Error classification.
std::string raw_response
Raw server response body for diagnostics.
bool success
Whether the operation succeeded.
OAuthToken token
Token data on success (may be partially filled on failure).
Holds the result of an OAuth2 token exchange.
std::string raw_response
Raw server response body for debugging.