Skip to main content
Question

Script to Remove Accesstokens in Keychain

  • May 16, 2024
  • 0 replies
  • 48 views

Forum|alt.badge.img+1

Trying to figure out a script that can delete all of the Default Keychains - local items - accesstokens. Would appreciate any assistants. Kind of new to scripting.

#!/bin/bash

# Function to list access tokens
list_access_tokens() {
    security find-generic-password -a $USER -s "OAuth Access Token" -g 2>&1 | grep "acct" | cut -d '"' -f 4
}

# Main function
main() {
    echo "Access Tokens found in Keychain:"
    list_access_tokens 

    echo "Enter the name of the access token you want to remove (or type 'exit' to quit):"
    read access_token_name

    if [ "$access_token_name" == "exit" ]; then
        echo "Exiting..."
        exit 0
    fi

    # Check if the access token exists
    if security find-generic-password -a $USER -s "OAuth Access Token" -l "$access_token_name" >/dev/null 2>&1; then
        echo "Removing access token: $access_token_name"
       
        echo "Access token removed successfully."
    else
        echo "Access token not found."
    fi
}

main