Module: Shai::Commands::Auth
- Included in:
- Shai::CLI
- Defined in:
- lib/shai/commands/auth.rb
Constant Summary collapse
- DEVICE_FLOW_TIMEOUT =
10 minutes
600
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/shai/commands/auth.rb', line 10 def self.included(base) base.class_eval do desc "login", "Log in to shaicli.dev" option :password, type: :boolean, default: false, desc: "Use password authentication instead of device flow" def login if [:password] login_with_password else login_with_device_flow end end desc "logout", "Log out and remove stored credentials" def logout credentials.clear ui.success("Logged out successfully") end desc "whoami", "Show current authentication status" def whoami if credentials.authenticated? name = credentials.display_name || credentials.username ui.info("Logged in as #{credentials.username} (#{name})") ui.info("Token expires: #{format_date(credentials.expires_at)}") else ui.info("Not logged in. Run `shai login` to authenticate.") end end no_commands do def login_with_password identifier = ui.ask("Email or username:") password = ui.mask("Password:") ui.blank begin response = ui.spinner("Logging in...") do api.login(identifier: identifier, password: password) end save_credentials_from_response(response) rescue AuthenticationError ui.error("Invalid credentials") exit EXIT_AUTH_REQUIRED rescue RateLimitError ui.error("Too many login attempts. Please try again later.") exit EXIT_GENERAL_ERROR rescue NetworkError => e ui.error(e.) exit EXIT_NETWORK_ERROR end end def login_with_device_flow ui.info("Starting device authorization...") ui.blank begin response = api. rescue RateLimitError => e retry_msg = e.retry_after ? " Try again in #{e.retry_after} seconds." : "" ui.error("Too many authorization attempts.#{retry_msg}") exit EXIT_GENERAL_ERROR rescue NetworkError => e ui.error(e.) exit EXIT_NETWORK_ERROR end device_code = response["device_code"] user_code = response["user_code"] verification_uri = response["verification_uri"] verification_uri_complete = response["verification_uri_complete"] interval = response["interval"] || 5 display_device_code_instructions( user_code: user_code, verification_uri: verification_uri ) ui.blank if ui.yes?("Open browser automatically?", default: true) open_browser(verification_uri_complete || verification_uri) end ui.blank (device_code: device_code, interval: interval) end def display_device_code_instructions(user_code:, verification_uri:) ui.info("To authorize this device:") ui.blank ui.indent("1. Visit: #{ui.cyan(verification_uri)}") ui.indent("2. Enter code: #{ui.bold(user_code)}") end def open_browser(url) Launchy.open(url) rescue Launchy::CommandNotFoundError ui.warning("Could not open browser automatically. Please visit the URL manually.") end def (device_code:, interval:) start_time = Time.now current_interval = interval spinner = TTY::Spinner.new("[:spinner] Waiting for authorization...", format: :dots) spinner.auto_spin loop do elapsed = Time.now - start_time if elapsed > DEVICE_FLOW_TIMEOUT spinner.error ui.error("Authorization timed out. Please try again.") exit EXIT_AUTH_REQUIRED end sleep(current_interval) begin response = api.device_token(device_code: device_code) # Success - we got a token spinner.success save_credentials_from_response(response) return rescue DeviceFlowError => e case e.error_code when "authorization_pending" # Keep polling next when "slow_down" current_interval = e.interval || (current_interval + 5) next when "access_denied" spinner.error ui.blank ui.error("Authorization denied by user.") exit EXIT_AUTH_REQUIRED when "expired_token" spinner.error ui.blank ui.error("Device code expired. Please try again.") exit EXIT_AUTH_REQUIRED else spinner.error ui.blank ui.error("Authorization failed: #{e.error_code}") exit EXIT_AUTH_REQUIRED end rescue NetworkError => e spinner.error ui.blank ui.error(e.) exit EXIT_NETWORK_ERROR end end end def save_credentials_from_response(response) data = response["data"] credentials.save( token: data["token"], expires_at: data["expires_at"], user: data["user"] ) ui.blank ui.success("Logged in as #{data.dig("user", "username")}") ui.indent("Token expires: #{format_date(data["expires_at"])}") ui.indent("Token stored in #{Shai.configuration.credentials_path}") end end end end |