aboutsummaryrefslogtreecommitdiffstats
path: root/src/agl-forgerock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/agl-forgerock.c')
-rw-r--r--src/agl-forgerock.c105
1 files changed, 60 insertions, 45 deletions
diff --git a/src/agl-forgerock.c b/src/agl-forgerock.c
index 97cc4b9..90c2e8f 100644
--- a/src/agl-forgerock.c
+++ b/src/agl-forgerock.c
@@ -21,34 +21,36 @@
#include <json-c/json.h>
-#define AFB_BINDING_VERSION 2
+#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>
+#include "agl-identity-common.h"
#include "oidc-agent.h"
#include "aia-get.h"
-#ifndef NULL
-#define NULL 0
-#endif
-
static int expiration_delay = 5;
-static const char default_endpoint[] = "https://agl-graphapi.forgerocklabs.org/getuserprofilefromtoken";
+static const char default_endpoint[] =
+ "https://agl-graphapi.forgerocklabs.org/getuserprofilefromtoken";
+
static const char *oidc_name;
static char *endpoint;
static void (*onloaded)(struct json_object *data, const char *error);
+
/***** configuration ********************************************/
-static void confsetstr(struct json_object *conf, const char *name, char **value, const char *def)
+static void confsetstr(struct json_object *conf,
+ const char *name, char **value, const char *def)
{
struct json_object *v;
const char *s;
char *p;
- s = conf && json_object_object_get_ex(conf, name, &v) ? json_object_get_string(v) : def;
+ s = conf && json_object_object_get_ex(conf, name, &v) ?
+ json_object_get_string(v) : def;
p = *value;
if (s && p != s) {
*value = strdup(s);
@@ -56,24 +58,27 @@ static void confsetstr(struct json_object *conf, const char *name, char **value,
}
}
-static void confsetint(struct json_object *conf, const char *name, int *value, int def)
+static void confsetint(struct json_object *conf,
+ const char *name, int *value, int def)
{
struct json_object *v;
- *value = conf && json_object_object_get_ex(conf, name, &v) ? json_object_get_int(v) : def;
+ *value = conf && json_object_object_get_ex(conf, name, &v) ?
+ json_object_get_int(v) : def;
}
static void confsetoidc(struct json_object *conf, const char *name)
{
struct json_object *idp, *appli;
- if (conf
- && json_object_object_get_ex(conf, "idp", &idp)
- && json_object_object_get_ex(conf, "appli", &appli)) {
- if (oidc_idp_set(name, idp) && oidc_appli_set(name, name, appli, 1)) {
+ if (!conf ||
+ !json_object_object_get_ex(conf, "idp", &idp) ||
+ !json_object_object_get_ex(conf, "appli", &appli))
+ return;
+
+ if (oidc_idp_set(name, idp) &&
+ oidc_appli_set(name, name, appli, 1))
oidc_name = name;
- }
- }
}
/****************************************************************/
@@ -84,7 +89,8 @@ static void loaded(struct json_object *data, const char *error)
onloaded(data, error);
}
-static void downloaded(void *closure, int status, const void *buffer, size_t size)
+static void downloaded(void *closure, int status,
+ const void *buffer, size_t size)
{
struct json_object *object, *subobj;
struct json_object *objkey = closure;
@@ -97,14 +103,15 @@ static void downloaded(void *closure, int status, const void *buffer, size_t siz
if (status == 0 && !buffer) {
AFB_ERROR("discarded");
loaded(NULL, "discarded");
- goto end; /* discarded */
+ goto ignore;
}
/* scan for the status */
if (status == 0 || !buffer) {
- AFB_ERROR("uploading %s failed %s", url ? : "?", (const char*)buffer ? : "");
+ AFB_ERROR("uploading %s failed %s", url ? : "?",
+ (const char*)buffer ? : "");
loaded(NULL, "failed");
- goto end;
+ goto ignore;
}
/* get the object */
@@ -132,7 +139,7 @@ static void downloaded(void *closure, int status, const void *buffer, size_t siz
AFB_INFO("unrecognized key for %s", url ? : "?");
json_object_put(object);
loaded(NULL, "malformed");
- goto end;
+ goto ignore;
}
// Save the profile to the database
@@ -140,11 +147,14 @@ static void downloaded(void *closure, int status, const void *buffer, size_t siz
struct json_object* record = json_object_new_object();
json_object_object_add(record, "key", objkey);
json_object_object_add(record, "value", json_object_get(subobj));
- afb_service_call_sync("persistence", "update", record, &dbr);
+ afb_api_call_sync(get_local_api(), "persistence", "update",
+ record, &dbr, NULL, NULL);
loaded(subobj, NULL);
json_object_put(object);
-end:
+ json_object_put(dbr);
+
+ ignore:
json_object_put(objkey);
}
@@ -158,7 +168,8 @@ void agl_forgerock_setconfig(struct json_object *conf)
AFB_NOTICE("Forgerock endpoint is: %s", endpoint);
}
-void agl_forgerock_setcb(void (*callback)(struct json_object *data, const char *error))
+void agl_forgerock_setcb(
+ void (*callback)(struct json_object *data, const char *error))
{
onloaded = callback;
}
@@ -174,35 +185,39 @@ void reply_from_db(void* closure, int status, struct json_object* result)
struct json_object* tmp;
json_object_object_get_ex(result, "response", &tmp);
json_object_object_get_ex(tmp, "value", &tmp);
- AFB_NOTICE("User profile retrieved from persistence: %s", json_object_to_json_string(tmp));
+ AFB_NOTICE("User profile retrieved from persistence: %s",
+ json_object_to_json_string(tmp));
loaded(json_object_get(tmp), NULL);
}
-void agl_forgerock_download_request(const char *vin, const char *kind, const char *key)
+void agl_forgerock_download_request(const char *vin,
+ const char *kind,
+ const char *key)
{
int rc;
char *url;
- rc = asprintf(&url, "%s?vin=%s&kind=%s&keytoken=%s", endpoint, vin, kind, key);
- if (rc >= 0)
- {
- struct json_object* obj = json_object_new_object();
- json_object_object_add(obj, "url", json_object_new_string(url));
- json_object_object_add(obj, "vin", json_object_new_string(vin));
- json_object_object_add(obj, "kind", json_object_new_string(kind));
- json_object_object_add(obj, "key", json_object_new_string(key));
-
- // Async get from database and from forgerock
- struct json_object* key = json_object_new_object();
- json_object_object_add(key, "key", json_object_get(obj));
- afb_service_call("persistence", "read", key, reply_from_db, NULL);
-
- // Async get from forgerock
- aia_get(url, expiration_delay, oidc_name, oidc_name, downloaded, obj);
- free(url);
- }
- else
+ rc = asprintf(&url, "%s?vin=%s&kind=%s&keytoken=%s",
+ endpoint, vin, kind, key);
+ if (rc < 0) {
AFB_ERROR("out of memory");
+ return;
+ }
+
+ struct json_object* obj = json_object_new_object();
+ json_object_object_add(obj, "url", json_object_new_string(url));
+ json_object_object_add(obj, "vin", json_object_new_string(vin));
+ json_object_object_add(obj, "kind", json_object_new_string(kind));
+ json_object_object_add(obj, "key", json_object_new_string(key));
+
+ // Async get from database and from forgerock
+ struct json_object* key_obj = json_object_new_object();
+ json_object_object_add(key_obj, "key", json_object_get(obj));
+ afb_api_call(get_local_api(), "persistence", "read", key_obj, reply_from_db, NULL);
+
+ // Async get from forgerock
+ aia_get(url, expiration_delay, oidc_name, oidc_name, downloaded, obj);
+ free(url);
}
/* vim: set colorcolumn=80: */