Kurlyk
Loading...
Searching...
No Matches
OAuthToken.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_HTTP_AUTH_DATA_OAUTH_TOKEN_HPP_INCLUDED
3#define _KURLYK_HTTP_AUTH_DATA_OAUTH_TOKEN_HPP_INCLUDED
4
7
8#include <string>
9#include <chrono>
10
11#if KURLYK_JSON_SUPPORT
12# include <nlohmann/json.hpp>
13#endif
14
15namespace kurlyk {
16
19 struct OAuthToken {
20 std::string access_token;
21 std::string refresh_token;
22 std::string token_type;
23 std::string scope;
24 int64_t expires_at_ms = 0;
25 std::string raw_response;
26
32 bool is_expired(int64_t skew_ms = 60000) const {
33 if (expires_at_ms <= 0) return false;
34 auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
35 std::chrono::system_clock::now().time_since_epoch()).count();
36 return (now_ms + skew_ms) >= expires_at_ms;
37 }
38 };
39
40} // namespace kurlyk
41
42#if KURLYK_JSON_SUPPORT
43
44namespace kurlyk {
45
46 inline void to_json(nlohmann::json& j, const OAuthToken& t) {
47 j = nlohmann::json{
48 {"access_token", t.access_token},
49 {"refresh_token", t.refresh_token},
50 {"token_type", t.token_type},
51 {"scope", t.scope},
52 {"expires_at_ms", t.expires_at_ms}
53 };
54 }
55
56 inline void from_json(const nlohmann::json& j, OAuthToken& t) {
57 if (j.contains("access_token")) t.access_token = j["access_token"].get<std::string>();
58 if (j.contains("refresh_token")) t.refresh_token = j["refresh_token"].get<std::string>();
59 if (j.contains("token_type")) t.token_type = j["token_type"].get<std::string>();
60 if (j.contains("scope")) t.scope = j["scope"].get<std::string>();
61 if (j.contains("expires_at_ms")) t.expires_at_ms = j["expires_at_ms"].get<int64_t>();
62 }
63
64} // namespace kurlyk
65
66#endif // KURLYK_JSON_SUPPORT
67
68#endif // _KURLYK_HTTP_AUTH_DATA_OAUTH_TOKEN_HPP_INCLUDED
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,...
Holds the result of an OAuth2 token exchange.
bool is_expired(int64_t skew_ms=60000) const
Checks whether the token has expired.
int64_t expires_at_ms
Absolute expiration time in milliseconds since epoch (0 = unknown).
std::string raw_response
Raw server response body for debugging.
std::string scope
Granted scope (may be empty).
std::string access_token
The access token string.
std::string token_type
Token type, typically "Bearer".
std::string refresh_token
The refresh token string (may be empty).