pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

commit f48d59f59b36c81e02055e2446536bbe9779051e
parent dd48f1809b6d38fb19c0ec7e5b29f254bd75dc8a
Author: Jul <jul@9o.is>
Date:   Tue, 22 Jul 2014 13:42:28 -0400

ATM REST interface. Implemented user purchase limit with user verification

Diffstat:
Msrc/main/scala/bootstrap/liftweb/Boot.scala | 3+++
Asrc/main/scala/inc/pyc/rest/AtmRest.scala | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -88,6 +88,9 @@ class Boot extends Loggable { // ID Verification API IdVerificationUpload.init() + + // Rest service for ATM's + AtmRest.init() } private def prettyPrintMime(m: MimeMessage): String = { diff --git a/src/main/scala/inc/pyc/rest/AtmRest.scala b/src/main/scala/inc/pyc/rest/AtmRest.scala @@ -0,0 +1,54 @@ +package inc.pyc +package rest + +import model._ +import net.liftweb._ +import common._ +import http._ +import rest._ +import util._ +import Helpers._ +import json._ +import JsonDSL._ + + +object AtmRest extends RestHelper { + + def init(): Unit = { + LiftRules.dispatch.append(AtmRest) + } + + /** + * API for uploading files to server. + */ + serve("api" / "atm" prefix { + + // /api/atm/{atm_id}/{atm_password}/user/{user_name}/{user_one-time_password}/purchase_limit + case id :: pw :: "user" :: username :: verifyPass :: "purchase_limit" :: Nil JsonGet _ => + authenticate(id, pw, { + authenticateUser(username, verifyPass, { + user => + success("purchase_limit" -> user.userLimitAsInt) + }) + }) + }) + + def authenticate(id: String, passwd: String, f: JValue) = + for { + atm <- Atm.findByStringId(id) ?~ "Invalid Credentials" ~> 401 + if atm.secret.isMatch(passwd) + } yield f + + def authenticateUser(username: String, verifyPass: String, f: User => JValue) = + (for { + user <- User.findByUsername(username) + if user.verifypass.get == verifyPass + } yield f(user)) openOr failure("Invalid Verification Credentials") + + + def success(data: JValue) = response(true, data = data) + def failure(reason: String) = response(false, reason = reason) + + def response(success: Boolean, data: JValue = JNull, reason: String = "") = + ("success" -> success) ~ ("data" -> data) ~ ("reason" -> reason) +} +\ No newline at end of file