Update copyright dates
[src/app-framework-binder.git] / bindings / samples / AuthLogin.c
1 /*
2  * Copyright (C) 2015-2020 "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 #define AFB_BINDING_VERSION 3
23 #include <afb/afb-binding.h>
24
25 // Dummy sample of Client Application Context
26 typedef struct {
27   int  something;
28   void *whateveryouwant;
29 } MyClientApplicationHandle;
30
31
32 // This function is call when Client Session Context is removed
33 // Note: when freeCtxCB==NULL standard free/malloc is called
34 static void clientContextFree(void *context) {
35     fprintf (stderr,"Plugin[token] Closing Session\n");
36     free (context);
37 }
38
39 // Request Creation of new context if it does not exist
40 static void clientContextConnect (afb_req_t request)
41 {
42     json_object *jresp;
43
44     // add an application specific client context to session
45     afb_req_context_set(request, malloc (sizeof (MyClientApplicationHandle)), clientContextFree);
46
47     // do something intelligent to check if we should or not update level of assurance from 0(anonymous) to 1(logged)
48     afb_req_session_set_LOA(request, 1);
49
50     // Send response to UI
51     jresp = json_object_new_object();
52     json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
53
54     afb_req_success(request, jresp, NULL);
55
56 }
57
58 // Before entering here token will be check and renew
59 static void clientContextRefresh (afb_req_t request) {
60     json_object *jresp;
61
62
63     jresp = json_object_new_object();
64     json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
65
66     afb_req_success(request, jresp, NULL);
67 }
68
69
70 // Session token will we verified before entering here
71 static void clientContextCheck (afb_req_t request) {
72
73     json_object *jresp = json_object_new_object();
74     json_object_object_add(jresp, "isvalid", json_object_new_boolean (1));
75
76     afb_req_success(request, jresp, NULL);
77 }
78
79
80 // Close and Free context
81 static void clientContextLogout (afb_req_t request) {
82     json_object *jresp;
83
84     /* after this call token will be reset
85      *  - no further access to API will be possible
86      *  - every context from any used plugin will be freed
87      */
88
89     jresp = json_object_new_object();
90     json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
91
92     // WARNING: if you free context resource manually here do not forget to set *request.context=NULL;
93     afb_req_success(request, jresp, NULL);
94
95     afb_req_session_set_LOA(request, 0);
96 }
97 // Close and Free context
98 static void clientGetPing (afb_req_t request) {
99     static int count=0;
100     json_object *jresp;
101
102     jresp = json_object_new_object();
103     json_object_object_add(jresp, "count", json_object_new_int (count ++));
104
105     afb_req_success(request, jresp, NULL);
106 }
107
108
109 static const struct afb_verb_v3 verbs[]= {
110   {.verb="ping"    , .session=AFB_SESSION_NONE                     , .callback=clientGetPing       ,.info="Ping Rest Test Service"},
111   {.verb="connect" , .session=AFB_SESSION_LOA_0 | AFB_SESSION_RENEW, .callback=clientContextConnect,.info="Connect/Login Client"},
112   {.verb="refresh" , .session=AFB_SESSION_LOA_1 | AFB_SESSION_RENEW, .callback=clientContextRefresh,.info="Refresh Client Authentication Token"},
113   {.verb="check"   , .session=AFB_SESSION_LOA_1                    , .callback=clientContextCheck  ,.info="Check Client Authentication Token"},
114   {.verb="logout"  , .session=AFB_SESSION_LOA_1 | AFB_SESSION_CLOSE, .callback=clientContextLogout ,.info="Logout Client and Free resources"},
115   {0, 0, 0, 0, 0, 0, 0}
116 };
117
118 const struct afb_binding_v3 afbBindingV3 =
119 {
120     .api   = "auth",            /* the API name (or binding name or prefix) */
121     .info  = "Application Framework Binder Authentication sample",      /* short description of of the binding */
122     .verbs = verbs      /* the array describing the verbs of the API */
123 };