From: Marnanel Thurman Date: Sun, 19 Dec 2021 08:39:03 +0000 (+0000) Subject: Challenge and cookies picked up correctly by grok_login_1 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~tthurman/git?a=commitdiff_plain;h=8b8784970a0d1f1e698b670e73d9e59948abddb6;p=dwim.git Challenge and cookies picked up correctly by grok_login_1 --- diff --git a/App.js b/App.js index 299def3..dc5ec0d 100644 --- a/App.js +++ b/App.js @@ -34,13 +34,15 @@ export default class App extends React.Component { grok_set_url(this.state.server); - grok_login1(this.handle_login1) + grok_login1() .then((login1) => { + console.log('g_l1 done'); + console.log(login1); this.ui_login_show_message("Logging in..."); + return; grok_login2( - this.handle_login2, login1['auth'], this.state.username, this.state.password, @@ -52,6 +54,7 @@ export default class App extends React.Component { }); } ).catch((error) => { + console.log("g_l1 failed"); this.ui_login_show_message(String(error)); }); }; diff --git a/grok.js b/grok.js index 89d949d..546f8f6 100644 --- a/grok.js +++ b/grok.js @@ -1,45 +1,39 @@ +import * as htmlparser2 from 'htmlparser2'; import React from 'react'; import { SafeAreaView, StyleSheet, View, - TouchableOpacity, Text, } from 'react-native'; import * as SecureStore from 'expo-secure-store'; import tough from 'tough-cookie'; -import DOMParser from 'react-native-html-parser'; /////////////////////////////////////// -//var DREAMWIDTH_URL = 'https://dreamwidth.org'; -var DREAMWIDTH_URL = 'https://marnanel.org'; // FIXME temporary +var DREAMWIDTH_URL = 'https://dreamwidth.org'; +//var DREAMWIDTH_URL = 'https://marnanel.org'; // FIXME temporary export var grok_url = DREAMWIDTH_URL; /////////////////////////////////////// -async function cookie_string(response) { +function cookie_string(response) { - var cookies; + console.log('Setting cookies.'); - console.log('Setting cookies from: '+response); + console.log(response.headers); + console.log(response.headers['set-cookie']); - if (!response.headers['set-cookie']) { + if (!response.headers.has('set-cookie')) { console.log(" -- no cookies to set"); - return; + return null; } - if (response.headers['set-cookie'] instanceof Array) - cookies = response.headers['set-cookie'].map(Cookie.parse); - else - cookies = [Cookie.parse(response.headers['set-cookie'])]; + var result = ""+tough.Cookie.parse(response.headers.get('set-cookie')); - console.log(" -- received cookies: "+cookies) - - var result = cookies.cookieString(); console.log(" -- using cookies: "+result) return result; } @@ -49,7 +43,7 @@ function make_post_body(params) { for (var key in params) { var encKey = encodeURIComponent(key); var encValue = encodeURIComponent(params[key]); - f.push(encodedKey + '=' + encodedValue); + f.push(encKey + '=' + encValue); } return f.join('&'); } @@ -66,35 +60,91 @@ export async function grok_login1() { return new Promise((resolve, reject) => { var result = {}; + console.log("grok_login1 begins..."); fetch(grok_url, { method: 'GET', }).then((response) => { - var parser = new DOMParser.DOMParser(); - var doc = parser.parseFromString( - response.text(), 'text/html'); + if (!response.ok) { + console.log(" -- fetch failed"); + result['success'] = false; + result['error'] = Response.statusText; + reject(result); + return; + } + + response.text().then(function(text) { - result['auth'] = doc.querySelectorAll( - 'name="lj_form_auth"]')[0]. - getAttribute('value'); + console.log(" -- fetch succeeded"); - result['cookies'] = cookie_string(response); + var hd; + try { + hd = htmlparser2.parseDocument(text); + } catch(err) { + console.log("parser failed:"); + console.log(err); + return; + } - resolve(result); + console.log("2"); + + const parser = new htmlparser2.Parser({ + onopentag(name, attributes) { + if (attributes['name'] == "chal") { + console.log(" -- it's chal"); + console.log(attributes['value']); + result['chal'] = attributes['value']; + } + }, + }); + parser.write(text); + parser.end(); + + result['cookies'] = cookie_string(response); + + console.log(result); + + resolve(result); + + }, function(error) { + + console.log("parse fails"); + console.log(JSON.stringify(error)); + + result['success'] = false; + result['error'] = error; + reject(result); + }); }).catch((error) => { - reject(error); + + console.log("fetch fails"); + console.log(error); + console.log(JSON.stringify(error)); + + result['success'] = false; + result['error'] = error; + reject(result); + + }); }); }; -export async function grok_login2(auth, username, password) { +export async function grok_login2( + auth, + username, + password, + cookies, +) { return new Promise((resolve, reject) => { var result = {}; + console.log("grok_login2 begins..."); + fetch(grok_url, { method: 'POST', @@ -117,22 +167,17 @@ export async function grok_login2(auth, username, password) { console.log('----'); console.log(response.data); - var parser = new DOMParser(); - var doc = parser.parseFromString( - response.data, 'text/html'); - - var h1 = doc.querySelectorAll( - 'h1')[0]. - innerHTML(); + var doc = cheerio.load(text); + var h1 = cheerio('h1').html(); if (h1.includes('Welcome back')) { result['success'] = true; } else { result['success'] = false; - var blockquote = doc.querySelectorAll( + var blockquote = doc.querySelect( 'blockquote' - )[0].innerHTML(); + ).innerHTML(); if (blockquote.includes('wrong password')) { result['message'] = 'Wrong password.'; @@ -149,6 +194,8 @@ export async function grok_login2(auth, username, password) { resolve(result); }).catch((error) => { + console.log("grok_login2 fails"); + console.log(JSON.stringify(error)); reject(error); }); }); diff --git a/package.json b/package.json index 806944a..17f2c1a 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@react-navigation/bottom-tabs": "^6.0.5", "@react-navigation/native": "^6.0.2", "@react-navigation/native-stack": "^6.1.0", + "cheerio": "^1.0.0-rc.10", "expo": "~43.0.2", "expo-asset": "~8.4.3", "expo-constants": "~12.1.3", @@ -33,11 +34,11 @@ "expo-splash-screen": "~0.13.5", "expo-status-bar": "~1.1.0", "expo-web-browser": "~10.0.3", + "htmlparser2": "^7.2.0", "jquery": "^3.6.0", "react": "17.0.1", "react-dom": "17.0.1", "react-native": "0.64.3", - "react-native-html-parser": "^0.1.0", "react-native-safe-area-context": "3.3.2", "react-native-screens": "~3.8.0", "react-native-web": "0.17.1",