@@ -41,7 +41,7 @@ class << self
4141 # @option options [String] :online if `true` only a temporary access token will be provided,
4242 # a long-lived refresh token will not be created and stored on the filesystem.
4343 # @option options [String] :port port for local server to listen on to capture oauth browser redirect.
44- # Defaults to 1234. Set to nil or 0 to use an out-of-band authentication process.
44+ # Defaults to 1234.
4545 # @option options [String] :client_id Google client ID
4646 # @option options [String] :client_secret Google client secret
4747 def initialize ( options = { } )
@@ -98,18 +98,7 @@ def google_oauth
9898 credentials . tap ( &storage . method ( :write_credentials ) )
9999 end
100100
101- def silence_output
102- outs = [ $stdout, $stderr]
103- clones = outs . map ( &:clone )
104- outs . each { |io | io . reopen '/dev/null' }
105- yield
106- ensure
107- outs . each_with_index { |io , i | io . reopen ( clones [ i ] ) }
108- end
109-
110101 def get_oauth_code ( client , options )
111- raise 'fallback' unless @port && !@port . zero?
112-
113102 require 'launchy'
114103 require 'webrick'
115104 code = nil
@@ -120,35 +109,27 @@ def get_oauth_code(client, options)
120109 )
121110 server . mount_proc '/' do |req , res |
122111 code = req . query [ 'code' ]
123- res . status = 202
124- res . body = 'Login successful, you may close this browser window.'
112+ if code
113+ res . status = 202
114+ res . body = 'Login successful, you may close this browser window.'
115+ else
116+ res . status = 500
117+ res . body = "Authentication failed. Received a request to http://localhost:#{ @port } that should complete Google OAuth flow, but no code was received."
118+ end
125119 server . stop
126120 end
127- trap ( 'INT' ) { server . shutdown }
128- client . redirect_uri = "http://localhost:#{ @port } "
129- silence_output do
130- launchy = Launchy . open ( client . authorization_uri ( options ) . to_s )
131- server_thread = Thread . new do
132- begin
133- server . start
134- ensure server . shutdown
135- end
136- end
137- while server_thread . alive?
138- raise 'fallback' if !launchy . alive? && !launchy . value . success?
139121
140- sleep 0.1
141- end
122+ client . redirect_uri = "http://localhost:#{ @port } "
123+ Launchy . open ( client . authorization_uri ( options ) . to_s ) do |exception |
124+ puts "Couldn't open browser, please authenticate with Google using this link:"
125+ puts client . authorization_uri ( options ) . to_s
126+ puts
127+ puts "Note: link must be opened on this computer, as Google will redirect to #{ client . redirect_uri } to complete authentication."
142128 end
143- code || raise ( 'fallback' )
144- rescue StandardError
145- trap ( 'INT' , 'DEFAULT' )
146- # Fallback to out-of-band authentication if browser launch failed.
147- client . redirect_uri = 'oob'
148- return ENV [ 'OAUTH_CODE' ] if ENV [ 'OAUTH_CODE' ]
149-
150- raise RuntimeError , 'Open the following URL in a browser to get a code,' \
151- "export to $OAUTH_CODE and rerun:\n #{ client . authorization_uri ( options ) } " , [ ]
129+
130+ server . start
131+
132+ code or raise 'Failed to get OAuth code from Google'
152133 end
153134
154135 def refresh
0 commit comments