Skip to content

Commit

Permalink
add support for checkpoint verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ifedapoolarewaju committed Sep 20, 2018
1 parent 2e6ad17 commit 8f76506
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
23 changes: 23 additions & 0 deletions browser/checkpoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>IG:dm - Instagram Desktop Messenger</title>

This comment has been minimized.

Copy link
@ailton123
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" type="text/css" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/login.css">
</head>
<body>
<div class="container cardBox pb-5 pt-3 pt-sm-5">
<form class="pageCard mx-auto p-4 p-sm-5">
<div class="clearfix welcomeBox"><span class="d-block text-center">Please enter the verification code sent to your e-mail address</span></div>
<div class="input-group loginInputs mt-4">
<input class="form-control code" type="text" name="code" required="true" placeholder="Code">
</div>
<button class="loginButton mt-3 btn btn-primary" type="submit">Submit Code</button><span class="d-block errorMessage text-center mt-3" id="error"></span>
</form>
</div>
<script type="text/javascript" src="js/checkpoint.js"></script>
</body>
</html>
13 changes: 13 additions & 0 deletions browser/js/checkpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var electron = require('electron');
var ipcRenderer = electron.ipcRenderer;

document.addEventListener('DOMContentLoaded', () => {
document.querySelector('form').onsubmit = (e) => {
e.preventDefault()
let button = document.querySelector('button[type=submit]');
button.innerText = 'Submitting code...'
button.classList.add('loggingIn', 'disabled');
const code = document.querySelector('input[name=code]').value;
ipcRenderer.send('checkpointCode', {code})
}
})
15 changes: 15 additions & 0 deletions browser/views/checkpoint.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends _layout

append head
link(rel='stylesheet', type='text/css', href='css/login.css')

block content
.container.cardBox.pb-5.pt-3.pt-sm-5
form.pageCard.mx-auto.p-4.p-sm-5
.clearfix.welcomeBox
span.d-block.text-center Please enter the verification code sent to your e-mail address
.input-group.loginInputs.mt-4
input.form-control.code(type='text', name='code', required='true', placeholder='Code')
button.loginButton.mt-3.btn.btn-primary(type='submit') Submit Code
span#error.d-block.errorMessage.text-center.mt-3
script(type='text/javascript', src='js/checkpoint.js')
14 changes: 12 additions & 2 deletions main/instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ exports.checkAuth = function (session) {
});
}

exports.login = function (username, password) {
utils.clearCookieFiles();
exports.login = function (username, password, keepLastSession) {
if (!keepLastSession) {
utils.clearCookieFiles();
}
return new Promise((resolve, reject) => {
const device = utils.getDevice(username);
const storage = utils.getCookieStorage(`${username}.json`);
Expand All @@ -31,6 +33,14 @@ exports.logout = function () {
utils.clearCookieFiles();
}

exports.isCheckpointError = (error) => {
return (error instanceof Client.Exceptions.CheckpointError)
}

exports.startCheckpoint = (error) => {
return Client.Web.Challenge.resolve(error, 'email')
}

exports.getChatList = function (session) {
return new Promise((resolve, reject) => {
var feed = new Client.Feed.Inbox(session, 10);
Expand Down
56 changes: 50 additions & 6 deletions main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ function createWindow () {
mainWindow.on('closed', () => mainWindow = null)
}

function createCheckpointWindow() {
const checkpointWindow = new BrowserWindow({
width: 300,
height: 300,
resizable: false,
icon: `${__dirname}/../browser/img/icon.png`,
})
checkpointWindow.setTitle('IG:dm - Instagram verification code')
checkpointWindow.loadURL(url.format({
pathname: path.join(__dirname, '../browser/checkpoint.html'),
protocol: 'file:',
slashes: true
}))
return checkpointWindow
}

function getChatList () {
instagram.getChatList(session).then((chats) => {
mainWindow.webContents.send('chatList', chats)
Expand All @@ -73,6 +89,20 @@ function getChat (evt, id) {
}).catch(() => setTimeout(getChat, RATE_LIMIT_DELAY, evt, id))
}

function handleCheckpoint (checkpointError) {
return new Promise((resolve, reject) => {
instagram.startCheckpoint(checkpointError)
.then((challenge) => {
const cpWindow = createCheckpointWindow()
electron.ipcMain.on('checkpointCode', (evt, data) => {
electron.ipcMain.removeAllListeners('checkpointCode')
cpWindow.close()
challenge.code(data.code).then(resolve).catch(reject)
})
}).catch(reject)
})
}

app.on('ready', () => {
createWindow();
// only set the menu template when in production mode/
Expand Down Expand Up @@ -110,18 +140,32 @@ electron.ipcMain.on('login', (evt, data) => {
if(data.username === "" || data.password === "") {
return mainWindow.webContents.send('loginError', "Please enter all required fields");
}
instagram.login(data.username, data.password).then((session_) => {
session = session_
createWindow()
}).catch((error) => {
const login = (keepLastSession) => {
instagram.login(data.username, data.password, keepLastSession).then((session_) => {
session = session_
createWindow()
}).catch((error) => {
if (instagram.isCheckpointError(error)) {
handleCheckpoint(error)
.then(() => login(true))
.catch(() => mainWindow.webContents.send('loginError', getErrorMsg(error)))
} else {
mainWindow.webContents.send('loginError', getErrorMsg(error));
}
})
}

const getErrorMsg = (error) => {
let message = 'An unknown error occurred.';
if (error.message) {
message = error.message;
} else if (error.hasOwnProperty('json') && !!error.json.two_factor_required) {
message = 'You have two factor authentication enabled. Two factor authentication is not yet supported.';
}
mainWindow.webContents.send('loginError', message);
})
return message
}

login()
})

electron.ipcMain.on('logout', () => {
Expand Down

0 comments on commit 8f76506

Please sign in to comment.