Files
ai-setup/home-configs/claude/hooks/account-email.sh
2026-06-30 12:12:42 +07:00

35 lines
1.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Copyright (c) 2026 Виталий Никитенко. MIT License.
# Определяет email Claude.ai аккаунта по OAuth-токену в credentials-файле.
# Источник истины — сам токен (НЕ claude auth status, который читает
# рассинхронизированный oauthAccount из ~/.claude.json).
# Сначала локальный матчинг с сохранёнными accounts/, затем API /api/oauth/profile.
# Использование: account-email.sh [credentials-file] (по умолчанию ~/.claude/.credentials.json)
CREDS="${1:-$HOME/.claude/.credentials.json}"
ACCOUNTS_DIR="$HOME/.claude/accounts"
[ -f "$CREDS" ] || exit 0
token=$(jq -r '.claudeAiOauth.accessToken // empty' "$CREDS" 2>/dev/null)
[ -z "$token" ] && exit 0
# 1) Локальный матчинг по токену с сохранёнными аккаунтами (мгновенно)
if [ -d "$ACCOUNTS_DIR" ]; then
for f in "$ACCOUNTS_DIR"/*.credentials.json; do
[ -f "$f" ] || continue
t=$(jq -r '.claudeAiOauth.accessToken // empty' "$f" 2>/dev/null)
if [ -n "$t" ] && [ "$t" = "$token" ]; then
basename "$f" .credentials.json
exit 0
fi
done
fi
# 2) Достоверно через API (по реальному владельцу токена)
email=$(curl -s --max-time 15 "https://api.anthropic.com/api/oauth/profile" \
-H "Authorization: Bearer $token" \
-H "anthropic-beta: oauth-2025-04-20" 2>/dev/null \
| jq -r '.account.email // empty' 2>/dev/null)
[ -n "$email" ] && echo "$email"
exit 0