prepares event propagation
[src/app-framework-binder.git] / plugins / session / token-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <json.h>
21
22 #include "afb-plugin.h"
23
24 // Dummy sample of Client Application Context
25 typedef struct {
26   int  something;       
27   void *whateveryouwant;
28 } MyClientApplicationHandle;
29
30
31 // Request Creation of new context if it does not exist
32 static void clientContextCreate (struct afb_req request)
33 {
34     json_object *jresp;
35
36     // add an application specific client context to session
37     request.context = malloc (sizeof (MyClientApplicationHandle));
38     
39     // Send response to UI
40     jresp = json_object_new_object();               
41     json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
42
43     afb_req_success(request, jresp, NULL);
44 }
45
46 // Before entering here token will be check and renew
47 static void clientContextRefresh (struct afb_req request) {
48     json_object *jresp;
49
50   
51     jresp = json_object_new_object();
52     json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));              
53     
54     afb_req_success(request, jresp, NULL);
55 }
56
57
58 // Session token will we verified before entering here
59 static void clientContextCheck (struct afb_req request) {
60     
61     json_object *jresp = json_object_new_object();    
62     json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));       
63         
64     afb_req_success(request, jresp, NULL);
65 }
66
67
68 // Close and Free context
69 static void clientContextReset (struct afb_req request) {
70     json_object *jresp;
71    
72     /* after this call token will be reset
73      *  - no further access to API will be possible 
74      *  - every context from any used plugin will be freed
75      */
76     
77     jresp = json_object_new_object();
78     json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
79     
80     // WARNING: if you free context resource manually here do not forget to set *request.context=NULL; 
81     afb_req_success(request, jresp, NULL);
82 }
83 // Close and Free context
84 static void clientGetPing (struct afb_req request) {
85     static int count=0;
86     json_object *jresp;
87
88     jresp = json_object_new_object();
89     json_object_object_add(jresp, "count", json_object_new_int (count ++));
90     
91     afb_req_success(request, jresp, NULL);
92 }
93
94
95 // This function is call when Client Session Context is removed
96 // Note: when freeCtxCB==NULL standard free/malloc is called
97 static void clientContextFree(void *context) {
98     fprintf (stderr,"Plugin[token] Closing Session\n");
99     free (context);
100 }
101
102 static const struct AFB_restapi pluginApis[]= {
103   {"ping"    , AFB_SESSION_NONE  , clientGetPing       ,"Ping Rest Test Service"},
104   {"create"  , AFB_SESSION_CREATE, clientContextCreate ,"Request Client Context Creation"},
105   {"refresh" , AFB_SESSION_RENEW , clientContextRefresh,"Refresh Client Context Token"},
106   {"check"   , AFB_SESSION_CHECK , clientContextCheck  ,"Check Client Context Token"},
107   {"reset"   , AFB_SESSION_CLOSE , clientContextReset  ,"Close Client Context and Free resources"},
108   {NULL}
109 };
110
111 static const struct AFB_plugin plugin_desc = {
112         .type = AFB_PLUGIN_JSON,
113         .info = "Application Framework Binder Service",
114         .prefix = "token",
115         .apis = pluginApis,
116         .freeCtxCB = clientContextFree
117 };
118
119 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
120 {
121         return &plugin_desc;
122 }