update to afb-daemon evolutions
[src/app-framework-demo.git] / afm-client / app / Backend / RestApis / TokenMockApi.js
1 /* 
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 /* ----------------------------------------------------------------------
20  *   This module simulate Application Framework Binder
21  *   
22  *   /api/afbs/create
23  *   /api/afbs/check?token=123456789
24  *   /api/afbs/refresh?token=123456789-xxxxx
25  *   /api/afbs/reset?123456789-xxxxx
26  *   
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.
30  *   
31  *   When you're happy with you HTML5 client OnePageApp check it with afb-daemon
32  * ----------------------------------------------------------------------*/
33
34  
35 function NewApi(handle, prefix) {
36     var scope=this; // I hate JavaScript
37     scope.connected=false;
38     
39     // Simulate Client Context Session Creation
40     handle.app.get(prefix +'/create', function (req, res) {
41         handle.trace (scope, 1, "%s/create", prefix);
42         var okResponse= '{ "jtype": "afb-reply"' +
43                         ', "request": { "prefix": "afbs", "api": "create", "uuid": "e4ef5e66-xxxx", "token": "123456789-xxxxx", "status": "processed" }'+
44                         ', "response": { "token": "Token was refreshed" }'+
45                         '}';
46                 
47         var fxResponse= '{ "jtype": "afb-reply" ' +
48                         ', "request": { "prefix": "afbs", "api": "create", "status": "fail", "info": "AFB_SESSION_REFRESH Not Initial Token Chain" }'+
49                         '}';
50     
51         //if (scope.connected) res.status(401).send(fxResponse);
52         //else {
53             res.send(okResponse);
54             scope.connected=true;
55         //}
56     });
57     
58     
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":"afb-reply"'+
63                         ',"request":{"prefix":"afbs","api":"check", "status":"processed"}'+
64                         ',"response":{"isvalid":true}'+
65                         '}';
66                 
67         var fxResponse= '{"jtype":"afb-reply",'+
68                         '"request":{"prefix":"afbs","api":"check","status":"empty","info":"AFB_SESSION_CHECK Not a Valid Active Token"}'+
69                         '}';
70     
71         if (!scope.connected) res.status(401).send(fxResponse);
72         else res.send(okResponse);
73     });
74     
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":"afb-reply"'+
79                         ',"request":{"prefix":"afbs","api":"refresh","uuid": "e4ef5e66-xxxx", "token": "123456789-xxxxx","status":"processed"}'+
80                         ',"response":{"isvalid":true}'+
81                         '}';
82                 
83         var fxResponse= '{"jtype":"afb-reply",'+
84                         '"request":{"prefix":"afbs","api":"refresh","status":"empty","info":"AFB_SESSION_REFRESH Not a Valid Active Token"}'+
85                         '}';
86     
87         if (!scope.connected) res.status(401).send(fxResponse);
88         else res.send(okResponse);
89     });
90
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":"afb-reply"'+
95                         ',"request":{"prefix":"afbs","api":"reset","uuid": "e4ef5e66-xxxx","status":"processed"}'+
96                         ',"response":{"uuid":"b028b883-8b47-4c6d-9c6e-e79b9e2b81b9"}'+
97                         '}';
98                 
99         var fxResponse= '{"jtype":"afb-reply",'+
100                         '"request":{"prefix":"afbs","api":"reset","status":"empty","info":"AFB_SESSION_CLOSE Not a Valid Access Token"}'+
101                         '}';
102     
103         if (!scope.connected) res.status(401).send(fxResponse);
104         else {
105             res.send(okResponse);
106             scope.connected=false;
107         }
108     });
109     
110
111 }
112
113 // Export Class
114 module.exports = NewApi;