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