Added Session Management
[src/app-framework-binder.git] / src / afbs-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 PUBLIC json_object* clientContextCreate (AFB_request *request) {
31     json_object *jresp;
32     int   res;
33     char *token;
34     AFB_clientCtx *client=request->client; // get client context from request
35
36     // check we do not already have a session
37     if (client->handle != NULL) {
38         request->errcode=MHD_HTTP_FORBIDDEN;
39         return (jsonNewMessage(AFB_FAIL, "Token exist use refresh"));
40     }
41         
42     // request a new client context token and check result
43     ctxTokenCreate (request);
44    
45     // add a client handle to session
46     client->handle = malloc (sizeof (MyClientApplicationHandle));
47     
48     // Send response to UI
49     jresp = json_object_new_object();               
50     json_object_object_add(jresp, "token", json_object_new_string (client->token));
51
52     return (jresp);
53 }
54
55 // Renew an existing context
56 PUBLIC json_object* clientContextRefresh (AFB_request *request) {
57     json_object *jresp;
58
59     // check we do not already have a session
60     if (request->client == NULL) return (jsonNewMessage(AFB_FAIL, "No Previous Token use Create"));
61     
62     // note: we do not need to parse the old token as clientContextRefresh doit for us
63     if (ctxTokenRefresh (request)) {
64         jresp = json_object_new_object();
65         json_object_object_add(jresp, "token", json_object_new_string (request->client->token));              
66     } else {
67         request->errcode=MHD_HTTP_UNAUTHORIZED;
68         jresp= jsonNewMessage(AFB_FAIL, "Token Exchange Broken Refresh Refused");
69     }
70             
71     return (jresp);
72 }
73
74
75 // Verify a context is still valid 
76 PUBLIC json_object* clientContextCheck (AFB_request *request) {
77     json_object *jresp;
78     int isvalid;
79
80     // check is token is valid
81     isvalid= ctxTokenCheck (request);
82     
83     // add an error code to respond
84     if (!isvalid) request->errcode=MHD_HTTP_UNAUTHORIZED;
85     
86     // prepare response for client side application
87     jresp = json_object_new_object();
88     json_object_object_add(jresp, "isvalid", json_object_new_boolean (isvalid));
89     
90     return (jresp); 
91 }
92
93 // Close and Free context
94 PUBLIC json_object* clientContextReset (AFB_request *request) {
95     json_object *jresp;
96     
97     jresp = json_object_new_object();
98     json_object_object_add(jresp, "done", json_object_new_boolean (ctxTokenReset (request)));
99     
100     return (jresp); 
101 }
102
103
104 STATIC  AFB_restapi pluginApis[]= {
105   {"ping"          , (AFB_apiCB)apiPingTest         ,"Ping Rest Test Service", NULL},
106   {"token-create"  , (AFB_apiCB)clientContextCreate ,"Request Client Context Creation",NULL},
107   {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token",NULL},
108   {"token-check"   , (AFB_apiCB)clientContextCheck  ,"Check Client Context Token",NULL},
109   {"token-reset"   , (AFB_apiCB)clientContextReset  ,"Close Client Context and Free resources",NULL},
110   {0,0,0,0}
111 };
112
113 PUBLIC AFB_plugin *afsvRegister () {
114     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
115     plugin->type  = AFB_PLUGIN; 
116     plugin->info  = "Application Framework Binder Service";
117     plugin->prefix= "afbs";  // url base
118     plugin->apis  = pluginApis;
119     
120     return (plugin);
121 };