summaryrefslogtreecommitdiffstats
path: root/agl-identity-service/src
diff options
context:
space:
mode:
Diffstat (limited to 'agl-identity-service/src')
-rw-r--r--agl-identity-service/src/CMakeLists.txt4
-rw-r--r--agl-identity-service/src/agl-forgerock.c51
-rw-r--r--agl-identity-service/src/agl-identity-binding.c50
-rw-r--r--agl-identity-service/src/aia-get.c7
4 files changed, 101 insertions, 11 deletions
diff --git a/agl-identity-service/src/CMakeLists.txt b/agl-identity-service/src/CMakeLists.txt
index 1ceb851..2e86ecc 100644
--- a/agl-identity-service/src/CMakeLists.txt
+++ b/agl-identity-service/src/CMakeLists.txt
@@ -41,3 +41,7 @@ set_target_properties(afb-identity-binding PROPERTIES
OUTPUT_NAME "${TARGET_NAME}"
)
+add_custom_command(TARGET ${TARGET_NAME}
+PRE_BUILD
+COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/../package/htdocs
+COMMAND cp -rv ${CMAKE_CURRENT_SOURCE_DIR}/../htdocs ${CMAKE_CURRENT_BINARY_DIR}/../package/)
diff --git a/agl-identity-service/src/agl-forgerock.c b/agl-identity-service/src/agl-forgerock.c
index 2a0d3da..c0c49ab 100644
--- a/agl-identity-service/src/agl-forgerock.c
+++ b/agl-identity-service/src/agl-forgerock.c
@@ -27,6 +27,10 @@
#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";
@@ -83,7 +87,11 @@ static void loaded(struct json_object *data, const char *error)
static void downloaded(void *closure, int status, const void *buffer, size_t size)
{
struct json_object *object, *subobj;
- char *url = closure;
+ struct json_object *objkey = closure;
+ struct json_object *tmp;
+
+ json_object_object_get_ex(objkey, "url", &tmp);
+ const char *url = json_object_get_string(tmp);
/* checks whether discarded */
if (status == 0 && !buffer) {
@@ -127,10 +135,17 @@ static void downloaded(void *closure, int status, const void *buffer, size_t siz
goto end;
}
+ // Save the profile to the database
+ struct json_object* dbr;
+ 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);
+
loaded(subobj, NULL);
json_object_put(object);
end:
- free(url);
+ json_object_put(objkey);
}
/** public **************************************************************/
@@ -148,6 +163,21 @@ void agl_forgerock_setcb(void (*callback)(struct json_object *data, const char *
onloaded = callback;
}
+void reply_from_db(void* closure, int status, struct json_object* result)
+{
+ if (status)
+ {
+ AFB_ERROR("Failed to retrieve profile from persistence!");
+ return;
+ }
+
+ 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));
+ loaded(json_object_get(tmp), NULL);
+}
+
void agl_forgerock_download_request(const char *vin, const char *kind, const char *key)
{
int rc;
@@ -155,7 +185,22 @@ void agl_forgerock_download_request(const char *vin, const char *kind, const cha
rc = asprintf(&url, "%s?vin=%s&kind=%s&keytoken=%s", endpoint, vin, kind, key);
if (rc >= 0)
- aia_get(url, expiration_delay, oidc_name, oidc_name, downloaded, url);
+ {
+ 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
AFB_ERROR("out of memory");
}
diff --git a/agl-identity-service/src/agl-identity-binding.c b/agl-identity-service/src/agl-identity-binding.c
index b10703e..12f43bd 100644
--- a/agl-identity-service/src/agl-identity-binding.c
+++ b/agl-identity-service/src/agl-identity-binding.c
@@ -29,6 +29,10 @@
#include "agl-forgerock.h"
+#ifndef NULL
+#define NULL 0
+#endif
+
static struct afb_event event;
static struct json_object *current_identity;
@@ -126,6 +130,18 @@ static int send_event_object(const char *name, const char *id, const char *nick)
static void do_login(struct json_object *desc)
{
+ if (current_identity == NULL && desc == NULL) return; // Switching from NULL to NULL -> do nothing
+ if (current_identity && desc)
+ {
+ const char* a = json_object_to_json_string(current_identity);
+ const char* b = json_object_to_json_string(desc);
+ if (strcmp(a, b) == 0)
+ {
+ AFB_NOTICE("Reloging of the same user.");
+ return; // Switching from one user to the same user -> do nothing
+ }
+ }
+
struct json_object *object;
/* switching the user */
@@ -226,6 +242,9 @@ static int service_init()
if (afb_daemon_require_api("nfc", 1))
return -1;
+ if (afb_daemon_require_api("persistence", 1))
+ return -1;
+
afb_service_call("nfc", "start", NULL, on_nfc_started, NULL);
return 0;
@@ -257,6 +276,35 @@ static void onevent(const char *event, struct json_object *object)
AFB_WARNING("Unhandled event: %s", event);
}
+static void fake_auth(struct afb_req req)
+{
+ struct json_object* req_object;
+ struct json_object* kind_object;
+ struct json_object* key_object;
+
+ req_object = afb_req_json(req);
+
+ if (!json_object_object_get_ex(req_object, "kind", &kind_object))
+ {
+ afb_req_fail(req, "Missing arg: kind", NULL);
+ return;
+ }
+
+ if (!json_object_object_get_ex(req_object, "key", &key_object))
+ {
+ afb_req_fail(req, "Missing arg: key", NULL);
+ return;
+ }
+
+ const char* kind = json_object_get_string(kind_object);
+ const char* key = json_object_get_string(key_object);
+
+ send_event_object("incoming", key, key);
+ agl_forgerock_download_request(vin ? vin : default_vin, kind, key);
+
+ afb_req_success(req, NULL, "fake auth success!");
+}
+
// NOTE: this sample does not use session to keep test a basic as possible
// in real application most APIs should be protected with AFB_SESSION_CHECK
static const struct afb_verb_v2 verbs[]=
@@ -266,6 +314,7 @@ static const struct afb_verb_v2 verbs[]=
{"fake-login" , fake_login , NULL, "fake a login" , AFB_SESSION_NONE },
{"logout" , logout , NULL, "log the current user out", AFB_SESSION_NONE },
{"get" , get , NULL, "get data" , AFB_SESSION_NONE },
+ {"fake-auth" , fake_auth , NULL, "fake an authentication" , AFB_SESSION_NONE },
{NULL}
};
@@ -282,4 +331,3 @@ const struct afb_binding_v2 afbBindingV2 =
};
/* vim: set colorcolumn=80: */
-
diff --git a/agl-identity-service/src/aia-get.c b/agl-identity-service/src/aia-get.c
index 93d9470..56c82b0 100644
--- a/agl-identity-service/src/aia-get.c
+++ b/agl-identity-service/src/aia-get.c
@@ -66,13 +66,6 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static int curl_initialized = 0;
-
-
-
-
-
-
-
static void perform_query_callback(void *closure, int status, CURL *curl, const char *result, size_t size)
{
struct keyrequest *kr = closure;