Skip to content

Commit 9fea75a

Browse files
committed
2 parents 8e4b8a5 + a9d7c39 commit 9fea75a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lib/core/sudo.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,41 @@ request_sudo_access() {
108108
return 0
109109
fi
110110

111-
# Get TTY path
111+
# Detect if running in TTY environment
112112
local tty_path="/dev/tty"
113+
local is_gui_mode=false
114+
113115
if [[ ! -r "$tty_path" || ! -w "$tty_path" ]]; then
114116
tty_path=$(tty 2> /dev/null || echo "")
115117
if [[ -z "$tty_path" || ! -r "$tty_path" || ! -w "$tty_path" ]]; then
116-
log_error "No interactive terminal available"
118+
is_gui_mode=true
119+
fi
120+
fi
121+
122+
# GUI mode: use osascript for password dialog
123+
if [[ "$is_gui_mode" == true ]]; then
124+
# Clear sudo cache before attempting authentication
125+
sudo -k 2> /dev/null
126+
127+
# Display native macOS password dialog
128+
local password
129+
password=$(osascript -e "display dialog \"$prompt_msg\" default answer \"\" with title \"Mole\" with icon caution with hidden answer" -e 'text returned of result' 2> /dev/null)
130+
131+
if [[ -z "$password" ]]; then
132+
# User cancelled the dialog
133+
unset password
117134
return 1
118135
fi
136+
137+
# Attempt sudo authentication with the provided password
138+
if printf '%s\n' "$password" | sudo -S -p "" -v > /dev/null 2>&1; then
139+
unset password
140+
return 0
141+
fi
142+
143+
# Password was incorrect
144+
unset password
145+
return 1
119146
fi
120147

121148
sudo -k

0 commit comments

Comments
 (0)