diff --git a/countdown.js b/countdown.js index 3ee3748..f3c9d2d 100644 --- a/countdown.js +++ b/countdown.js @@ -1,11 +1,113 @@ -var countdown = $("#countdown").countdown360({ - radius: 30, - seconds: 30, - fontColor: '#000', - autostart: false, - onComplete: function() { - console.log('done') +(function (exports) { + 'use strict'; + + $('body').addClass('in'); + + var Authenticator = exports.Authenticator; + + function parseQuery(search) { + + var args = search.substring(1).split('&'); + + var argsParsed = {}; + + var i, arg, kvp, key, value; + + for (i=0; i < args.length; i++) { + + arg = args[i]; + + if (-1 === arg.indexOf('=')) { + + argsParsed[decodeURIComponent(arg).trim()] = true; + } + else { + + kvp = arg.split('='); + + key = decodeURIComponent(kvp[0]).trim(); + + value = decodeURIComponent(kvp[1]).trim(); + + argsParsed[key] = value; + } + } + + return argsParsed; } -}); -countdown.start(); -console.log('countdown360 ', countdown); \ No newline at end of file + + /* + function parseQuery(search) { + var args = search.substring(1).split('&'); + var argsParsed = {}; + var i; + + console.log('parse args', args); + for (i = 0; i < args.length; i++) { + var arg = args[i]; + + if (-1 === arg.indexOf('=')) { + argsParsed[decodeURIComponent(arg).trim()] = true; + } + else { + var kvp = arg.split('='); + argsParsed[decodeURIComponent(kvp[0]).trim()] = decodeURIComponent(kvp[1]).trim(); + } + } + + return argsParsed; + } + */ + + function run() { + var countdown = $(".js-countdown").countdown360({ + radius: 30, + seconds: 30, + fontColor: '#000', + autostart: false, + onComplete: function() { + console.log('done'); + run(); + } + }); + + // TODO change to token start time, regardless of the time the app began + countdown.start(new Date()); + console.log('countdown360 ', countdown); + console.log(document.location.search); + + var otpauth = parseQuery(document.location.search).otpuri; + var otplink = document.createElement('a'); + var otp; + var meta; + var issuer; + var accountName; + + otplink.href = otpauth; + otp = parseQuery(otplink.search); + + meta = otplink.pathname.replace(/.*\/totp\//, '').split(':'); + // TODO throw if otp.issuer !== decodeURI(meta[0]) + if (meta.length > 1) { + issuer = otp.issuer || decodeURI(meta[0]); + accountName = decodeURI(meta[1]); + } + else { + issuer = otp.issuer; + accountName = decodeURI(meta[0]); + } + + console.log('otpuri', otpauth); + console.log('otplink', otplink); + console.log('otplink.search', otplink.search); + console.log('otp', otp); + + $('.js-issuer').text(issuer); + $('.js-account-name').text(accountName); + Authenticator.generateToken(otp.secret).then(function (token) { + $('.js-token').text(token.replace(/(\d{3})/g, '$1 ').trim()); + }); + } + + run(); +}(window)); diff --git a/jquery.countdown.js b/jquery.countdown.js index f5b9b72..4847e7d 100644 --- a/jquery.countdown.js +++ b/jquery.countdown.js @@ -29,7 +29,7 @@ Plugin.prototype = { getTimeRemaining: function() { - + var timeRemaining = this._secondsLeft(this.getElapsedTime()); return timeRemaining; }, @@ -55,15 +55,16 @@ } }, - start: function () { - this.startedAt = new Date(); + start: function (date) { + this.updateInterval = 25; + this.startedAt = date || new Date(); this._drawCountdownShape(Math.PI*3.5, true); this._drawCountdownLabel(0); - this.interval = setInterval(jQuery.proxy(this._draw, this), 1000); + this._interval = setInterval(jQuery.proxy(this._draw, this), this.updateInterval); }, stop: function (cb) { - clearInterval(this.interval); + clearInterval(this._interval); if (cb) { cb(); } }, @@ -135,8 +136,14 @@ }, _draw: function () { - var secondsElapsed = Math.round((new Date().getTime() - this.startedAt.getTime())/1000), - endAngle = (Math.PI*3.5) - (((Math.PI*2)/this.settings.seconds) * secondsElapsed); + var secondsElapsed = Math.round((new Date().getTime() - this.startedAt.getTime())/1000); + var milisecondsElapsed = Math.round((Date.now() - this.startedAt.getTime())); + var whole = (Math.PI*2)/(this.settings.seconds * 1000); + var parts = milisecondsElapsed; + var endAngle = (Math.PI*3.5) + - ( (whole) * parts); + //console.log('endAngle', endAngle); + this._clearRect(); this._drawCountdownShape(Math.PI*3.5, false); if (secondsElapsed < this.settings.seconds) { diff --git a/phone.html b/phone.html index 1248d96..becaaa1 100644 --- a/phone.html +++ b/phone.html @@ -1,11 +1,52 @@ - - - -
otpauth://totp/company:user?secret=xxxx
+otpauth://totp/company:user?secret=xxxx&issuer=company