Commit b034f021 authored by Alexei's avatar Alexei

Add OAuth authentication

parent 7c9ce300
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel='stylesheet' href='css/main.css' type='text/css'>
<title>Authorize application - qvitter.js</title>
</head>
<body>
<script>
'use strict';
const ipc = require('electron').ipcRenderer;
var element = document.createElement('input');
element.type = 'text';
element.id = 'pin-input';
element.className = 'pin-input';
element.name = 'pin';
element.maxLength = 7;
element.style.width = '56px';
document.body.appendChild(element);
element = document.createElement('input');
element.type = 'submit';
element.value = 'Submit';
element.onclick = function() {
ipc.send('verifier-ping', document.querySelector('.pin-input').value);
console.log("sending data");
};
document.body.appendChild(element);
</script>
</body>
</html>
\ No newline at end of file
'use strict';
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
const ipcRenderer = require('electron').ipcRenderer;
var fs = require('fs'),
xml2js = require('xml2js'),
$ = require('jquery');
var parseString = require('xml2js').parseString;
var OAuth = require('oauth');
var apiUrl = null;
var access_token = {
public: '',
secret: '',
};
var allowed = false;
var GSInstance = {
fancyLink: 0,
language: '',
email: '',
closed: 0,
inviteOnly: 0,
private: 0,
textLimit: 0,
ssl: ''
};
var oauth = new OAuth.OAuth(
'https://quitter.no/api/oauth/request_token',
'https://quitter.no/api/oauth/access_token',
'e16cc7fd32c52ea10faedcbdb789f55b',
'fb69e34abb5828111b4e2e6d0ace7fed',
'1.0',
'oob',
'HMAC-SHA1'
);
var verifier = null;
ipcRenderer.on('verifier-pong', function (event, data) {
verifier = data;
});
$(function() {
ipcRenderer.send('did-finish-load');
});
ipcRenderer.on('needs-auth', function(event, data) {
oauth.getOAuthRequestToken(handleRequestToken);
});
ipcRenderer.on('token', function(event, data) {
access_token = data;
console.log(access_token);
});
var authorizeWindow = null;
function handleRequestToken(err, token, token_secret, parsedQueryString) {
if (err === null) {
if (parsedQueryString.oauth_callback_confirmed === 'true') {
authorizeWindow = new BrowserWindow({
width: 800,
height: 600,
autoHideMenuBar: true
});
authorizeWindow.loadURL('https://quitter.no/api/oauth/authorize?oauth_token=' + token);
authorizeWindow.on('closed', function () {
authorizeWindow = new BrowserWindow({
width: 800,
height: 600,
autoHideMenuBar: true
});
authorizeWindow.loadURL('file://' + __dirname + '/authorize.html');
authorizeWindow.webContents.openDevTools();
authorizeWindow.on('closed', function () {
authorizeWindow = null;
if (verifier != null)
oauth.getOAuthAccessToken(token, token_secret, verifier, handleAccessToken);
});
});
}
}
}
function handleAccessToken(err, token, token_secret, parsedString) {
if (err === null) {
access_token.public = token;
access_token.secret = token_secret;
}
ipcRenderer.send('save-token', access_token);
}
function changeTab(tab) {
switch (tab) {
case 'public':
loadPublicTimeline();
break;
case 'home':
loadHomeTimeline();
break;
case 'mentions':
loadMentions();
break;
}
}
/*$.get('https://quitter.se/rsd.xml', function (data) {
parseString(data, function (e, r) {
var apis = r.rsd.service[0].apis;
for (var i = 0; i < apis.length; i++) {
var api = apis[i].api[i]['$'];
if (api.name === "Twitter") {
apiUrl = api.apiLink;
}
}
});
}, "text").done(function () {
getServerConfig();
});*/
function getServerConfig() {
$.getJSON(apiUrl + 'statusnet/config.json', function (data) {
var site = data.site;
GSInstance.fancyLink = site.fancy;
GSInstance.language = site.language;
GSInstance.email = site.email;
GSInstance.closed = site.closed;
GSInstance.inviteOnly = site.inviteonly;
GSInstance.private = site.private;
GSInstance.textLimit = site.textlimit;
GSInstance.ssl = site.ssl;
}).done(function () {
allowed = true;
});
}
function loadPublicTimeline() {
var notices = document.querySelector('#notices');
while (notices.firstChild) {
notices.removeChild(notices.firstChild);
}
var tabs = document.querySelector('.tabs');
tabs.children[0].id = '';
tabs.children[1].id = 'selected';
tabs.children[2].id = '';
$.getJSON(apiUrl + 'statuses/public_timeline.json', function (data) {
for (var i = 0; i < data.length; i++) {
var user = data[i].user;
var tweet = document.createElement('div');
tweet.id = 'tweet';
tweet.className = 'tweet';
var author = document.createElement('span');
author.id = 'author';
author.className = 'noticeAuthor';
var authorLink = document.createElement('a');
authorLink.text = user.name + ' @' + user.screen_name;
authorLink.href = user['statusnet:profile_url'];
authorLink.onclick = function () {
window.open(authorLink.href, authorLink.text);
};
author.appendChild(authorLink);
var avatarImage = document.createElement('img');
avatarImage.height = 48;
avatarImage.width = 48;
avatarImage.src = user.profile_image_url_https;
avatarImage.align = 'left';
avatarImage.className = 'authorAvatar';
var notice = document.createElement('p');
notice.id = 'notice';
notice.innerHTML = data[i].statusnet_html;
notice.className = 'notice';
tweet.appendChild(author);
tweet.appendChild(avatarImage);
tweet.appendChild(notice);
document.getElementById('notices').appendChild(tweet);
document.getElementById('notices').appendChild(document.createElement('br'));
}
});
}
function loadHomeTimeline() {
var notices = document.querySelector('#notices');
while (notices.firstChild) {
notices.removeChild(notices.firstChild);
}
var tabs = document.querySelector('.tabs');
tabs.children[0].id = 'selected';
tabs.children[1].id = '';
tabs.children[2].id = '';
}
\ No newline at end of file
......@@ -5,12 +5,17 @@ const electron = require('electron');
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const ipcMain = electron.ipcMain;
const ElectronSettings = require('electron-settings');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let settings = new ElectronSettings();
function createWindow () {
function createWindow() {
//var settingsPath = settings.getConfigFilePath();
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
......@@ -19,6 +24,13 @@ function createWindow () {
// Open the DevTools.
mainWindow.webContents.openDevTools();
ipcMain.on('did-finish-load', function() {
if (isEmpty(settings.get('token')))
mainWindow.webContents.send('needs-auth');
else
mainWindow.webContents.send('token', settings.get('token'));
});
// Emitted when the window is closed.
mainWindow.on('closed', function() {
......@@ -29,9 +41,21 @@ function createWindow () {
});
}
ipcMain.on('verifier-ping', function(event, data) {
if (mainWindow !== null)
mainWindow.webContents.send('verifier-pong', data);
});
ipcMain.on('save-token', function(event, data) {
settings.set('token', data);
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
app.on('ready', function() {
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
......@@ -49,3 +73,26 @@ app.on('activate', function () {
createWindow();
}
});
// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;
function isEmpty(obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
\ No newline at end of file
......@@ -4,13 +4,17 @@
"description": "GNUsocial client written in Javascript",
"main": "main.js",
"scripts": {
"start": "electron main.js"
"start": "electron ."
},
"author": "Alexei Ivanov",
"license": "GPL-3.0",
"devDependencies": {
"electron-prebuilt": "^0.36.7",
"electron-settings": "^1.0.1",
"jquery": "^2.2.0",
"oauth": "^0.9.14",
"xml2js": "^0.4.16"
},
"dependencies": {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment