2017-08-03 16:32:30 +00:00
|
|
|
securePassword = null
|
|
|
|
|
|
|
|
createSecret = () ->
|
|
|
|
secret = $('#formCreateSecret').find('textarea').val()
|
|
|
|
|
2017-08-04 13:17:30 +00:00
|
|
|
securePassword = Math.random().toString(36).substring(2)
|
|
|
|
secret = GibberishAES.enc(secret, securePassword)
|
2017-08-03 16:32:30 +00:00
|
|
|
|
|
|
|
$.ajax 'api/create',
|
|
|
|
data:
|
|
|
|
secret: secret
|
|
|
|
dataType: "json"
|
|
|
|
statusCode:
|
|
|
|
201: secretCreated
|
|
|
|
400: somethingWrong
|
|
|
|
500: somethingWrong
|
|
|
|
404: () ->
|
|
|
|
# Mock for interface testing
|
|
|
|
secretCreated
|
|
|
|
secret_id: 'foobar'
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
dataNotFound = () ->
|
|
|
|
$('#notfound').show()
|
|
|
|
|
|
|
|
hashLoad = () ->
|
|
|
|
hash = window.location.hash
|
|
|
|
if hash.length == 0
|
|
|
|
return
|
|
|
|
|
2017-08-03 19:43:37 +00:00
|
|
|
hash = decodeURIComponent(hash)
|
|
|
|
|
2017-08-03 16:32:30 +00:00
|
|
|
parts = hash.split '|'
|
|
|
|
if parts.length == 2
|
|
|
|
hash = parts[0]
|
|
|
|
securePassword = parts[1]
|
|
|
|
|
|
|
|
id = hash.substring(1)
|
|
|
|
$.ajax "api/get/#{id}",
|
|
|
|
dataType: "json"
|
|
|
|
statusCode:
|
|
|
|
404: dataNotFound
|
|
|
|
200: showData
|
|
|
|
|
|
|
|
initBinds = () ->
|
|
|
|
$('#formCreateSecret').bind 'submit', createSecret
|
2017-08-03 18:01:22 +00:00
|
|
|
$('#newSecret, .navbar-brand').bind 'click', newSecret
|
2017-08-03 17:44:13 +00:00
|
|
|
$(window).bind 'hashchange', hashLoad
|
2017-08-03 16:32:30 +00:00
|
|
|
|
|
|
|
newSecret = () ->
|
|
|
|
location.href = location.href.split('#')[0]
|
|
|
|
false
|
|
|
|
|
|
|
|
secretCreated = (data) ->
|
|
|
|
secretHash = data.secret_id
|
|
|
|
if securePassword != null
|
|
|
|
secretHash = "#{secretHash}|#{securePassword}"
|
|
|
|
url = "#{location.href.split('#')[0]}##{secretHash}"
|
|
|
|
|
|
|
|
$('#panelNewSecret').hide()
|
|
|
|
$('#panelSecretURL').show()
|
|
|
|
$('#panelSecretURL').find('input').val url
|
|
|
|
$('#panelSecretURL').find('input').focus()
|
|
|
|
$('#panelSecretURL').find('input').select()
|
|
|
|
|
2017-08-03 18:01:22 +00:00
|
|
|
securePassword = null
|
|
|
|
|
2017-08-03 16:32:30 +00:00
|
|
|
showData = (data) ->
|
|
|
|
secret = data.secret
|
|
|
|
if securePassword != null
|
|
|
|
secret = GibberishAES.dec(secret, securePassword)
|
|
|
|
|
|
|
|
$('#panelNewSecret').hide()
|
2017-08-03 17:44:13 +00:00
|
|
|
$('#panelSecretURL').hide()
|
2017-08-03 18:01:22 +00:00
|
|
|
$('#notfound').hide()
|
|
|
|
$('#somethingwrong').hide()
|
2017-08-03 16:32:30 +00:00
|
|
|
$('#panelReadSecret').show()
|
|
|
|
$('#panelReadSecret').find('textarea').val secret
|
|
|
|
|
|
|
|
somethingWrong = () ->
|
|
|
|
$('#somethingwrong').show()
|
|
|
|
|
|
|
|
|
|
|
|
$ ->
|
|
|
|
initBinds()
|
|
|
|
hashLoad()
|