Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
password_value.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <portals/secure_string.hpp>
7#include <portals/widget_types.hpp>
8
9namespace portals {
10
14template <class E = std::monostate> struct PasswordValueAccessFailure {
16 std::optional<TreeError> ownership;
18 std::optional<SecureStringAccessFailure<E>> access;
19};
20
21namespace detail {
22struct PasswordValueAccess;
23#if defined(PORTALS_SECURE_STRING_TESTING)
24struct SecureStringTestHooks;
25#endif
26} // namespace detail
27
45public:
47 PasswordValue() noexcept = default;
51 PasswordValue(const PasswordValue &other) noexcept = default;
56 PasswordValue &operator=(const PasswordValue &other) noexcept = default;
60 PasswordValue(PasswordValue &&other) noexcept = default;
65 PasswordValue &operator=(PasswordValue &&other) noexcept;
67 ~PasswordValue() noexcept = default;
68
74 [[nodiscard]] static std::expected<PasswordValue, SecureStringFailure> create(std::string_view bytes = {},
77 [[nodiscard]] std::size_t size() const noexcept;
79 [[nodiscard]] bool empty() const noexcept;
81 [[nodiscard]] bool available() const noexcept;
83 [[nodiscard]] SecureStringOptions options() const noexcept;
85 [[nodiscard]] SecureStringCapabilities capabilities() const noexcept;
90 [[nodiscard]] bool operator==(const PasswordValue &other) const noexcept = default;
91
106 template <class F>
107 requires detail::SecretCallback<F>
108 [[nodiscard]] auto withPlaintext(F &&callback) const
109 -> std::expected<void, PasswordValueAccessFailure<typename detail::SecretCallbackResult<
110 std::invoke_result_t<std::decay_t<F> &, SecretView>>::Error>> {
111 return accessState(state_, std::forward<F>(callback));
112 }
113
114private:
115 struct State;
116
117 template <class F>
118 requires detail::SecretCallback<F>
119 static auto accessState(std::shared_ptr<State> state, F &&callback)
120 -> std::expected<void, PasswordValueAccessFailure<typename detail::SecretCallbackResult<
121 std::invoke_result_t<std::decay_t<F> &, SecretView>>::Error>> {
122 using Return = std::invoke_result_t<std::decay_t<F> &, SecretView>;
125
126 if (!state) {
127 return std::unexpected(Failure{
128 {},
129 AccessFailure{SecureStringError{SecureStringOperation::BORROW, SecureStringErrorCode::UNAVAILABLE}, {}, {}}});
130 }
131
132 if (auto error = checkOwner(state)) {
133 return std::unexpected(Failure{error, {}});
134 }
135
136 auto result = protectedStorage(state).withPlaintext(std::forward<F>(callback));
137
138 auto cleanup = finishAccess(state);
139
140 if (cleanup) {
141 auto failure = result ? AccessFailure{} : std::move(result.error());
142
143 failure.cleanup.add(cleanup->primary);
144 failure.cleanup.append(cleanup->cleanup);
145
146 return std::unexpected(Failure{{}, std::move(failure)});
147 }
148
149 if (!result) {
150 return std::unexpected(Failure{{}, std::move(result.error())});
151 }
152
153 return {};
154 }
155
156 explicit PasswordValue(std::shared_ptr<State> state) noexcept;
157 static std::expected<PasswordValue, SecureStringFailure> freeze(SecureString &&value, SecureStringOptions options);
158 static std::optional<TreeError> checkOwner(const std::shared_ptr<State> &state) noexcept;
159 static SecureString &protectedStorage(const std::shared_ptr<State> &state) noexcept;
160 static std::optional<SecureStringFailure> finishAccess(const std::shared_ptr<State> &state) noexcept;
161
162 std::shared_ptr<State> state_;
163 friend struct detail::PasswordValueAccess;
164};
165
167namespace detail {
169struct PasswordValueAccess {
176 template <class F>
177 requires SecretCallback<F>
178 static auto borrowOwned(PasswordValue value, F &&callback) {
179 return PasswordValue::accessState(std::move(value.state_), std::forward<F>(callback));
180 }
181
192 [[nodiscard]] static std::expected<PasswordValue, PasswordValueAccessFailure<SecureStringFailure>>
193 replace(const PasswordValue &source, std::size_t first, std::size_t last, std::string_view insertion);
194#if defined(PORTALS_SECURE_STRING_TESTING)
195 static void attachTestHooks(const PasswordValue &value, SecureStringTestHooks &hooks) noexcept;
196#endif
197};
198} // namespace detail
199
201
202} // namespace portals
SecureStringOptions options() const noexcept
static std::expected< PasswordValue, SecureStringFailure > create(std::string_view bytes={}, SecureStringOptions options={})
Establish mandatory native protection before copying opaque input bytes.
bool empty() const noexcept
PasswordValue() noexcept=default
Construct an unavailable identity without allocating protected storage.
bool available() const noexcept
auto withPlaintext(F &&callback) const -> std::expected< void, PasswordValueAccessFailure< typename detail::SecretCallbackResult< std::invoke_result_t< std::decay_t< F > &, SecretView > >::Error > >
Borrow read-only plaintext synchronously on the creating thread.
Definition password_value.hpp:108
SecureStringCapabilities capabilities() const noexcept
std::size_t size() const noexcept
Synchronous read-only byte borrow, valid only during its callback.
Definition secure_string.hpp:195
Public Portals API.
Definition action.hpp:8
@ BORROW
Admit plaintext access, capture a callable or invoke its callback.
Definition secure_string.hpp:78
@ UNAVAILABLE
The handle is moved-from, closed or invalidated by a security failure.
Definition secure_string.hpp:110
Independent ownership and protected-access failures, without secret diagnostics.
Definition password_value.hpp:14
std::optional< SecureStringAccessFailure< E > > access
Protected-storage, callback and cleanup diagnostics, retained independently.
Definition password_value.hpp:18
std::optional< TreeError > ownership
Ownership/admission rejection; the callback was neither captured nor invoked.
Definition password_value.hpp:16
Callback and cleanup diagnostics; opaque user errors are never logged.
Definition secure_string.hpp:223
Applied guarantees, rather than potential platform features.
Definition secure_string.hpp:52
A primary failure and independently preserved bounded cleanup diagnostics.
Definition secure_string.hpp:182
Requested mandatory guarantees, established even for an empty value.
Definition secure_string.hpp:38