Skip to content

Commit 5ce60ef

Browse files
author
Jacob Gray
authored
Merge pull request #3 from Jacob-Gray/development
Finally fixed the issue that was breaking the library
2 parents 17c7d93 + 5383baa commit 5ce60ef

File tree

6 files changed

+176
-48
lines changed

6 files changed

+176
-48
lines changed

ChatExchangeJS/browser.js

Lines changed: 101 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
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+
514
const 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

ChatExchangeJS/client.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const Browser = require("./browser"),
2+
Request = require("request"),
23
Room = require("./room");
34

45
class Client {
56

67
constructor(host) {
78
this.host = host;
89

10+
/**
11+
* Store cookies separately for each client
12+
*/
13+
this.jar = Request.jar();
14+
915
this._br = new Browser(this);
1016
}
1117

@@ -19,7 +25,7 @@ class Client {
1925
if (this._br.loggedIn) {
2026
return new Room(this, room_id).join();
2127
}
22-
28+
2329
throw new Error("Cannot join room " + room_id + "@" + this.host + ": Not logged in!");
2430
}
2531
}

ChatExchangeJS/config.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ var config = {
33

44
return {
55
"key": "https://" + host + "/users/login?returnurl=%2f%2f" + host,
6-
"submit": "https://" + host + "/users/authenticate",
6+
"submit": "https://" + host + "/users/login",
77
"confirm": "https://" + host + "/"
88
}
99
},
10-
"chat": {
11-
"sendMessage": (host, room) => "http://chat." + host + "/chats/" + room + "/messages/new",
12-
"editMessage": (host, id) => "http://chat." + host + "/messages/" + id,
13-
"deleteMessage": (host, id) => "http://chat." + host + "/messages/" + id + "/delete",
10+
openid: {
11+
key: "https://openid.stackexchange.com/account/login/",
12+
submit: "https://openid.stackexchange.com/account/login/submit",
13+
confirm: "https://openid.stackexchange.com/user",
14+
},
15+
chat: {
16+
sendMessage: (host, room) => "http://chat." + host + "/chats/" + room + "/messages/new",
17+
editMessage: (host, id) => "http://chat." + host + "/messages/" + id,
18+
deleteMessage: (host, id) => "http://chat." + host + "/messages/" + id + "/delete",
1419
}
1520
}
1621
module.exports = config;

demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function Session(me) {
6666

6767
async function main() {
6868
//Create new client for site
69-
var me = new Client("stackoverflow.com");
69+
var me = new Client("3dprinting.stackexchange.com");
7070

7171
console.log("Logging in")
7272

package-lock.json

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
"repository": "git@github.com:Jacob-Gray/ChatExchangeJS.git",
77
"author": "Jacob Gray <jacob@jacobgray.me>",
88
"license": "GPL-3.0",
9-
"keywords": ["stackexchange", "chat", "chatexchange", "api", "chatexchangejs"],
9+
"keywords": [
10+
"stackexchange",
11+
"chat",
12+
"chatexchange",
13+
"api",
14+
"chatexchangejs"
15+
],
1016
"scripts": {
1117
"up": "up.cmd",
1218
"build": "docker build --no-cache -t chatexchangejs ."
@@ -17,4 +23,4 @@
1723
"request": "^2.79.0",
1824
"ws": "^1.1.1"
1925
}
20-
}
26+
}

0 commit comments

Comments
 (0)