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