二要素認証の設定
コマンドラインから二要素認証(2FA)のTOTPトークンを生成できるツールをセットアップします。
oath-toolkitのインストール
Section titled “oath-toolkitのインストール”brew install oath-toolkit基本的な使い方
Section titled “基本的な使い方”TOTPトークンの生成
Section titled “TOTPトークンの生成”# 基本的な使用方法oathtool --totp --base32 YOUR_SECRET_KEY
# 生成したトークンをクリップボードにコピーoathtool --totp --base32 YOUR_SECRET_KEY | pbcopyエイリアスの設定
Section titled “エイリアスの設定”よく使用するサービスのトークン生成をエイリアスとして設定すると便利です。
.zshrcに追加:
# 例:各サービス用のエイリアスalias service1_key='oathtool --totp --base32 YOUR_SERVICE1_SECRET | pbcopy'alias service2_key='oathtool --totp --base32 YOUR_SERVICE2_SECRET | pbcopy'設定を反映:
source ~/.zshrcセキュリティ上の注意事項
Section titled “セキュリティ上の注意事項”シークレットキーの管理
Section titled “シークレットキーの管理”- シークレットキーは機密情報です
.zshrcにハードコードする場合は、ファイルの権限を適切に設定- 可能であれば、環境変数や別の安全な場所に保存
より安全な設定方法
Section titled “より安全な設定方法”1. 環境変数を使用
Section titled “1. 環境変数を使用”# .zshrc.secretなど別ファイルに記載export SERVICE1_SECRET="YOUR_SECRET_KEY"
# .zshrcで読み込みsource ~/.zshrc.secretalias service1_key='oathtool --totp --base32 $SERVICE1_SECRET | pbcopy'2. macOSキーチェーンを使用
Section titled “2. macOSキーチェーンを使用”# シークレットをキーチェーンに保存security add-generic-password -a "$USER" -s "service1-totp" -w "YOUR_SECRET_KEY"
# キーチェーンから取得して使用alias service1_key='oathtool --totp --base32 $(security find-generic-password -a "$USER" -s "service1-totp" -w) | pbcopy'Google Authenticatorからの移行
Section titled “Google Authenticatorからの移行”Google Authenticatorアプリから秘密鍵を抽出する必要がある場合:
- QRコードから秘密鍵を抽出
- Base32形式であることを確認
- oath-toolkitで使用
トラブルシューティング
Section titled “トラブルシューティング”トークンが無効な場合
Section titled “トークンが無効な場合”-
時刻同期の確認
Terminal window # システム時刻の確認date# NTPで時刻同期sudo sntp -sS time.apple.com -
秘密鍵の形式確認
- Base32形式であることを確認
- 大文字・小文字の区別
30秒ウィンドウの調整
Section titled “30秒ウィンドウの調整”# 前後のタイムウィンドウも考慮oathtool --totp --base32 YOUR_SECRET_KEY --window 1他の便利なオプション
Section titled “他の便利なオプション”# 6桁以外のトークン長を指定oathtool --totp --base32 YOUR_SECRET_KEY --digits 8
# SHA256アルゴリズムを使用(デフォルトはSHA1)oathtool --totp --base32 YOUR_SECRET_KEY --totp=sha256