signReceiptToken($orderNumber, $expiresTs); $tokenValue = $orderNumber.'|'.$expiresTs.'|'.$signature; /** @var CookieJar $jar */ $jar = app(CookieJar::class); // HttpOnly + SameSite=Lax + Secure(HTTPS) — XSS 방어 + same-site 흐름 한정 $cookie = new Cookie( name: self::RECEIPT_COOKIE_NAME, value: $tokenValue, expire: $expiresTs, path: '/', domain: null, secure: request()->isSecure(), httpOnly: true, raw: false, sameSite: Cookie::SAMESITE_LAX, ); $jar->queue($cookie); } /** * 영수증 토큰 cookie 를 검증하여 주문번호 일치 여부를 반환한다. * * @param string|null $cookieValue 요청에서 추출한 cookie 값 * @param string $expectedOrderNumber 검증할 주문번호 * @return bool 유효한 토큰 여부 */ protected function verifyReceiptCookie(?string $cookieValue, string $expectedOrderNumber): bool { if (! is_string($cookieValue) || $cookieValue === '') { return false; } $parts = explode('|', $cookieValue); if (count($parts) !== 3) { return false; } [$orderNumber, $expiresTs, $signature] = $parts; // 다른 주문 cookie 재사용 차단 if ($orderNumber !== $expectedOrderNumber) { return false; } // 만료시각 형식 + 만료 여부 if (! ctype_digit($expiresTs) || (int) $expiresTs < time()) { return false; } // 서명 형식 (HMAC-SHA256 hex 64자) if (! ctype_xdigit($signature) || strlen($signature) !== 64) { return false; } $expectedSignature = $this->signReceiptToken($orderNumber, (int) $expiresTs); return hash_equals($expectedSignature, $signature); } private function signReceiptToken(string $orderNumber, int $expiresTs): string { $payload = $orderNumber.'|'.$expiresTs; $key = (string) config('app.key', ''); return hash_hmac('sha256', $payload, $key); } }