+char *make_desc(struct json_object *o)
+{
+ return make_info(json_object_to_json_string_ext(root, 0), 1);
+}
+
+struct json_object *permissions_of_verb(struct json_object *obj)
+{
+ struct json_object *x, *y;
+
+ if (json_object_object_get_ex(obj, "x-permissions", &x))
+ return x;
+
+ if (json_object_object_get_ex(obj, "get", &x))
+ if (json_object_object_get_ex(x, "x-permissions", &y))
+ return y;
+
+ return NULL;
+}
+
+void print_perms()
+{
+ int i, n;
+
+ n = a_perms ? json_object_array_length(a_perms) : 0;
+ if (n) {
+ printf("static const struct afb_auth _afb_auths_v2_%s[] = {\n" , capi);
+ i = 0;
+ while (i < n) {
+ printf("\t{ %s }", json_object_get_string(json_object_array_get_idx(a_perms, i)));
+ printf(",\n"+(++i == n));
+ }
+ printf("};\n\n");
+ }
+}
+
+struct json_object *new_perm(struct json_object *obj, const char *desc)
+{
+ const char *tag;
+ char *b;
+ struct json_object *x, *y;
+
+ tag = obj ? json_object_to_json_string_ext(obj, 0) : desc;
+ if (!json_object_object_get_ex(d_perms, tag, &y)) {
+ if (!d_perms) {
+ d_perms = json_object_new_object();
+ a_perms = json_object_new_array();
+ }
+
+ asprintf(&b, "&_afb_auths_v2_%s[%d]", capi, json_object_array_length(a_perms));
+ x = json_object_new_string(desc);
+ y = json_object_new_string(b);
+ json_object_array_add(a_perms, x);
+ json_object_object_add(d_perms, tag, y);
+ free(b);
+ }
+ return y;
+}
+
+struct json_object *decl_perm(struct json_object *obj);
+
+struct json_object *decl_perm_a(const char *op, struct json_object *obj)
+{
+ int i, n;
+ char *a;
+ struct json_object *x, *y;
+
+ x = NULL;
+ i = n = obj ? json_object_array_length(obj) : 0;
+ while (i) {
+ y = decl_perm(json_object_array_get_idx(obj, --i));
+ if (!y)
+ ;
+ else if (!x)
+ x = y;
+ else if (x != y) {
+ asprintf(&a, ".type = afb_auth_%s, .first = %s, .next = %s",
+ op, json_object_get_string(y), json_object_get_string(x));
+ x = new_perm(NULL, a);
+ free(a);
+ }
+ }
+ return x;
+}
+
+struct json_object *decl_perm(struct json_object *obj)
+{
+ char *a;
+ struct json_object *x, *y;
+
+ if (json_object_object_get_ex(d_perms, json_object_to_json_string_ext(obj, 0), &x))
+ return x;
+
+ if (json_object_object_get_ex(obj, "permission", &x)) {
+ asprintf(&a, ".type = afb_auth_Permission, .text = \"%s\"", json_object_get_string(x));
+ y = new_perm(obj, a);
+ free(a);
+ }
+ else if (json_object_object_get_ex(obj, "anyOf", &x)) {
+ y = decl_perm_a("Or", x);
+ }
+ else if (json_object_object_get_ex(obj, "allOf", &x)) {
+ y = decl_perm_a("And", x);
+ }
+ else if (json_object_object_get_ex(obj, "not", &x)) {
+ x = decl_perm(x);
+ asprintf(&a, ".type = afb_auth_Not, .first = %s", json_object_get_string(x));
+ y = new_perm(obj, a);
+ free(a);
+ }
+ else if (json_object_object_get_ex(obj, "LOA", &x))
+ y = NULL;
+ else if (json_object_object_get_ex(obj, "session", &x))
+ y = NULL;
+ else
+ y = NULL;
+
+ return y;
+}
+
+void declare_permissions(const char *name, struct json_object *obj)
+{
+ struct json_object *p;
+
+ p = permissions_of_verb(obj);
+ if (p)
+ decl_perm(p);
+}
+
+
+#define SESSION_CLOSE 0x000001
+#define SESSION_RENEW 0x000010
+#define SESSION_CHECK 0x000100
+#define SESSION_LOA_1 0x001000
+#define SESSION_LOA_2 0x011000
+#define SESSION_LOA_3 0x111000
+#define SESSION_MASK 0x111111
+
+
+int get_session(struct json_object *obj);
+
+int get_session_a(int and, struct json_object *obj)
+{
+ int i, n, x, y;
+
+ n = obj ? json_object_array_length(obj) : 0;
+ if (n == 0)
+ return 0;
+
+ i = n;
+ x = get_session(json_object_array_get_idx(obj, --i));
+ while (i) {
+ y = get_session(json_object_array_get_idx(obj, --i));
+ if (and)
+ x &= y;
+ else
+ x |= y;
+ }
+ return x;
+}
+
+int get_session(struct json_object *obj)
+{
+ int y;
+ const char *a;
+ struct json_object *x;
+
+ y = 0;
+ if (json_object_object_get_ex(obj, "anyOf", &x)) {
+ y = get_session_a(1, x);
+ }
+ else if (json_object_object_get_ex(obj, "allOf", &x)) {
+ y = get_session_a(0, x);
+ }
+ else if (json_object_object_get_ex(obj, "not", &x)) {
+ y = ~get_session(x) & SESSION_MASK;
+ }
+ else if (json_object_object_get_ex(obj, "LOA", &x)) {
+ switch (json_object_get_int(x)) {
+ case 3: y = SESSION_LOA_3; break;
+ case 2: y = SESSION_LOA_2; break;
+ case 1: y = SESSION_LOA_1; break;
+ default: break;
+ }
+ }
+ else if (json_object_object_get_ex(obj, "session", &x)) {
+ a = json_object_get_string(x);
+ if (!strcmp(a, "check"))
+ y = SESSION_CHECK;
+ else if (!strcmp(a, "close"))
+ y = SESSION_CLOSE;
+ }
+ else if (json_object_object_get_ex(obj, "token", &x)) {
+ a = json_object_get_string(x);
+ if (!strcmp(a, "refresh"))
+ y = SESSION_RENEW;
+ }
+
+ return y;
+}
+
+void print_session(struct json_object *p)
+{
+ int s, c, l;
+
+ s = p ? get_session(p) : 0;
+ c = 1;
+ if (s & SESSION_CHECK) {
+ printf("%s", "|AFB_SESSION_CHECK_V2" + c);
+ c = 0;
+ }
+ if (s & SESSION_LOA_3 & ~SESSION_LOA_2)
+ l = 3;
+ else if (s & SESSION_LOA_2 & ~SESSION_LOA_1)
+ l = 2;
+ else if (s & SESSION_LOA_1)
+ l = 1;
+ else
+ l = 0;
+ if (l) {
+ printf("%s%d_V2", "|AFB_SESSION_LOA_" + c, l);
+ c = 0;
+ }
+ if (s & SESSION_CLOSE) {
+ printf("%s", "|AFB_SESSION_CLOSE_V2" + c);
+ c = 0;
+ }
+ if (s & SESSION_RENEW) {
+ printf("%s", "|AFB_SESSION_REFRESH_V2" + c);
+ c = 0;
+ }
+ if (c)
+ printf("AFB_SESSION_NONE_V2");
+}
+
+void print_verb(const char *name)
+{
+ printf("%s%s%s" , prefix, name, postfix);
+}
+
+void print_declare_verb(const char *name, struct json_object *obj)
+{
+ printf("%s void ", scope);
+ print_verb(name);
+ printf("(struct afb_req req);\n");
+}
+
+void print_struct_verb(const char *name, struct json_object *obj)
+{
+ struct json_object *p, *i;
+ const char *info;
+
+ info = NULL;
+ if (json_object_object_get_ex(obj, "description", &i))
+ info = json_object_get_string(i);
+
+ p = permissions_of_verb(obj);
+ printf(
+ " {\n"
+ " .verb = \"%s\",\n"
+ " .callback = "
+ , name
+ );
+ print_verb(name);
+ printf(
+ ",\n"
+ " .auth = %s,\n"
+ " .info = %s,\n"
+ " .session = "
+ , p && decl_perm(p) ? json_object_get_string(decl_perm(p)) : "NULL"
+ , info ? make_info(info, 0) : "NULL"
+ );
+ print_session(p);
+ printf(
+ "\n"
+ " },\n"
+ );
+}
+
+void enum_verbs(void (*func)(const char *name, struct json_object *obj))