Adds 2017 to copyrights
[src/app-framework-binder.git] / bindings / samples / AuthLogin.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "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-binding.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 clientContextConnect (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     // do something intelligent to check if we should or not update level of assurance from 0(anonymous) to 1(logged)
47     afb_req_session_set_LOA(request, 1);
48
49     // Send response to UI
50     jresp = json_object_new_object();
51     json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
52
53     afb_req_success(request, jresp, NULL);
54
55 }
56
57 // Before entering here token will be check and renew
58 static void clientContextRefresh (struct afb_req request) {
59     json_object *jresp;
60
61
62     jresp = json_object_new_object();
63     json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
64
65     afb_req_success(request, jresp, NULL);
66 }
67
68
69 // Session token will we verified before entering here
70 static void clientContextCheck (struct afb_req request) {
71
72     json_object *jresp = json_object_new_object();
73     json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
74
75     afb_req_success(request, jresp, NULL);
76 }
77
78
79 // Close and Free context
80 static void clientContextLogout (struct afb_req request) {
81     json_object *jresp;
82
83     /* after this call token will be reset
84      *  - no further access to API will be possible
85      *  - every context from any used plugin will be freed
86      */
87
88     jresp = json_object_new_object();
89     json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
90
91     // WARNING: if you free context resource manually here do not forget to set *request.context=NULL;
92     afb_req_success(request, jresp, NULL);
93
94     afb_req_session_set_LOA(request, 0);
95 }
96 // Close and Free context
97 static void clientGetPing (struct afb_req request) {
98     static int count=0;
99     json_object *jresp;
100
101     jresp = json_object_new_object();
102     json_object_object_add(jresp, "count", json_object_new_int (count ++));
103
104     afb_req_success(request, jresp, NULL);
105 }
106
107
108 static const struct afb_verb_desc_v1 verbs[]= {
109   {"ping"    , AFB_SESSION_NONE                        , clientGetPing       ,"Ping Rest Test Service"},
110   {"connect" , AFB_SESSION_LOA_EQ_0 | AFB_SESSION_RENEW, clientContextConnect,"Connect/Login Client"},
111   {"refresh" , AFB_SESSION_LOA_GE_1 | AFB_SESSION_RENEW, clientContextRefresh,"Refresh Client Authentication Token"},
112   {"check"   , AFB_SESSION_LOA_GE_1                    , clientContextCheck  ,"Check Client Authentication Token"},
113   {"logout"  , AFB_SESSION_LOA_GE_1 | AFB_SESSION_CLOSE, clientContextLogout ,"Logout Client and Free resources"},
114   {NULL}
115 };
116
117 static const struct afb_binding plugin_desc = {
118         .type = AFB_BINDING_VERSION_1,
119         .v1 = {
120                 .info = "Application Framework Binder Authentication sample",
121                 .prefix = "auth",
122                 .verbs = verbs
123         }
124 };
125
126 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
127 {
128         return &plugin_desc;
129 }