25d0b9936682733bb210338d13def20183ec2638
[src/app-framework-binder.git] / plugins / session / token-api.c
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 #include "local-def.h"
21
22 // Dummy sample of Client Application Context
23 typedef struct {
24   int  something;       
25   void *whateveryouwant;
26 } MyClientApplicationHandle;
27
28
29 // Request Creation of new context if it does not exist
30 STATIC json_object* clientContextCreate (AFB_request *request) {
31     json_object *jresp;
32
33     // add an application specific client context to session
34     request->context = malloc (sizeof (MyClientApplicationHandle));
35     
36     // Send response to UI
37     jresp = json_object_new_object();               
38     json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
39
40     return (jresp);
41 }
42
43 // Before entering here token will be check and renew
44 STATIC json_object* clientContextRefresh (AFB_request *request) {
45     json_object *jresp;
46
47   
48     jresp = json_object_new_object();
49     json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));              
50     
51     return (jresp);
52 }
53
54
55 // Session token will we verified before entering here
56 STATIC json_object* clientContextCheck (AFB_request *request) {
57     
58     json_object *jresp = json_object_new_object();    
59     json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));       
60         
61     return (jresp); 
62 }
63
64
65 // Close and Free context
66 STATIC json_object* clientContextReset (AFB_request *request) {
67     json_object *jresp;
68    
69     /* after this call token will be reset
70      *  - no further access to API will be possible 
71      *  - every context from any used plugin will be freed
72      */
73     
74     jresp = json_object_new_object();
75     json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
76     
77     // WARNING: if you free context resource manually here do not forget to set request->context=NULL; 
78     return (jresp); 
79 }
80 // Close and Free context
81 STATIC json_object* clientGetPing (AFB_request *request) {
82     static count=0;
83     json_object *jresp;
84
85     jresp = json_object_new_object();
86     json_object_object_add(jresp, "count", json_object_new_int (count ++));
87     
88     return (jresp); 
89 }
90
91
92 // This function is call when Client Session Context is removed
93 // Note: when freeCtxCB==NULL standard free/malloc is called
94 STATIC void clientContextFree(void *context, char* uuid) {
95     fprintf (stderr,"Plugin[token] Closing Session uuid=[%s]\n", uuid);
96     free (context);
97 }
98
99 STATIC  AFB_restapi pluginApis[]= {
100   {"ping"    , AFB_SESSION_NONE  , (AFB_apiCB)clientGetPing       ,"Ping Rest Test Service"},
101   {"create"  , AFB_SESSION_CREATE, (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"},
102   {"refresh" , AFB_SESSION_RENEW , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"},
103   {"check"   , AFB_SESSION_CHECK , (AFB_apiCB)clientContextCheck  ,"Check Client Context Token"},
104   {"reset"   , AFB_SESSION_CLOSE , (AFB_apiCB)clientContextReset  ,"Close Client Context and Free resources"},
105   {NULL}
106 };
107
108 PUBLIC AFB_plugin *pluginRegister () {
109     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
110     plugin->type  = AFB_PLUGIN_JSON; 
111     plugin->info  = "Application Framework Binder Service";
112     plugin->prefix= "token";  // url base
113     plugin->apis  = pluginApis;
114     plugin->handle= (void*) "What ever you want";
115     plugin->freeCtxCB= (void*) clientContextFree;
116     
117     return (plugin);
118 };