Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
- Basic Chrome extension
- Test website
  • Loading branch information
andrew-tpfc committed Jun 17, 2014
1 parent a6bd06e commit 93e5bfd
Show file tree
Hide file tree
Showing 40 changed files with 2,097 additions and 0 deletions.
171 changes: 171 additions & 0 deletions chrome_basic/pgid_chrome/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
"use strict";

chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
if (change.status === 'complete') {
chrome.tabs.sendMessage(tabId, { tabId: tabId, cmd: 'init' }, onTabUpdated);
}
});

chrome.tabs.onActivated.addListener(function(info) {
chrome.tabs.sendMessage(info.tabId, { tabId: info.tabId, cmd: 'init' }, onTabUpdated);
});

function onTabUpdated(msg) {
// Decide whether we want to show page action icon when tab is activated or updated based on declarations in HTML content
if (msg !== undefined && (msg.register !== undefined || msg.login !== undefined || msg.update !== undefined || msg.updatepw !== undefined)) {
chrome.pageAction.show(msg.tabId);
}
}

var idlist = {};

chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
switch(msg.cmd) {
case 'register':
case 'login':
doRegisterOrLogin(msg.cmd, msg.payload);
break;
case 'update':
doUpdate(msg.cmd, msg.payload);
break;
case 'updatepw':
doUpdatePw(msg.cmd, msg.payload);
break;
}
});

function processID(keytext) {
// Extract PGID header
var keytext = keytext;
var header = keytext.match(/(-----BEGIN PGID HEADER-----(.|[\r|\n])*-----END PGID HEADER-----)/gm);
if (header === null) {
alert('Invalid format: Cannot find header block');
return false;
}
// Extract private key
var seckey = keytext.match(/(-----BEGIN RSA PRIVATE KEY-----(.|[\r|\n])*-----END RSA PRIVATE KEY-----)/gm);
if (seckey === null) {
alert('Invalid format: Cannot find private key block');
return false;
}
// Extract public key
var pubkey = keytext.match(/(-----BEGIN PUBLIC KEY-----(.|[\r|\n])*-----END PUBLIC KEY-----)/gm);
if (pubkey === null) {
alert('Invalid format: Cannot find public key block');
return false;
}
var pubkeytext = header[0] + '\n' + pubkey[0];
return { header: header[0], seckey: seckey[0], pubkey: pubkey[0], pubkeytext: pubkeytext };
}

function doRegisterOrLogin(cmd, msg) {
var pgid = processID(idlist[msg.uid].keytext);
if (pgid === false) return;

// Get encrypted token (challenge) from server
$.ajax({
url: cmd == 'register' ? msg.register : msg.login,
type: 'POST',
data: { cmd: cmd, pgid: pgid.pubkeytext },
dataType: 'json',
cache: false,
async: false
})
.done(function(reply) {
console.log(JSON.stringify(reply));
if (reply && reply.error) {
alert('Error: ' + reply.error);
} else {
// Decrypt token
var crypto = new JSEncrypt();
crypto.setPrivateKey(pgid.seckey);
var token = crypto.decrypt(reply.token);

// Pass decrypted token (response) to content script to submit to server
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { tabId: tabs[0].id, cmd: cmd, pgid: pgid.pubkeytext, token: token });
});
}
})
.fail(function(jqXHR, textStatus) {
alert('Error: ' + textStatus);
});
}

