Kurlyk
Toggle main menu visibility
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
15
namespace
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
44
namespace
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
cryptox::to_json
void to_json(nlohmann::json &j, const ProxyConfig &config)
Serializes ProxyConfig to JSON.
Definition
ProxyConfig.hpp:106
cryptox::from_json
void from_json(const nlohmann::json &j, ProxyConfig &config)
Deserializes ProxyConfig from JSON.
Definition
ProxyConfig.hpp:116
kurlyk
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
kurlyk::OAuthToken
Holds the result of an OAuth2 token exchange.
Definition
OAuthToken.hpp:19
kurlyk::OAuthToken::is_expired
bool is_expired(int64_t skew_ms=60000) const
Checks whether the token has expired.
Definition
OAuthToken.hpp:32
kurlyk::OAuthToken::expires_at_ms
int64_t expires_at_ms
Absolute expiration time in milliseconds since epoch (0 = unknown).
Definition
OAuthToken.hpp:24
kurlyk::OAuthToken::raw_response
std::string raw_response
Raw server response body for debugging.
Definition
OAuthToken.hpp:25
kurlyk::OAuthToken::scope
std::string scope
Granted scope (may be empty).
Definition
OAuthToken.hpp:23
kurlyk::OAuthToken::access_token
std::string access_token
The access token string.
Definition
OAuthToken.hpp:20
kurlyk::OAuthToken::token_type
std::string token_type
Token type, typically "Bearer".
Definition
OAuthToken.hpp:22
kurlyk::OAuthToken::refresh_token
std::string refresh_token
The refresh token string (may be empty).
Definition
OAuthToken.hpp:21
include
kurlyk
http
auth
data
OAuthToken.hpp
Generated by
1.17.0