001/* 002 * Copyright 2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.revetsec.saml; 017 018import com.revetsec.BrowserBinding; 019import com.revetsec.StateSealer; 020import com.revetsec.internal.crypto.SealedStateType; 021import org.jspecify.annotations.NonNull; 022import javax.annotation.concurrent.Immutable; 023import java.time.Duration; 024import java.time.Instant; 025import java.util.Objects; 026 027/** 028 * Engine-created pending SP logout. Its separate seal type cannot be replayed as login state. 029 * 030 * @since 1.0.0 031 */ 032@Immutable 033public final class PendingSamlLogout { 034 private final @NonNull PendingSamlAuthentication record; 035 036 PendingSamlLogout(@NonNull PendingSamlAuthentication record) { this.record = record; } 037 038 /** 039 * Seals logout pending state for a browser-bound secure cookie. 040 * A valid pending record may exceed the configured sealer limit, which throws 041 * {@link IllegalArgumentException}. Persist successfully before delivering the request. 042 * 043 * @param sealer active and verification keys 044 * @param context application cookie namespace 045 * @return sealed value 046 * @since 1.0.0 047 */ 048 public @NonNull String toSealedForm(@NonNull StateSealer sealer, @NonNull String context) { 049 String sealed = PendingSamlAuthenticationCodec.sealIfFits(record, Objects.requireNonNull(sealer), 050 SealedStateType.PENDING_SAML_LOGOUT, Objects.requireNonNull(context)); 051 if (sealed == null) throw new IllegalArgumentException("SAML pending state exceeds sealing limit"); 052 return sealed; 053 } 054 055 /** 056 * Seals and saves logout pending state in an atomic browser-bound store. 057 * If the encoded record exceeds the configured sealer limit, returns UNAVAILABLE without 058 * calling the store or drawing sealing entropy. Shorten configuration before retrying; 059 * invalid contexts remain argument errors. A possibly completed save remains INDETERMINATE. 060 * 061 * @param store atomic pending store 062 * @param sealer active and verification keys 063 * @param context application store namespace 064 * @param browserBinding browser-specific secret 065 * @param remaining positive operation budget 066 * @return save outcome 067 * @since 1.0.0 068 */ 069 public @NonNull SamlPendingSaveResult saveToResult(@NonNull PendingSamlAuthenticationStore store, 070 @NonNull StateSealer sealer, @NonNull String context, @NonNull BrowserBinding browserBinding, 071 @NonNull Duration remaining) { 072 return saveToResult(store, sealer, context, Objects.requireNonNull(browserBinding).toSecretString(), remaining); 073 } 074 075 @NonNull SamlPendingSaveResult saveToResult(@NonNull PendingSamlAuthenticationStore store, 076 @NonNull StateSealer sealer, @NonNull String context, @NonNull String browserBinding, 077 @NonNull Duration remaining) { 078 Objects.requireNonNull(store); 079 Objects.requireNonNull(sealer); 080 Objects.requireNonNull(remaining); 081 if (remaining.isZero() || remaining.isNegative()) return SamlPendingSaveResult.UNAVAILABLE; 082 String bound = SamlPendingBinding.context(Objects.requireNonNull(context) + "|logout", 083 Objects.requireNonNull(browserBinding)); 084 String sealed = PendingSamlAuthenticationCodec.sealIfFits(record, sealer, 085 SealedStateType.PENDING_SAML_LOGOUT, bound); 086 if (sealed == null) return SamlPendingSaveResult.UNAVAILABLE; 087 long started = System.nanoTime(); 088 try { 089 SamlPendingSaveResult result = store.save(browserBinding, record.relayState(), sealed, 090 record.expiresAt(), remaining); 091 return result == null || SamlPendingBudget.exhausted(started, remaining) 092 ? SamlPendingSaveResult.INDETERMINATE : result; 093 } catch (RuntimeException exception) { return SamlPendingSaveResult.INDETERMINATE; } 094 } 095 096 /** 097 * Returns the generated logout request ID. 098 * 099 * @return request ID 100 * @since 1.0.0 101 */ 102 public @NonNull String getRequestId() { return record.requestId(); } 103 /** 104 * Returns the generated RelayState handle. 105 * 106 * @return handle 107 * @since 1.0.0 108 */ 109 public @NonNull String getRelayStateHandle() { return record.relayState(); } 110 /** 111 * Returns pending expiry. 112 * 113 * @return expiry 114 * @since 1.0.0 115 */ 116 public @NonNull Instant getExpiresAt() { return record.expiresAt(); } 117 @NonNull PendingSamlAuthentication record() { return record; } 118 /** 119 * Redacts pending state. 120 * 121 * @return redacted description 122 * @since 1.0.0 123 */ 124 @Override public @NonNull String toString() { return "PendingSamlLogout{<redacted>}"; } 125}