import sqlite3
import getpass
from keepersdk.authentication import login_auth, configuration, endpoint
from keepersdk.vault import sqlite_storage, vault_online, vault_record
# 構成および認証コンテキストを初期化
config = configuration.JsonConfigurationStorage()
keeper_endpoint = endpoint.KeeperEndpoint(config)
login_auth_context = login_auth.LoginAuth(keeper_endpoint)
# ユーザーを認証
login_auth_context.login(config.get().users()[0].username, config.get().users()[0].password)
while not login_auth_context.login_step.is_final():
if isinstance(login_auth_context.login_step, login_auth.LoginStepDeviceApproval):
login_auth_context.login_step.send_push(login_auth.DeviceApprovalChannel.KeeperPush)
print("Device approval request sent. Login to existing vault/console or ask admin to approve this device and then press return/enter to resume")
input()
elif isinstance(login_auth_context.login_step, login_auth.LoginStepPassword):
password = getpass.getpass('Enter password: ')
login_auth_context.login_step.verify_password(password)
elif isinstance(login_auth_context.login_step, login_auth.LoginStepTwoFactor):
channel = login_auth_context.login_step.get_channels()[0]
code = getpass.getpass(f'Enter 2FA code for {channel.channel_name}: ')
login_auth_context.login_step.send_code(channel.channel_uid, code)
else:
raise NotImplementedError()
# ログインが成功したかを確認
if isinstance(login_auth_context.login_step, login_auth.LoginStepConnected):
# 認証済みセッションを取得
keeper_auth = login_auth_context.login_step.take_keeper_auth()
# ボルトストレージを設定(SQLiteのインメモリデータベースを使用)
conn = sqlite3.Connection('file::memory:', uri=True)
vault_storage = sqlite_storage.SqliteVaultStorage(
lambda: conn,
vault_owner=bytes(keeper_auth.auth_context.username, 'utf-8')
)
# ボルトを初期化し、Keeperサーバーと同期
vault = vault_online.VaultOnline(keeper_auth, vault_storage)
vault.sync_down()
# ボルト内のレコードにアクセスして表示
print("Vault Records:")
print("-" * 50)
for record in vault.vault_data.records():
print(f'Title: {record.title}')
# 旧形式(v2)レコードの処理
if record.version == 2:
legacy_record = vault.vault_data.load_record(record.record_uid)
if isinstance(legacy_record, vault_record.PasswordRecord):
print(f'Username: {legacy_record.login}')
print(f'URL: {legacy_record.link}')
# 新形式(v3以降)レコードの処理
elif record.version >= 3:
print(f'Record Type: {record.record_type}')
print("-" * 50)
vault.close()
keeper_auth.close()