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(push)
25#pragma warning(disable : 4251)
26#endif
27
45SOURCEMETA_CORE_OAUTH_EXPORT
47 const std::string_view client_id, const std::string_view scope,
48 const std::span<const OAuthParameter> resources, std::string &sink) -> void;
49
68SOURCEMETA_CORE_OAUTH_EXPORT
70 const std::string_view device_code,
71 const std::span<const OAuthParameter> resources, SecureString &sink)
72 -> void;
73
92class SOURCEMETA_CORE_OAUTH_EXPORT OAuthDeviceAuthorizationResponse {
93public:
96
98 [[nodiscard]] auto device_code() const -> std::optional<std::string_view>;
100 [[nodiscard]] auto user_code() const -> std::optional<std::string_view>;
102 [[nodiscard]] auto verification_uri() const
103 -> std::optional<std::string_view>;
105 [[nodiscard]] auto verification_uri_complete() const
106 -> std::optional<std::string_view>;
109 [[nodiscard]] auto expires_in() const -> std::optional<std::chrono::seconds>;
112 [[nodiscard]] auto interval() const -> std::chrono::seconds;
113
115 [[nodiscard]] auto data() const -> const JSON &;
116
117private:
118 const JSON *data_;
119};
120
139
157class SOURCEMETA_CORE_OAUTH_EXPORT OAuthDevicePoller {
158public:
163 OAuthDevicePoller(const std::chrono::seconds interval,
164 const std::chrono::seconds lifetime,
165 const std::chrono::steady_clock::time_point start) noexcept;
166
168 [[nodiscard]] auto interval() const noexcept -> std::chrono::seconds;
169
171 [[nodiscard]] auto
172 expired(const std::chrono::steady_clock::time_point now) const noexcept
173 -> bool;
174
180 auto observe(const OAuthTokenError error) noexcept -> OAuthDevicePollDecision;
181
182private:
183 std::chrono::seconds interval_;
184 std::chrono::seconds lifetime_;
185 std::chrono::steady_clock::time_point start_;
186};
187
188#if defined(_MSC_VER)
189#pragma warning(pop)
190#endif
191
212SOURCEMETA_CORE_OAUTH_EXPORT
214 const std::string_view device_code, const std::string_view user_code,
215 const std::string_view verification_uri,
216 const std::string_view verification_uri_complete,
217 const std::chrono::seconds expires_in, const std::chrono::seconds interval)
218 -> std::optional<JSON>;
219
233SOURCEMETA_CORE_OAUTH_EXPORT
234auto oauth_device_user_code() -> std::array<char, 8>;
235
249SOURCEMETA_CORE_OAUTH_EXPORT
250auto oauth_device_user_code_matches(const std::string_view presented,
251 const std::string_view stored) -> bool;
252
253} // namespace sourcemeta::core
254
255#endif
Definition crypto_secure.h:165
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:124
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:132
@ Error
Definition oauth_device.h:137
@ Continue
Keep polling at the current interval.
Definition oauth_device.h:126
@ RetryWithNonce
Definition oauth_device.h:130
@ Expired
The assertion has expired (RFC 7523 Section 3 check 4).
Definition oauth_assertion.h:147