From time to time I need to run some curl or wget call from terminal that requires authentication. Something like this:
curl --anyauth -u "username:pass" "url"
The problem obviously is that typing passwords in plaintext in terminal is not a very good idea. For once they’ll get stored in history.
The quick and crude solution I’ve came up with was to use OS X ability to run AppleScript directly from terminal using
osascript -e 'script here' syntax. So why not run AppleScript that would display input dialog and return the text user has entered into it?
Here’s the simple way to get user input from call to osascript:
osascript -e 'return text returned of (display dialog "Input text" default answer "")'
Naturally, for passwords we’d like to hide the text we type in. And also we need to put the result of the command into curl parameters. The final result is this:
curl --anyauth -u "username:$(osascript -e 'return text returned of (display dialog "Input text" default answer "" with hidden answer)')" "url"
Final note: naturally, you can remove “username:” part from command and type it into the dialog instead if you’re also concerned about usernames ending up in terminal in plaintext.
That’s it. Hope that helps someone like it helped me.
P.S. Couple of useful aliases for the command
alias inputp='osascript -e "return text returned of (display dialog \"Input text\" default answer \"\" with hidden answer)"'
alias inputt='osascript -e "return text returned of (display dialog \"Input text\" default answer \"\")"'