aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app.coffee103
-rw-r--r--public/js/calculator.coffee88
-rw-r--r--views/index.html5
-rw-r--r--views/login.html26
4 files changed, 157 insertions, 65 deletions
diff --git a/app.coffee b/app.coffee
index decf269a..f60fdd25 100644
--- a/app.coffee
+++ b/app.coffee
@@ -3,15 +3,38 @@ http = require('http')
path = require('path')
engines = require('consolidate')
passport = require('passport')
+bcrypt = require('bcrypt')
config = require('./config')
app = express()
+LocalStrategy = require('passport-local').Strategy
+passport.use(new LocalStrategy(
+ (username, password, done) ->
+ user = {username: 'soltysa', password: 'adam'}
+ return done(null, user)
+))
+
+passport.serializeUser((user, done) ->
+ done(null, user.username)
+)
+
+passport.deserializeUser((id, done) ->
+ user = {username: 'soltysa', password: 'adam'}
+ done(null, user)
+)
+
app.engine('html', engines.hogan)
+app.enable('trust proxy')
app.set('view engine', 'html')
app.set('views', __dirname + '/views')
app.use(express.static(__dirname + '/public'))
app.use(require('connect-assets')(src: 'public'))
-app.use(express.bodyParser());
+app.use(express.bodyParser())
+app.use(express.cookieParser())
+app.use(express.session(secret: 'weareallmadeofstars'))
+app.use(passport.initialize())
+app.use(passport.session())
+app.use(app.router)
app.use((err, req, res, next) ->
res.status(500)
console.log(err)
@@ -22,8 +45,19 @@ app.get('/', (req, res) ->
res.render('index', js: (-> global.js), css: (-> global.css))
)
+app.get('/bcrypt', (req, res) ->
+ salt = "$2a$10$3R8tUtIoU/fllQD9zEgTZe"
+ hash = bcrypt.hashSync("chelsea", salt)
+ res.write(salt + "\n")
+ res.write(hash)
+ res.end()
+)
+
app.get('/:user/report', (req, res) ->
- res.render('report', user: req.params.user, js: (-> global.js), css: (-> global.css))
+ res.render('report',
+ user: req.params.user,
+ js: (-> global.js),
+ css: (-> global.css))
)
app.get('/:user.json', (req, res) ->
@@ -54,44 +88,63 @@ app.get('/:user/transactions', (req, res) ->
)
)
-
app.get('/ticker', (req, res) ->
- t = setTimeout(->
- res.writeHead(500, {'Content-Type': 'text/plain'})
- res.end()
- , 2000)
-
options =
host: 'bitcoincharts.com',
- path: '/t/depthcalc.json?symbol=' + req.query.symbol + '&type=bid&amount=1000&currency=true'
+ path: '/t/depthcalc.json?symbol=' +
+ req.query.symbol +
+ '&type=bid&amount=1000&currency=true'
require('http').get(options, (r) ->
r.setEncoding('utf-8')
r.on('data', (chunk) ->
- clearTimeout(t)
- res.send(chunk)
+ res.writeHead(200,
+ 'Content-Length': chunk.length,
+ 'Content-Type': 'text/plain')
+ res.write(chunk)
res.end()
)
)
)
-app.post('/login',
- passport.authenticate('local'),
- (req, res) ->
- res.redirect('/users/' + req.user.username)
+app.get('/login', (req, res) ->
+ res.render('login',
+ js: (-> global.js),
+ css: (-> global.css)
+ )
+)
+
+app.post('/login', (req, res, next) ->
+ passport.authenticate('local', (err, user, info) ->
+ if (err)
+ return next(err)
+ if (!user)
+ return res.redirect('/login')
+ req.logIn(user, (err) ->
+ if (err)
+ return next(err)
+ return res.redirect('/' + user.username)
+ )
+ )(req, res, next)
)
app.post('/users', (req, res) ->
if req.body.login
- db = require("redis").createClient()
- db.exists(req.body.login, (err, obj) ->
- if obj
- res.redirect(req.body.login)
- else
- db.hmset(req.body.login, req.body, ->
- res.redirect(req.body.login)
+ if req.body.password
+ bcrypt.hash(req.body.password, 12, (err, hash) ->
+ db = require("redis").createClient()
+ db.hget(req.body.login, 'password', (err, password) ->
+ bcrypt.compare(req.body.password, password, (err, match) ->
+ if match
+ req.body.password = password
+ db.hmset(req.body.login, req.body, ->
+ res.redirect(req.body.login)
+ )
+ else
+ res.redirect(req.body.login)
+ )
)
- )
+ )
else
params = []
for k,v of req.body
@@ -137,9 +190,5 @@ app.get('/:user', (req, res) ->
)
)
-app.use((err, req, res, next) ->
- console.log(err)
-)
-
app.listen(3000)
diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee
index 7e01e504..e589bedd 100644
--- a/public/js/calculator.coffee
+++ b/public/js/calculator.coffee
@@ -8,6 +8,7 @@
#= require check_address.js
g = exports ? this
+
EXCHANGE_FAIL = "Error fetching exchange rate"
SOCKET_FAIL = "Error connecting to payment server"
ADDRESS_FAIL = "Invalid address"
@@ -18,23 +19,28 @@ $(->
g.name = get('name')
g.address = get('address')
g.symbol = get('symbol')
- g.commission = get('commission')
+ g.commission = parseFloat(get('commission'))
g.logo = get('logo')
g.exchange = 0
+ g.errors = []
setupPage()
setupQR()
setupSocket()
if user? and user
- $.getJSON(user + '.json', (data) ->
- return unless data?
- g.name = data.name
- g.address = data.address
- g.symbol = data.symbol
- g.commission = data.commission
- g.logo = data.logo
- setupPage()
+ $.ajax(
+ url: user + '.json',
+ cache: false,
+ dataType: 'json',
+ success: (data) ->
+ return unless data?
+ g.name = data.name
+ g.address = data.address
+ g.symbol = data.symbol
+ g.commission = data.commission
+ g.logo = data.logo
+ setupPage()
)
$('#amount').keyup(updateTotal)
@@ -58,42 +64,45 @@ updateTotal = ->
$('#total').html(total.toString())
displayQR('bitcoin:' + g.address + '?amount=' + total.toString())
-exchangeFail = ->
- $('#error').show().html(EXCHANGE_FAIL)
- $('#calculator').hide()
-
fetchExchangeRate = ->
- $.getJSON('ticker?symbol=' + g.symbol)
- .success((data) ->
- unless data?
- exchangeFail()
+ $.ajax(
+ url: 'ticker?symbol=' + g.symbol,
+ dataType: 'json',
+ cache: false,
+ success: (data) ->
+ if data?
+ clear(EXCHANGE_FAIL)
+ else
+ fail(EXCHANGE_FAIL)
return
exchange = 1000 / data.out
- exchange = exchange - exchange * commission * 0.01
+ exchange = exchange - exchange * g.commission * 0.01
g.exchange = Math.ceil(exchange * 100) / 100
$('#exchange').val(g.exchange.toFixed(2))
updateTotal()
+ error: -> fail(EXCHANGE_FAIL)
+ )
- clear(EXCHANGE_FAIL)
-
- if $('#error').html() is ""
- $('#error').hide()
- $('#calculator').fadeIn('slow')
- ).error(exchangeFail)
setTimeout(fetchExchangeRate, 900000)
-fail = (err) ->
+fail = (msg) ->
+ g.errors.push(msg)
$('#calculator').hide()
- $('#error').html(err).show()
+ $('#error').show().html(g.errors.toString())
clear = (msg) ->
- if $('#error').html() is msg
- $('#error').html("")
+ i = g.errors.indexOf(msg)
+ g.errors.splice(i, 1) if i >= 0
+ if (g.errors.length > 0)
+ $('#error').show().html(g.errors.toString())
+ else
+ $('#error').hide()
+ $('#calculator').fadeIn('slow')
setupPage = ->
g.address or= '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr'
- g.commission or= 3
+ g.commission or= 0
g.symbol or= 'virtexCAD'
if g.logo
@@ -108,11 +117,15 @@ setupPage = ->
else
fail(ADDRESS_FAIL)
- $('#symbol').html(g.symbol + " - " + commission.toFixed(0) + "%")
+ symbol = g.symbol
+ symbol += " - #{g.commission.toFixed(0)}%" if g.commission > 0
+
+ $('#symbol').html(symbol)
$('#currency').html(g.symbol.slice(-3))
$('#received').hide()
- fetchExchangeRate()
+ if g.exchange is 0
+ fetchExchangeRate()
setupSocket = ->
setTimeout(setupSocket, 10000)
@@ -148,12 +161,13 @@ setupSocket = ->
$('#payment').hide()
$('#received').fadeIn('slow')
- $.post("/#{g.user}/transactions",
- address: from_address,
- date: moment().format("YYYY-MM-DD HH:mm:ss"),
- received: received,
- exchange: g.exchange
- )
+ if g.user
+ $.post("/#{g.user}/transactions",
+ address: from_address,
+ date: moment().format("YYYY-MM-DD HH:mm:ss"),
+ received: received,
+ exchange: g.exchange
+ )
get = (name) ->
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
diff --git a/views/index.html b/views/index.html
index 9e97b16b..fa2cd0c3 100644
--- a/views/index.html
+++ b/views/index.html
@@ -13,9 +13,12 @@
<form action="users" method="post">
<h1>Calculator Setup</h1>
- <label for="login">Alias:</label>
+ <label for="login">Username:</label>
<input type="text" id="login" name="login" value="{{user}}" />
+ <label for="password">Password:</label>
+ <input type="password" id="password" name="password" value="" />
+
<label for="name">Title:</label>
<input type="text" id="name" name="name" />
diff --git a/views/login.html b/views/login.html
new file mode 100644
index 00000000..f451db39
--- /dev/null
+++ b/views/login.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <meta name="viewport" content="width=480, height=752, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
+ <title>Bitcoin Payment Calculator</title>
+ <link href="img/favicon.ico" rel="shortcut icon" />
+ {{#css}}common{{/css}}
+ </head>
+ <body>
+ <div class="container-fluid">
+ <form action="login" method="post">
+ <h1>Login</h1>
+
+ <label for="username">Username:</label>
+ <input type="text" id="username" name="username" value="{{user}}" />
+
+ <label for="password">Password:</label>
+ <input type="password" id="password" name="password" />
+
+ <br />
+ <input type="submit" value="Login" />
+ </form>
+ </div>
+ </body>
+</html>