48inline std::string
sha256(std::string_view data) {
49 static constexpr std::array<uint32_t, 64> kRound = {
50 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
51 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
52 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
53 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
54 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
55 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
56 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
57 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
59 std::array<uint32_t, 8> hash = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
60 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
62 std::vector<uint8_t> msg(data.begin(), data.end());
63 const uint64_t bitLen = msg.size() * 8;
65 while (msg.size() % 64 != 56) {
68 for (
int shift = 56; shift >= 0; shift -= 8) {
69 msg.push_back(
static_cast<uint8_t
>((bitLen >> shift) & 0xff));
72 auto rotr = [](uint32_t val, uint32_t bits) {
return (val >> bits) | (val << (32 - bits)); };
74 for (std::size_t chunk = 0; chunk < msg.size(); chunk += 64) {
75 std::array<uint32_t, 64> w{};
76 for (std::size_t i = 0; i < 16; ++i) {
77 w[i] = (
static_cast<uint32_t
>(msg[chunk + (i * 4)]) << 24) |
78 (
static_cast<uint32_t
>(msg[chunk + (i * 4) + 1]) << 16) |
79 (
static_cast<uint32_t
>(msg[chunk + (i * 4) + 2]) << 8) |
80 static_cast<uint32_t
>(msg[chunk + (i * 4) + 3]);
82 for (std::size_t i = 16; i < 64; ++i) {
83 const uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
84 const uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
85 w[i] = w[i - 16] + s0 + w[i - 7] + s1;
96 for (std::size_t i = 0; i < 64; ++i) {
97 const uint32_t bigS1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
98 const uint32_t choose = (e & f) ^ (~e & g);
99 const uint32_t t1 = h + bigS1 + choose + kRound[i] + w[i];
100 const uint32_t bigS0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
101 const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
102 const uint32_t t2 = bigS0 + maj;
122 std::string out(32,
'\0');
123 for (std::size_t i = 0; i < 8; ++i) {
124 out[(i * 4)] =
static_cast<char>((hash[i] >> 24) & 0xff);
125 out[(i * 4) + 1] =
static_cast<char>((hash[i] >> 16) & 0xff);
126 out[(i * 4) + 2] =
static_cast<char>((hash[i] >> 8) & 0xff);
127 out[(i * 4) + 3] =
static_cast<char>(hash[i] & 0xff);
534 std::function<bool(
const SessionToken& claims, std::string_view modelType, std::string_view actionType)>;
536#ifdef MORPH_REQUIRE_VETTED_HMAC
555 : _verifier{std::move(secret), std::move(mac)}, _clock{std::move(clock)}, _policy{std::move(policy)} {}
569 : _verifier{std::move(secret), std::move(mac)}, _clock{std::move(clock)}, _policy{std::move(policy)} {}
579 std::string_view modelType, std::string_view actionType)
const override {
580 const auto claims = verified(ctx);
584 return !_policy || _policy(*claims, modelType, actionType);
591 const auto claims = verified(ctx);
595 return claims->principal;
614 std::string_view modelType, std::string_view actionType)
const override {
615 Verification
const once{.owner =
this, .ctx = &ctx, .claims = _verifier.verify(ctx.
token, _clock())};
616 ActiveVerification
const scope{once};
618 return Admission{.allowed =
authorize(ctx, modelType, actionType), .principal = std::move(principal)};
623 struct Verification {
626 std::expected<SessionToken, AuthError> claims;
630 static const Verification*& active() noexcept {
631 thread_local const Verification* current =
nullptr;
637 class ActiveVerification {
639 explicit ActiveVerification(
const Verification& verification) noexcept
640 : _previous{std::exchange(active(), &verification)} {}
641 ~ActiveVerification() { active() = _previous; }
642 ActiveVerification(
const ActiveVerification&) =
delete;
643 ActiveVerification& operator=(
const ActiveVerification&) =
delete;
644 ActiveVerification(ActiveVerification&&) =
delete;
645 ActiveVerification& operator=(ActiveVerification&&) =
delete;
648 const Verification* _previous;
653 [[nodiscard]] std::expected<SessionToken, AuthError> verified(
const Context& ctx)
const {
654 if (
const auto* running = active(); running !=
nullptr && running->owner ==
this && running->ctx == &ctx) {
655 return running->claims;
657 return _verifier.verify(ctx.token, _clock());
660 TokenVerifier _verifier;
Per-call session context carried through the bridge to the model.
std::string token
Bearer credential verified server-side (empty if unauthenticated).