2 #include <json-c/json.h>
4 #include <afb/afb-binding>
6 afb::event event_login, event_logout;
10 json_object *args, *user, *passwd;
15 if (!json_object_object_get_ex(args, "user", &user)
16 || !json_object_object_get_ex(args, "password", &passwd)) {
17 AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args));
18 req.fail("bad-request");
19 } else if (afb_req_context_get(req)) {
20 AFB_REQ_ERROR(req, "login, bad state, logout first");
21 req.fail("bad-state");
22 } else if (strcmp(json_object_get_string(passwd), "please")) {
23 AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args));
24 req.fail("unauthorized");
26 usr = strdup(json_object_get_string(user));
27 AFB_REQ_NOTICE(req, "login user: %s", usr);
28 req.session_set_LOA(1);
29 req.context_set(usr, free);
31 event_login.push(json_object_new_string(usr));
35 void action(afb_req r)
37 json_object *args, *val;
42 usr = (char*)req.context_get();
43 AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args));
44 if (json_object_object_get_ex(args, "subscribe", &val)) {
45 if (json_object_get_boolean(val)) {
46 AFB_REQ_NOTICE(req, "user %s subscribes to events", usr);
47 req.subscribe(event_login);
48 req.subscribe(event_logout);
50 AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr);
51 req.unsubscribe(event_login);
52 req.unsubscribe(event_logout);
55 req.success(json_object_get(args));
58 void logout(afb_req r)
63 usr = (char*)req.context_get();
64 AFB_REQ_NOTICE(req, "login user %s out", usr);
65 event_logout.push(json_object_new_string(usr));
66 req.session_set_LOA(0);
74 event_login = afb_daemon_make_event("login");
75 event_logout = afb_daemon_make_event("logout");
76 if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout))
78 AFB_ERROR("Can't create events");
82 const afb_verb_v2 verbs[] = {
83 afb::verb("login", login, "log in the system"),
84 afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1),
85 afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1),
89 const afb_binding_v2 afbBindingV2 = afb::binding("tuto-3", verbs, "third tutorial: C++", init);