2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* ----------------------------------------------------------------------
20 * This module simulate Application Framework Binder
23 * /api/afbs/check?token=123456789
24 * /api/afbs/refresh?token=123456789-xxxxx
25 * /api/afbs/reset?123456789-xxxxx
27 * Note: this MOCK api does not handle any session login. It only returns
28 * a fake valid or false message depending on call order.
29 * Its goal is to get a quick way to check you HTML5 client rendering & behaviour.
31 * When you're happy with you HTML5 client OnePageApp check it with afb-daemon
32 * ----------------------------------------------------------------------*/
35 function NewApi(handle, prefix) {
36 var scope=this; // I hate JavaScript
37 scope.connected=false;
39 // Simulate Client Context Session Creation
40 handle.app.get(prefix +'/create', function (req, res) {
41 handle.trace (scope, 1, "%s/create body=%s", prefix, req.body.action);
42 var okResponse= '{ "jtype": "AJB_reply"' +
43 ', "request": { "prefix": "afbs", "api": "create", "uuid": "e4ef5e66-xxxx", "token": "123456789-xxxxx", "status": "processed" }'+
44 ', "response": { "token": "Token was refreshed" }'+
47 var fxResponse= '{ "jtype": "AJB_reply" ' +
48 ', "request": { "prefix": "afbs", "api": "create", "status": "fail", "info": "AFB_SESSION_REFRESH Not Initial Token Chain" }'+
51 if (scope.connected) res.status(401).send(fxResponse);
59 // Simulate Client Context Check
60 handle.app.get(prefix +'/check', function (req, res) {
61 handle.trace (scope, 1, "%s/check query=%s", prefix, req.query.token);
62 var okResponse= '{"jtype":"AJB_reply"'+
63 ',"request":{"prefix":"afbs","api":"check", "status":"processed"}'+
64 ',"response":{"isvalid":true}'+
67 var fxResponse= '{"jtype":"AJB_reply",'+
68 '"request":{"prefix":"afbs","api":"check","status":"empty","info":"AFB_SESSION_CHECK Not a Valid Active Token"}'+
71 if (!scope.connected) res.status(401).send(fxResponse);
72 else res.send(okResponse);
75 // Simulate Client Context Check
76 handle.app.get(prefix +'/refresh', function (req, res) {
77 handle.trace (scope, 1, "%s/refresh query=%s", prefix, req.query.token);
78 var okResponse= '{"jtype":"AJB_reply"'+
79 ',"request":{"prefix":"afbs","api":"refresh","uuid": "e4ef5e66-xxxx", "token": "123456789-xxxxx","status":"processed"}'+
80 ',"response":{"isvalid":true}'+
83 var fxResponse= '{"jtype":"AJB_reply",'+
84 '"request":{"prefix":"afbs","api":"refresh","status":"empty","info":"AFB_SESSION_REFRESH Not a Valid Active Token"}'+
87 if (!scope.connected) res.status(401).send(fxResponse);
88 else res.send(okResponse);
91 // Simulate Client Context Session Closing
92 handle.app.get(prefix +'/reset', function (req, res) {
93 handle.trace (scope, 1, "%s/reset query=%s", prefix, req.query.token);
94 var okResponse= '{"jtype":"AJB_reply"'+
95 ',"request":{"prefix":"afbs","api":"reset","uuid": "e4ef5e66-xxxx","status":"processed"}'+
96 ',"response":{"uuid":"b028b883-8b47-4c6d-9c6e-e79b9e2b81b9"}'+
99 var fxResponse= '{"jtype":"AJB_reply",'+
100 '"request":{"prefix":"afbs","api":"reset","status":"empty","info":"AFB_SESSION_CLOSE Not a Valid Access Token"}'+
103 if (!scope.connected) res.status(401).send(fxResponse);
105 res.send(okResponse);
106 scope.connected=false;
114 module.exports = NewApi;