From 6cbc8e707ceb4fb5bd06635ebf0e30b93631b1a2 Mon Sep 17 00:00:00 2001 From: Angelos Michalopoulos Date: Mon, 18 Nov 2024 17:57:17 +0200 Subject: [PATCH] feat: add `apple-passwords-otp` plugin --- plugins/apple-passwords-otp/README.md | 26 +++++++++++++ .../apple-passwords-otp.zsh | 37 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 plugins/apple-passwords-otp/README.md create mode 100644 plugins/apple-passwords-otp/apple-passwords-otp.zsh diff --git a/plugins/apple-passwords-otp/README.md b/plugins/apple-passwords-otp/README.md new file mode 100644 index 000000000..60c984917 --- /dev/null +++ b/plugins/apple-passwords-otp/README.md @@ -0,0 +1,26 @@ +## apple-passwords-otp + +This plugin adds a function that allows to quickly copy the One-Time-Password (OTP) for a specific domain from +Apple Passwords to the clipboard. + +To use it, add `apple-passwords-otp` to the plugins array of your zshrc file: + +```zsh +plugins=(... apple-passwords-otp) +``` + +## Functions + +- `otp(domain)` : copies the OTP for the specified domain to the clipboard + +## Usage + +```bash +otp domain +``` + +## Requirements + +This plugin depends on the [Apple Passwords CLI](https://github.com/bendews/apw) tool. It can be installed via +Homebrew. Other requirements are `jq` to parse json files which can be installed via Homebrew as well. You +need to have Mac OS Sequoia or later to use this plugin. diff --git a/plugins/apple-passwords-otp/apple-passwords-otp.zsh b/plugins/apple-passwords-otp/apple-passwords-otp.zsh new file mode 100644 index 000000000..3d1ee398c --- /dev/null +++ b/plugins/apple-passwords-otp/apple-passwords-otp.zsh @@ -0,0 +1,37 @@ +function otp() { + if [ -z "$1" ]; then + echo "Usage: otp " + return 1 + fi + if ! command -v apw &> /dev/null || ! command -v jq &> /dev/null || ! command -v awk &> /dev/null; then + echo "This function requires apw, jq and awk to be installed" + return 1 + fi + CODES=$(apw otp get $1 2>/dev/null) + STATUS=$? + # ✋ If return code 9, not authenticated, run apw auth + if [ $STATUS -eq 9 ]; then + echo "🔐 Authenticating …" + apw auth + CODES=$(apw otp get $1) + fi + # ✋ If return code 3, domain not found, alert user + if [ $STATUS -eq 3 ]; then + echo "🚫 Domain not found" + return 1 + fi + # Grab available OTP codes for domain + if [ $(echo $CODES | jq '.results | length') -gt 1 ]; then + echo 'Multiple OTP codes found, select username:' + echo $CODES | jq -r '.results | to_entries[] | "\(.key + 1). \(.value.username)"' + read -k 1 index + echo + CODE=$(echo $CODES | jq -r ".results[$index - 1].code") + tput cuu $(echo $CODES | jq '.results | length' | awk '{print $1 + 2}') + tput ed + else + CODE=$(echo $CODES | jq -r '.results[0].code') + fi + echo $CODE | pbcopy + echo "🔑 OTP code copied to clipboard" +} \ No newline at end of file