focus the master on first oepn
[skeletonkey.git] / core.js
1 /* Copyright (c) 2012 Robert Sesek <http://robert.sesek.com>
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 */
21
22 (function main() {
23 if (typeof chrome !== 'undefined') {
24 // TODO: load the extension JS
25 } else {
26 // TODO: load the hosted JS
27 }
28
29 document.addEventListener('DOMContentLoaded', function() {
30 var controller = new SkeletonKey(document);
31 });
32 })();
33
34 /**
35 * SkeletonKey is view controller for generating secure passwords.
36 *
37 * @param {HTMLDocument} doc The document on which to operate.
38 */
39 var SkeletonKey = SkeletonKey || function(doc) {
40 this._master = doc.getElementById('master');
41 this._sitekey = doc.getElementById('sitekey');
42 this._username = doc.getElementById('username');
43 this._password = doc.getElementById('password');
44 this._generateButton = doc.getElementById('generate');
45 this._init();
46 };
47
48 /**
49 * The number of iterations to perform in PBKDF2.
50 * @const {int}
51 */
52 SkeletonKey.prototype.ITERATIONS = 1000;
53 /**
54 * The size of the key, in bytes.
55 * @const {int}
56 */
57 SkeletonKey.prototype.KEYSIZE = 256/32;
58
59 /**
60 * The minimum length of a password.
61 * @const {int}
62 */
63 SkeletonKey.prototype.MIN_LENGTH = 6;
64
65 /**
66 * Initializes event handlers for the page.
67 * @private
68 */
69 SkeletonKey.prototype._init = function() {
70 this._generateButton.onclick = this._onGenerate.bind(this);
71
72 this._master.onkeyup = this._nextFieldInterceptor.bind(this);
73 this._sitekey.onkeyup = this._nextFieldInterceptor.bind(this);
74 this._username.onkeyup = this._nextFieldInterceptor.bind(this);
75
76 this._password.onclick = this._selectPassword.bind(this);
77 this._password.labels[0].onclick = this._selectPassword.bind(this);
78
79 this._initChromeExtension();
80
81 this._master.focus();
82 };
83
84 /**
85 * Event handler for generating a new password.
86 * @param {Event} e
87 * @private
88 */
89 SkeletonKey.prototype._onGenerate = function(e) {
90 var salt = this._username.value + '@' + this._sitekey.value;
91
92 // |key| is a WordArray of 32-bit words.
93 var key = CryptoJS.PBKDF2(this._master.value, salt,
94 {keySize: this.KEYSIZE, iterations: this.ITERATIONS});
95 var hexString = key.toString();
96 hexString = this._capitalizeKey(hexString);
97 this._password.value = hexString;
98 this._selectPassword();
99 };
100
101 /**
102 * Takes a HEX string and returns a mixed-case string.
103 * @param {string} key
104 * @return string
105 * @private
106 */
107 SkeletonKey.prototype._capitalizeKey = function(key) {
108 // |key| is too long for a decent password, so try and use the second half of
109 // it as the basis for capitalizing the key.
110 var capsSource = null;
111 var keyLength = key.length;
112 if (keyLength / 2 <= this.MIN_LENGTH) {
113 capsSouce = key.substr(0, keyLength - this.MIN_LENGTH);
114 } else {
115 capsSource = key.substr(keyLength / 2);
116 }
117
118 if (!capsSource || capsSource.length < 1) {
119 return key;
120 }
121
122 key = key.substr(0, capsSource.length);
123 var capsSourceLength = capsSource.length;
124
125 var j = 0;
126 var newKey = "";
127 for (var i = 0; i < key.length; i++) {
128 var c = key.charCodeAt(i);
129 // If this is not a lowercase letter or there's no more source, skip.
130 if (c < 0x61 || c > 0x7A || j >= capsSourceLength) {
131 newKey += key[i];
132 continue;
133 }
134
135 var makeCap = capsSource.charCodeAt(j++) % 2;
136 if (makeCap)
137 newKey += String.fromCharCode(c - 0x20);
138 else
139 newKey += key[i];
140 }
141
142 return newKey;
143 };
144
145 /**
146 * Checks if the given key event is from the enter key and moves onto the next
147 * field or generates the password.
148 * @param {Event} e
149 * @private
150 */
151 SkeletonKey.prototype._nextFieldInterceptor = function(e) {
152 if (e.keyCode != 0xD)
153 return;
154
155 if (this._master.value == "") {
156 this._master.focus();
157 } else if (this._sitekey.value == "") {
158 this._sitekey.focus();
159 } else if (this._username.value == "") {
160 this._username.focus();
161 } else {
162 this._generateButton.click();
163 }
164 };
165
166 /**
167 * Selects the contents of the generated password.
168 * @private
169 */
170 SkeletonKey.prototype._selectPassword = function() {
171 this._password.focus();
172 this._password.select();
173 };
174
175 /**
176 * Initalizes the Chrome extension pieces if running inside chrome.
177 * @private
178 */
179 SkeletonKey.prototype._initChromeExtension = function() {
180 return;
181 if (typeof chrome == 'undefined' || typeof chrome.extension == 'undefined')
182 return;
183
184 // getCurrent is undefined for backround pages. Need content script.
185 chrome.tabs.getCurrent(function (tab) {
186 if (tab == null)
187 return;
188
189 var url = tab.url;
190 if (url == null || url == "")
191 return;
192
193 var siteKey = url.search(/https?:\/\/(www.?|login|accounts?)\.(.*)\.(com?|net|org|edu|biz|info)?.*/);
194 console.log(siteKey);
195 });
196 };