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