function doUpdate(cmd, msg) {
var opgid = processID(idlist[msg.ouid].keytext);
var npgid = processID(idlist[msg.nuid].keytext);
if (opgid === false || npgid == false) return;

// Get encrypted token (challenge) from server
$.ajax({
url: msg.update,
type: 'POST',
data: { cmd: cmd, opgid: opgid.pubkeytext, npgid: npgid.pubkeytext },
dataType: 'json',
cache: false,
async: false
})
.done(function(reply) {
if (reply && reply.error) {
alert('Error: ' + reply.error);
} else {
var crypto = new JSEncrypt();

// Decrypt otoken
crypto.setPrivateKey(opgid.seckey);
var otoken = crypto.decrypt(reply.otoken);

// Decrypt ntoken
crypto.setPrivateKey(npgid.seckey);
var ntoken = crypto.decrypt(reply.ntoken);

// Pass both decrypted tokens (response) to content script to submit to server
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
tabId: tabs[0].id,
cmd: cmd,
opgid: opgid.pubkeytext,
npgid: npgid.pubkeytext,
otoken: otoken,
ntoken: ntoken });
});
}
})
.fail(function(jqXHR, textStatus) {
alert('Error: ' + textStatus);
});
}

function doUpdatePw(cmd, msg) {
var pgid = processID(idlist[msg.uid].keytext);
if (pgid === false) return;

// Get encrypted token (challenge) from server
$.ajax({
url: msg.updatepw,
type: 'POST',
data: { cmd: cmd, password: msg.password, npgid: pgid.pubkeytext },
dataType: 'json',
cache: false,
async: false
})
.done(function(reply) {
if (reply && reply.error) {
alert('Error: ' + reply.error);
} else {
// Decrypt token
var crypto = new JSEncrypt();
crypto.setPrivateKey(pgid.seckey);
var token = crypto.decrypt(reply.token);

// Pass decrypted token (response) to content script to submit to server
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { tabId: tabs[0].id, cmd: cmd, password: msg.password, npgid: pgid.pubkeytext, token: token });
});
}
})
.fail(function(jqXHR, textStatus) {
alert('Error: ' + textStatus);
});
}
51 changes: 51 additions & 0 deletions chrome_basic/pgid_chrome/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.cmd) {
switch(msg.cmd) {
case 'init':
sendResponse({
tabId : msg.tabId,
register : $('link[rel=pgid-register]').eq(0).attr('href'),
login : $('link[rel=pgid-login]').eq(0).attr('href'),
update : $('link[rel=pgid-update]').eq(0).attr('href'),
updatepw : $('link[rel=pgid-updatepw]').eq(0).attr('href')
});
break;
case 'login':
case 'register':
var linkname = 'link[rel=pgid-' + msg.cmd + ']';
var formhtml = '<form id="pgidform" method="post" action="' + $(linkname).eq(0).attr('href') + '">';
formhtml += '<input type="hidden" name="cmd" value="' + msg.cmd + '" />';
formhtml += '<input type="hidden" name="pgid" value="' + msg.pgid + '" />';
formhtml += '<input type="hidden" name="token" value="' + msg.token + '" />';
formhtml += '</form>';
document.body.innerHTML += formhtml;
$('#pgidform').submit();
break;
case 'update':
var linkname = 'link[rel=pgid-' + msg.cmd + ']';
var formhtml = '<form id="pgidform" method="post" action="' + $(linkname).eq(0).attr('href') + '">';
formhtml += '<input type="hidden" name="cmd" value="' + msg.cmd + '" />';
formhtml += '<input type="hidden" name="opgid" value="' + msg.opgid + '" />';
formhtml += '<input type="hidden" name="npgid" value="' + msg.npgid + '" />';
formhtml += '<input type="hidden" name="otoken" value="' + msg.otoken + '" />';
formhtml += '<input type="hidden" name="ntoken" value="' + msg.ntoken + '" />';
formhtml += '</form>';
document.body.innerHTML += formhtml;
$('#pgidform').submit();
break;
case 'updatepw':
var linkname = 'link[rel=pgid-' + msg.cmd + ']';
var formhtml = '<form id="pgidform" method="post" action="' + $(linkname).eq(0).attr('href') + '">';
formhtml += '<input type="hidden" name="cmd" value="' + msg.cmd + '" />';
formhtml += '<input type="hidden" name="password" value="' + msg.password + '" />';
formhtml += '<input type="hidden" name="npgid" value="' + msg.npgid + '" />';
formhtml += '<input type="hidden" name="token" value="' + msg.token + '" />';
formhtml += '</form>';
document.body.innerHTML += formhtml;
$('#pgidform').submit();
break;
}
}
});
Binary file added chrome_basic/pgid_chrome/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chrome_basic/pgid_chrome/id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions chrome_basic/pgid_chrome/jquery-2.1.1.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions chrome_basic/pgid_chrome/jsencrypt.min.js

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions chrome_basic/pgid_chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"manifest_version" : 2,
"name" : "Pretty Good ID",
"description" : "Pretty Good ID - Chrome Extension - Basic",
"version" : "0.1",

