1- var request_lib = require ( "request" ) ,
2- _ = require ( 'cheerio' ) ,
1+ var Request = require ( "request" ) ,
2+ {
3+ URL
4+ } = require ( "url" ) ,
5+ cheerio = require ( 'cheerio' ) ,
36 WebSocket = require ( 'ws' ) ;
47
8+ const session = Request . jar ( ) ,
9+ request = Request . defaults ( {
10+ jar : session ,
11+ followAllRedirects : true ,
12+ } ) ;
13+
514const https = require ( "https" ) ,
615 querystring = require ( "querystring" ) ;
716
@@ -304,8 +313,6 @@ function Browser(client) {
304313 this . stackexchange . submit = config . login . stackexchange . submit ( host ) ;
305314 this . stackexchange . confirm = config . login . stackexchange . confirm ( host ) ;
306315
307- this . request ( )
308-
309316 return this . openid_login ( ) . then ( this . site_login ) ;
310317
311318
@@ -319,62 +326,117 @@ class Browser2 {
319326 this . client = client ;
320327 }
321328
322- request ( uri , method , data = { } , urlargs = { } ) {
329+ request ( options ) {
323330
324331 return new Promise ( ( resolve , reject ) => {
325332
326- if ( method === "GET" ) urlargs = data ;
333+ request ( options , ( err , res , body ) => {
334+ if ( err ) reject ( err ) ;
335+ resolve ( res ) ;
336+ } ) ;
337+ } ) ;
338+ }
327339
328- const postData = querystring . stringify ( data ) ,
329- args = querystring . stringify ( urlargs ) ,
330- options = new URL ( uri + "?" + args ) ;
340+ cookies ( host ) {
341+ let cookies = session . getCookies ( host ) ,
342+ output = { } ;
331343
344+ cookies . forEach ( cookie => {
345+ output [ cookie . key ] = cookie ;
346+ } ) ;
332347
333- options . method = method ;
334- options . headers = {
335- 'Content-Type' : 'application/x-www-form-urlencoded' ,
336- 'Content-Length' : Buffer . byteLength ( postData )
337- }
348+ return output ;
349+ }
338350
339- const req = https . request ( options , res => {
351+ async html ( url ) {
340352
341- res . setEncoding ( 'utf8' ) ;
353+ let responseHTML = await this . request ( {
354+ method : "GET" ,
355+ url : url
356+ } ) ;
342357
343- let data = [ ] ;
358+ return cheerio . load ( responseHTML . body ) ;
359+ }
344360
345- res . on ( 'data' , chunk => {
346- data . push ( chunk ) ;
347- } ) ;
361+ async getValues ( url , els ) {
348362
349- res . on ( 'end' , ( ) => {
350- let body = data . join ( "" ) ,
351- output ;
363+ let $ = await this . html ( url ) ,
364+ output = { } ;
352365
353- try {
354- output = JSON . parse ( body ) ;
355- } catch ( e ) {
356- output = body ;
357- }
366+ for ( let el of els ) {
358367
359- resolve ( output ) ;
360- } ) ;
361- } ) ;
368+ el = $ ( `[name="${ el } "]` ) ;
362369
363- req . on ( 'error' , e => {
364- reject ( e ) ;
370+ output [ el . attr ( "name" ) ] = el . val ( ) ;
371+ }
372+
373+ return output ;
374+ }
375+
376+ async handleAccountConfirmation ( response ) {
377+
378+ if ( response . request . uri . href . indexOf ( "https://openid.stackexchange.com/account/prompt" ) > - 1 ) {
379+
380+ console . log ( "NEEDS ACCOUNT CONFIRMATION" )
381+
382+ let data = await this . getValues ( response . request . uri . href , [ "fkey" , "session" ] ) ;
383+
384+ console . log ( data )
385+
386+ let openid_response = await this . request ( {
387+ method : "POST" ,
388+ url : "https://openid.stackexchange.com/account/prompt/submit" ,
389+ form : data
365390 } ) ;
366391
367- // write data to request body
368- req . write ( postData ) ;
369- req . end ( ) ;
392+ console . log ( openid_response . request . uri . href )
393+ }
394+ }
395+
396+ async openid ( email , password ) {
397+
398+ let fkey = ( await this . getValues ( config . openid . key , [ "fkey" ] ) ) . fkey ;
399+
400+ let openid_response = await this . request ( {
401+ method : "POST" ,
402+ url : config . openid . submit ,
403+ form : {
404+ email : email ,
405+ password : password ,
406+ fkey : fkey
407+ }
408+ } ) ;
409+
410+ if ( ! this . cookies ( config . openid . submit ) . usr ) throw new Error ( "Was unable to login with the provided credentials, please verify them." ) ;
411+ }
412+
413+ async siteLogin ( host ) {
414+
415+ let stackexchange = config . stackexchange ( host ) ,
416+ fkey = ( await this . getValues ( stackexchange . key , [ "fkey" ] ) ) . fkey ;
417+
418+ let site_response = await this . request ( {
419+ method : "POST" ,
420+ url : stackexchange . submit ,
421+ form : {
422+ 'oauth_version' : '' ,
423+ 'oauth_server' : '' ,
424+ 'openid_identifier' : 'https://openid.stackexchange.com/' ,
425+ fkey : fkey
426+ }
370427 } ) ;
428+
429+ this . handleAccountConfirmation ( site_response ) ;
371430 }
372431
373- async login ( host , email , password ) {
432+ async login ( host , email , password ) {
433+
434+ let x = await this . getValues ( config . openid . key , [ "fkey" ] )
374435
375- let links = config . stackexchange ( host ) ;
436+ console . log ( x )
376437
377- console . log ( links )
438+ await this . openid ( email , password ) ;
439+ await this . siteLogin ( host ) ;
378440 }
379441}
380442
0 commit comments