2 #include <json-c/json.h>
4 #define AFB_BINDING_VERSION 3
5 #include <afb/afb-binding>
7 afb::event event_login, event_logout;
9 void login(afb_req_t r)
11 json_object *args, *user, *passwd;
16 if (!json_object_object_get_ex(args, "user", &user)
17 || !json_object_object_get_ex(args, "password", &passwd)) {
18 AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args));
19 req.fail("bad-request");
20 } else if (afb_req_context_get(req)) {
21 AFB_REQ_ERROR(req, "login, bad state, logout first");
22 req.fail("bad-state");
23 } else if (strcmp(json_object_get_string(passwd), "please")) {
24 AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args));
25 req.fail("unauthorized");
27 usr = strdup(json_object_get_string(user));
28 AFB_REQ_NOTICE(req, "login user: %s", usr);
29 req.session_set_LOA(1);
30 // req.context(1, nullptr, free, usr);
32 event_login.push(json_object_new_string(usr));
36 void action(afb_req_t r)
38 json_object *args, *val;
43 // usr = (char*)req.context_get();
45 AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args));
46 if (json_object_object_get_ex(args, "subscribe", &val)) {
47 if (json_object_get_boolean(val)) {
48 AFB_REQ_NOTICE(req, "user %s subscribes to events", usr);
49 req.subscribe(event_login);
50 req.subscribe(event_logout);
52 AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr);
53 req.unsubscribe(event_login);
54 req.unsubscribe(event_logout);
57 req.success(json_object_get(args));
60 void logout(afb_req_t r)
65 // usr = (char*)req.context_get();
67 AFB_REQ_NOTICE(req, "login user %s out", usr);
68 event_logout.push(json_object_new_string(usr));
69 req.session_set_LOA(0);
70 // req.context_clear();
75 #if AFB_BINDING_VERSION >= 3
81 event_login = afb_daemon_make_event("login");
82 event_logout = afb_daemon_make_event("logout");
83 if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout))
85 AFB_ERROR("Can't create events");
89 const afb_verb_t verbs[] = {
90 afb::verb("login", login, "log in the system"),
91 afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1),
92 afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1),
96 const afb_binding_t afbBindingExport = afb::binding("tuto-3", verbs, "third tutorial: C++", init);