"background" : {
"scripts" : [ "jquery-2.1.1.min.js", "jsencrypt.min.js", "background.js" ],
"persistent" : false
},

"page_action" : {
"default_title" : "Pretty Good ID",
"default_icon" : "icon.png",
"default_popup" : "popup.html"
},

"content_scripts" : [ {
"matches" : [ "http://*/*" ],
"js" : [ "jquery-2.1.1.min.js", "content.js" ]
} ],

"options_page" : "options.html"
}
66 changes: 66 additions & 0 deletions chrome_basic/pgid_chrome/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<head>
<title>Pretty Good ID</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="content-container">
<div id="content">
<div>
<label for="desc"><b>Description</b></label><br />
<small>This field is optional. It lets you identify the PGID by giving it a meaningful label.</small><br />
<input id="desc" size="64" type="text" />
</div><br/>
<div>
<label for="fullname"><b>Full name</b></label><br />
<small>This field is optional. It helps to prefill the form during registration for websites that require this information.</small><br />
<input id="fullname" size="64" type="text" />
</div><br/>
<div>
<label for="shortname"><b>Short name (or nickname)</b></label><br />
<small>This field is optional. It helps to prefill the form during registration for websites that supports a nickname or alias, such as online forums.</small><br />
<input id="shortname" size="64" type="text" />
</div><br />
<div>
<label for="email"><b>Email address</b></label><br />
<small>This field is optional. Many websites use the email address as a means of unique identification and communication. Having this information helps to prefill the form during registration.</small><br />
<input id="email" size="64" type="text" />
</div><br />
<div>
<button id="generate">Generate new PGID</button>
<span id="progress" class="progress" />
</div><br />
<div>
<input id="fileloader" type="file" name="qrfile" accept="image/*" style="display:none" />
<button id="loadfile">Open QR-Code image file</button>
</div>
</div>
<div id="aside" style="display:none">
<b>Key Text</b><br />
<p><textarea id="keytext" class="keytext" rows="8" cols="65" readonly="true"></textarea></p>
<b>Encoded as QR-Code</b>
</br /></br />
<div id="card"><table class="card">
<tr>
<td align="center">
<img src="id.png" />
<p><small><b><span id="displayname"></span></b></small></p>
</td>
<td align="center">
<img id="qrcode" width="320" height="320" />
<small><i>(Right-click to save this QR-Code)</i></small>
</td>
</tr>
</table></div>
</div>
</div>
<canvas id="canvas" width="10" height="10" style="display:none" />
<script src="jquery-2.1.1.min.js"></script>
<script src="jsencrypt.min.js"></script> <!-- https://github.com/travist/jsencrypt -->
<script src="qrencoder.min.js"></script> <!-- https://bitbucket.org/lifthrasiir/qrjs/ -->
<script src="qrdecoder.min.js"></script> <!-- http://qrlogo.kaarposoft.dk/ -->
<script src="sha1sum.min.js"></script> <!-- http://www.movable-type.co.uk/scripts/sha1.html -->
<script src="options.js"></script> <!-- https://github.com/eligrey/FileSaver.js/ -->
</body>
</html>
Loading

0 comments on commit 93e5bfd

Please sign in to comment.