#!/bin/bash
# verify-totp-impl.sh - TOTP实现验证脚本
# 依赖: go 1.21+
# 用法: ./verify-totp-impl.sh [project_dir]

set -e

PROJECT_DIR="${1:-.}"

echo "=== TOTP 实现验证 ==="
echo "项目目录: $PROJECT_DIR"
echo ""

# 1. 查找TOTP实现文件
echo "[1/4] 查找TOTP实现..."
TOTP_FILES=$(find "$PROJECT_DIR" -name "*.go" -exec grep -l "totp\|TOTP" {} \; 2>/dev/null || true)
if [ -n "$TOTP_FILES" ]; then
    echo "找到TOTP相关文件:"
    echo "$TOTP_FILES" | head -10
    echo ""
else
    echo "⚠ 未找到TOTP相关文件"
fi

# 2. 验证Tier-One参数
echo "[2/4] 验证Tier-One参数 (8位/30分钟/SHA256)..."
TIER_ONE_OK=true

if grep -rq "8" "$PROJECT_DIR" --include="*.go" 2>/dev/null | grep -qi "digit\|Digit"; then
    echo "✓ 找到8位配置"
else
    echo "⚠ 未明确找到8位配置"
    TIER_ONE_OK=false
fi

if grep -rq "1800\|30.*[Mm]in" "$PROJECT_DIR" --include="*.go" 2>/dev/null; then
    echo "✓ 找到30分钟有效期配置"
else
    echo "⚠ 未找到30分钟有效期配置"
    TIER_ONE_OK=false
fi

if grep -rq "SHA256\|sha256" "$PROJECT_DIR" --include="*.go" 2>/dev/null; then
    echo "✓ 找到SHA256算法"
else
    echo "⚠ 未找到SHA256算法"
    TIER_ONE_OK=false
fi

# 3. 验证Tier-Two参数
echo ""
echo "[3/4] 验证Tier-Two参数 (6位/30秒/SHA1)..."
TIER_TWO_OK=true

if grep -rq "6" "$PROJECT_DIR" --include="*.go" 2>/dev/null | grep -qi "digit\|Digit"; then
    echo "✓ 找到6位配置"
else
    echo "⚠ 未明确找到6位配置"
    TIER_TWO_OK=false
fi

if grep -rq "30" "$PROJECT_DIR" --include="*.go" 2>/dev/null | grep -qi "period\|second\|Period\|Second"; then
    echo "✓ 找到30秒有效期配置"
else
    echo "⚠ 未找到30秒有效期配置"
    TIER_TWO_OK=false
fi

if grep -rq "SHA1\|sha1" "$PROJECT_DIR" --include="*.go" 2>/dev/null; then
    echo "✓ 找到SHA1算法"
else
    echo "⚠ 未找到SHA1算法"
    TIER_TWO_OK=false
fi

# 4. 验证双向验证
echo ""
echo "[4/4] 验证双向验证实现..."
if grep -rq "response.*[Tt]otp\|[Tt]otp.*response" "$PROJECT_DIR" --include="*.go" 2>/dev/null; then
    echo "✓ 找到响应TOTP相关代码"
else
    echo "⚠ 未找到响应TOTP实现，请确认双向验证"
fi

# 总结
echo ""
echo "=== 验证总结 ==="
if [ "$TIER_ONE_OK" = true ] && [ "$TIER_TWO_OK" = true ]; then
    echo "✓ TOTP参数配置基本正确"
else
    echo "⚠ 部分TOTP参数需要确认"
fi
