Skip to content

Remove oob fallback and launchy liveness check #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions lib/aws/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,28 @@ def silence_output
end

def get_oauth_code(client, options)
raise 'fallback' unless @port && !@port.zero?
raise ArgumentError.new('Missing port for local oauth server') unless @port && !@port.zero?

require 'launchy'
require 'webrick'

code = nil
server = WEBrick::HTTPServer.new(
Port: @port,
Logger: WEBrick::Log.new(STDOUT, 0),
AccessLog: []
)

server.mount_proc '/' do |req, res|
code = req.query['code']
res.status = 202
res.body = 'Login successful, you may close this browser window.'
server.stop
server.shutdown
end

trap('INT') { server.shutdown }
client.redirect_uri = "http://localhost:#{@port}"

silence_output do
launchy = Launchy.open(client.authorization_uri(options).to_s)
server_thread = Thread.new do
Expand All @@ -134,21 +138,13 @@ def get_oauth_code(client, options)
ensure server.shutdown
end
end
while server_thread.alive?
raise 'fallback' if !launchy.alive? && !launchy.value.success?

sleep 0.1
end

server_thread.join
end
code || raise('fallback')
rescue StandardError
trap('INT', 'DEFAULT')
# Fallback to out-of-band authentication if browser launch failed.
client.redirect_uri = 'oob'
return ENV['OAUTH_CODE'] if ENV['OAUTH_CODE']

raise RuntimeError, 'Open the following URL in a browser to get a code,' \
"export to $OAUTH_CODE and rerun:\n#{client.authorization_uri(options)}", []
code || raise('Local Google Oauth failed to get code')
ensure
trap('INT', 'DEFAULT')
end

def refresh
Expand Down
2 changes: 1 addition & 1 deletion lib/aws/google/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Aws
class Google
VERSION = '0.2.2'.freeze
VERSION = '0.2.3'.freeze
end
end