123 lines
3.3 KiB
Python
Executable File
123 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Bitwarden API 客户端
|
|
使用 API Client ID/Secret 获取访问令牌
|
|
"""
|
|
|
|
import requests
|
|
import os
|
|
import json
|
|
from datetime import datetime
|
|
|
|
# 配置
|
|
SERVER_URL = "https://yaxing.eicp.vip"
|
|
CLIENT_ID = "user.7ab49ada-32e6-48d5-aaf1-1943921ea84f"
|
|
CLIENT_SECRET = "m1K8ck9PUhiMlYElm45ua2LyIuVizZ"
|
|
|
|
def get_access_token():
|
|
"""获取访问令牌"""
|
|
url = f"{SERVER_URL}/identity/connect/token"
|
|
|
|
data = {
|
|
"client_id": CLIENT_ID,
|
|
"client_secret": CLIENT_SECRET,
|
|
"scope": "api",
|
|
"grant_type": "client_credentials",
|
|
"device_identifier": f"openclaw-{datetime.now().timestamp()}",
|
|
"device_name": "OpenClaw",
|
|
"device_type": "24"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(url, data=data, timeout=30)
|
|
if response.status_code == 200:
|
|
token_data = response.json()
|
|
return token_data.get("access_token")
|
|
else:
|
|
print(f"❌ 获取 Token 失败: {response.text}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"❌ 请求失败: {e}")
|
|
return None
|
|
|
|
def list_items():
|
|
"""列出所有密码项"""
|
|
token = get_access_token()
|
|
if not token:
|
|
return None
|
|
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
# 尝试不同的 API 端点
|
|
endpoints = [
|
|
f"{SERVER_URL}/api/v1/ciphers",
|
|
f"{SERVER_URL}/api/ciphers",
|
|
f"{SERVER_URL}/api/v2/ciphers",
|
|
]
|
|
|
|
for url in endpoints:
|
|
try:
|
|
response = requests.get(url, headers=headers, timeout=30)
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
elif response.status_code == 404:
|
|
continue
|
|
else:
|
|
print(f" {url}: {response.status_code}")
|
|
except Exception as e:
|
|
print(f" {url}: {e}")
|
|
|
|
return None
|
|
|
|
def search_items(query):
|
|
"""搜索密码项"""
|
|
items = list_items()
|
|
if not items:
|
|
return None
|
|
|
|
# 简单的关键词匹配
|
|
results = []
|
|
if isinstance(items, dict):
|
|
if "data" in items:
|
|
for item in items["data"]:
|
|
if query.lower() in str(item).lower():
|
|
results.append(item)
|
|
elif isinstance(items, list):
|
|
for item in items:
|
|
if query.lower() in str(item).lower():
|
|
results.append(item)
|
|
|
|
return results
|
|
|
|
def main():
|
|
print(f"\n{'='*60}")
|
|
print("🔐 Bitwarden API 客户端")
|
|
print(f"{'='*60}\n")
|
|
|
|
# 获取 Token
|
|
print("获取访问令牌...")
|
|
token = get_access_token()
|
|
if token:
|
|
print(f"✅ Token 获取成功")
|
|
print(f" Token: {token[:30]}...")
|
|
|
|
# 保存 Token
|
|
os.makedirs("/root/.openclaw/.bitwarden", exist_ok=True)
|
|
with open("/root/.openclaw/.bitwarden/token", "w") as f:
|
|
f.write(token)
|
|
print(f"✅ Token 已保存到 /root/.openclaw/.bitwarden/token")
|
|
|
|
# 列出密码
|
|
print(f"\n获取密码列表...")
|
|
items = list_items()
|
|
if items:
|
|
print(f"✅ 获取到数据: {type(items)}")
|
|
print(f" 数据大小: {len(str(items))} 字符")
|
|
else:
|
|
print("❌ 无法获取密码,可能需要更高权限")
|
|
else:
|
|
print("❌ Token 获取失败")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|