@@ -146,72 +146,50 @@ def check
146146 end
147147
148148 def authenticate
149- vprint_status ( 'Authenticating...' )
150- res = send_request_cgi (
149+ nounce = nil
150+
151+ opts = {
151152 'uri' => normalize_uri ( target_uri . path , '/publisher/services/auth/login' ) ,
152153 'method' => 'GET' ,
154+ 'headers' => {
155+ 'Connection' => 'keep-alive'
156+ } ,
153157 'keep_cookies' => true
154- )
155-
156- fail_with ( Failure ::UnexpectedReply , 'Failed to authenticate' ) unless res
157-
158- nounce = nil
159- loop_dectector = 0
160-
161- while res . redirect?
162- loop_dectector += 1
163- res = send_request_cgi (
164- 'uri' => "#{ res . redirection . path } ?#{ res . redirection . query } " ,
165- 'method' => 'GET' ,
166- 'headers' => {
167- 'Connection' => 'keep-alive'
168- } ,
169- 'keep_cookies' => true
170- )
171-
172- if res &.get_cookies && res . get_cookies . match ( /sessionNonceCookie-(.*)=/ )
173- vprint_status ( 'Got session nonce' )
174- nounce = ::Regexp . last_match ( 1 )
175- end
176- break if nounce
177-
178- fail_with ( Failure ::UnexpectedReply , 'Loop detected' ) if loop_dectector > 3
158+ }
159+ res = send_request_cgi! ( opts , 20 , 1 ) # timeout and redirect_depth
179160
161+ if res &.get_cookies && res . get_cookies . match ( /sessionNonceCookie-(.*)=/ )
162+ vprint_status ( 'Got session nonce' )
163+ nounce = ::Regexp . last_match ( 1 )
180164 end
181165
166+ fail_with ( Failure ::UnexpectedReply , 'Failed to authenticate. Could not get session nonce' ) unless nounce
167+
182168 auth_data = {
183169 'usernameUserInput' => datastore [ 'HttpUsername' ] ,
184170 'username' => datastore [ 'HttpUsername' ] ,
185171 'password' => datastore [ 'HttpPassword' ] ,
186172 'sessionDataKey' => nounce
187173 }
188174
189- res = send_request_cgi (
175+ opts = {
190176 'uri' => normalize_uri ( target_uri . path , '/commonauth' ) ,
191177 'method' => 'POST' ,
178+ 'headers' => {
179+ 'Connection' => 'keep-alive'
180+ } ,
181+ 'keep_cookies' => true ,
192182 'vars_post' => auth_data
193- )
183+ }
194184
195- loop_dectector = 0
196- while res . redirect?
197- loop_dectector += 1
198- res = send_request_cgi (
199- 'uri' => "#{ res . redirection . path } ?#{ res . redirection . query } " ,
200- 'method' => 'GET' ,
201- 'headers' => {
202- 'Connection' => 'keep-alive'
203- } ,
204- 'keep_cookies' => true
205- )
206- if res &.get_cookies && res . get_cookies . match ( /:?WSO2_AM_TOKEN_1_Default=([\w |-]+);\s / )
207- self . bearer = ::Regexp . last_match ( 1 )
208- end
209- break if bearer
185+ res = send_request_cgi! ( opts , 20 , 1 ) # timeout and redirect_depth
210186
211- fail_with ( Failure ::UnexpectedReply , 'Loop detected' ) if loop_dectector > 3
187+ if res &.get_cookies && res . get_cookies . match ( /:?WSO2_AM_TOKEN_1_Default=([\w |-]+);\s / )
188+ vprint_status ( 'Got bearer token' )
189+ self . bearer = ::Regexp . last_match ( 1 )
212190 end
213191
214- fail_with ( Failure ::UnexpectedReply , 'Authentication attempt failed' ) unless bearer
192+ fail_with ( Failure ::UnexpectedReply , 'Authentication attempt failed. Could not get bearer token ' ) unless bearer
215193
216194 print_good ( 'Authentication successful' )
217195 end
0 commit comments