Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
oauth_device.h
1#ifndef SOURCEMETA_CORE_OAUTH_DEVICE_H_
2#define SOURCEMETA_CORE_OAUTH_DEVICE_H_
3
4#ifndef SOURCEMETA_CORE_OAUTH_EXPORT
5#include <sourcemeta/core/oauth_export.h>
6#endif
7
8#include <sourcemeta/core/crypto.h>
9#include <sourcemeta/core/json.h>
10#include <sourcemeta/core/oauth_authorization.h>
11#include <sourcemeta/core/oauth_error.h>
12
13#include <array> // std::array
14#include <chrono> // std::chrono::seconds, std::chrono::system_clock
15#include <cstdint> // std::uint8_t
16#include <optional> // std::optional
17#include <span> // std::span
18#include <string> // std::string
19#include <string_view> // std::string_view
20
21namespace sourcemeta::core {
22
23#if defined(_MSC_VER)
24#pragma warning(disable : 4251)
25#endif
26
44SOURCEMETA_CORE_OAUTH_EXPORT
46 const std::string_view client_id, const std::string_view scope,
47 const std::span<const OAuthParameter> resources, std::string &sink) -> void;
48
67SOURCEMETA_CORE_OAUTH_EXPORT
69 const std::string_view device_code,
70 const std::span<const OAuthParameter> resources, SecureString &sink)
71 -> void;
72
91class SOURCEMETA_CORE_OAUTH_EXPORT OAuthDeviceAuthorizationResponse {
92public:
95
97 [[nodiscard]] auto device_code() const -> std::optional<std::string_view>;
99 [[nodiscard]] auto user_code() const -> std::optional<std::string_view>;
101 [[nodiscard]] auto verification_uri() const
102 -> std::optional<std::string_view>;
104 [[nodiscard]] auto verification_uri_complete() const
105 -> std::optional<std::string_view>;
108 [[nodiscard]] auto expires_in() const -> std::optional<std::chrono::seconds>;
111 [[nodiscard]] auto interval() const -> std::chrono::seconds;
112
114 [[nodiscard]] auto data() const -> const JSON &;
115
116private:
117 const JSON *data_;
118};
119
138
156class SOURCEMETA_CORE_OAUTH_EXPORT OAuthDevicePoller {
157public:
162 OAuthDevicePoller(const std::chrono::seconds interval,
163 const std::chrono::seconds lifetime,
164 const std::chrono::steady_clock::time_point start) noexcept;
165
167 [[nodiscard]] auto interval() const noexcept -> std::chrono::seconds;
168
170 [[nodiscard]] auto
171 expired(const std::chrono::steady_clock::time_point now) const noexcept
172 -> bool;
173
179 auto observe(const OAuthTokenError error) noexcept -> OAuthDevicePollDecision;
180
181private:
182 std::chrono::seconds interval_;
183 std::chrono::seconds lifetime_;
184 std::chrono::steady_clock::time_point start_;
185};
186
187#if defined(_MSC_VER)
188#pragma warning(default : 4251)
189#endif
190
211SOURCEMETA_CORE_OAUTH_EXPORT
213 const std::string_view device_code, const std::string_view user_code,
214 const std::string_view verification_uri,
215 const std::string_view verification_uri_complete,
216 const std::chrono::seconds expires_in, const std::chrono::seconds interval)
217 -> std::optional<JSON>;
218
232SOURCEMETA_CORE_OAUTH_EXPORT
233auto oauth_device_user_code() -> std::array<char, 8>;
234
248SOURCEMETA_CORE_OAUTH_EXPORT
249auto oauth_device_user_code_matches(const std::string_view presented,
250 const std::string_view stored) -> bool;
251
252} // namespace sourcemeta::core
253
254#endif
Definition crypto_secure.h:162
Definition json_value.h:39
auto verification_uri() const -> std::optional< std::string_view >
The end-user verification URI (RFC 8628 Section 3.2).
auto device_code() const -> std::optional< std::string_view >
The device verification code (RFC 8628 Section 3.2).
auto observe(const OAuthTokenError error) noexcept -> OAuthDevicePollDecision
auto interval() const -> std::chrono::seconds
OAuthDevicePoller(const std::chrono::seconds interval, const std::chrono::seconds lifetime, const std::chrono::steady_clock::time_point start) noexcept
auto interval() const noexcept -> std::chrono::seconds
The current polling interval, which a slow_down error grows.
auto data() const -> const JSON &
The underlying document.
auto user_code() const -> std::optional< std::string_view >
The end-user verification code (RFC 8628 Section 3.2).
OAuthDeviceAuthorizationResponse(const JSON &data)
Construct a view over a device authorization response, which is borrowed.
auto verification_uri_complete() const -> std::optional< std::string_view >
The verification URI with the user code included (RFC 8628 Section 3.2).
auto expired(const std::chrono::steady_clock::time_point now) const noexcept -> bool
Whether the codes have expired locally by the given time.
auto expires_in() const -> std::optional< std::chrono::seconds >
OAuthDevicePollDecision
Definition oauth_device.h:123
SOURCEMETA_CORE_OAUTH_EXPORT auto oauth_device_user_code() -> std::array< char, 8 >
SOURCEMETA_CORE_OAUTH_EXPORT auto oauth_device_user_code_matches(const std::string_view presented, const std::string_view stored) -> bool
SOURCEMETA_CORE_OAUTH_EXPORT auto oauth_make_device_authorization_response(const std::string_view device_code, const std::string_view user_code, const std::string_view verification_uri, const std::string_view verification_uri_complete, const std::chrono::seconds expires_in, const std::chrono::seconds interval) -> std::optional< JSON >
SOURCEMETA_CORE_OAUTH_EXPORT auto oauth_build_device_authorization_request(const std::string_view client_id, const std::string_view scope, const std::span< const OAuthParameter > resources, std::string &sink) -> void
OAuthTokenError
Definition oauth_error.h:42
SOURCEMETA_CORE_OAUTH_EXPORT auto oauth_build_token_request_device(const std::string_view device_code, const std::span< const OAuthParameter > resources, SecureString &sink) -> void
@ Denied
The end user denied the request.
Definition oauth_device.h:131
@ Error
Definition oauth_device.h:136
@ Continue
Keep polling at the current interval.
Definition oauth_device.h:125
@ RetryWithNonce
Definition oauth_device.h:129
@ Expired
The assertion has expired (RFC 7523 Section 3 check 4).
Definition oauth_assertion.h:147