@@ -108,24 +108,28 @@ def silence_output
108108 end
109109
110110 def get_oauth_code ( client , options )
111- raise 'fallback' unless @port && !@port . zero?
111+ raise ArgumentError . new ( 'Missing port for local oauth server' ) unless @port && !@port . zero?
112112
113113 require 'launchy'
114114 require 'webrick'
115+
115116 code = nil
116117 server = WEBrick ::HTTPServer . new (
117118 Port : @port ,
118119 Logger : WEBrick ::Log . new ( STDOUT , 0 ) ,
119120 AccessLog : [ ]
120121 )
122+
121123 server . mount_proc '/' do |req , res |
122124 code = req . query [ 'code' ]
123125 res . status = 202
124126 res . body = 'Login successful, you may close this browser window.'
125- server . stop
127+ server . shutdown
126128 end
129+
127130 trap ( 'INT' ) { server . shutdown }
128131 client . redirect_uri = "http://localhost:#{ @port } "
132+
129133 silence_output do
130134 launchy = Launchy . open ( client . authorization_uri ( options ) . to_s )
131135 server_thread = Thread . new do
@@ -134,21 +138,13 @@ def get_oauth_code(client, options)
134138 ensure server . shutdown
135139 end
136140 end
137- while server_thread . alive?
138- raise 'fallback' if !launchy . alive? && !launchy . value . success?
139-
140- sleep 0.1
141- end
141+
142+ server_thread . join
142143 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' ]
149144
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 ) } " , [ ]
145+ code || raise ( 'Local Google Oauth failed to get code' )
146+ ensure
147+ trap ( 'INT' , 'DEFAULT' )
152148 end
153149
154150 def refresh
0 commit comments