X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-hreq.c;h=a9010b8873abf20e103c59bba57d5f02fa366f3d;hb=e08d57c0e397018f0c463a66adc232f6358caef5;hp=495bf06c24c94e23430684fe55c20062c5536fcb;hpb=77ca8b40f2d0c8b1cbf9960bd5a5b2aec36fef38;p=src%2Fapp-framework-binder.git diff --git a/src/afb-hreq.c b/src/afb-hreq.c index 495bf06c..a9010b88 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017, 2018 "IoT.bzh" + * Copyright (C) 2016-2019 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,9 @@ static const char short_key_for_token[] = "token"; static const char long_key_for_reqid[] = "x-afb-reqid"; static const char short_key_for_reqid[] = "reqid"; +static const char key_for_bearer[] = "Bearer"; +static const char key_for_access_token[] = "access_token"; + static char *cookie_name = NULL; static char *cookie_setter = NULL; static char *tmp_pattern = NULL; @@ -162,7 +166,8 @@ static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_ MHD_add_response_header(response, k, v); k = va_arg(args, const char *); } - v = afb_context_sent_uuid(&hreq->xreq.context); + + v = afb_context_uuid(&hreq->xreq.context); if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) { MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie); free(cookie); @@ -735,6 +740,23 @@ const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name) return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name); } +const char *afb_hreq_get_authorization_bearer(struct afb_hreq *hreq) +{ + const char *value = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_AUTHORIZATION); + if (value) { + if (strncasecmp(value, key_for_bearer, sizeof key_for_bearer - 1) == 0) { + value += sizeof key_for_bearer - 1; + if (isblank(*value++)) { + while (isblank(*value)) + value++; + if (*value) + return value; + } + } + } + return NULL; +} + int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size) { void *p; @@ -948,19 +970,30 @@ int afb_hreq_init_context(struct afb_hreq *hreq) if (hreq->xreq.context.session != NULL) return 0; + /* get the uuid of the session */ uuid = afb_hreq_get_header(hreq, long_key_for_uuid); - if (uuid == NULL) + if (uuid == NULL) { uuid = afb_hreq_get_argument(hreq, long_key_for_uuid); - if (uuid == NULL) - uuid = afb_hreq_get_cookie(hreq, cookie_name); - if (uuid == NULL) - uuid = afb_hreq_get_argument(hreq, short_key_for_uuid); - - token = afb_hreq_get_header(hreq, long_key_for_token); - if (token == NULL) - token = afb_hreq_get_argument(hreq, long_key_for_token); - if (token == NULL) - token = afb_hreq_get_argument(hreq, short_key_for_token); + if (uuid == NULL) { + uuid = afb_hreq_get_cookie(hreq, cookie_name); + if (uuid == NULL) + uuid = afb_hreq_get_argument(hreq, short_key_for_uuid); + } + } + + /* get the authorisation token */ + token = afb_hreq_get_authorization_bearer(hreq); + if (token == NULL) { + token = afb_hreq_get_argument(hreq, key_for_access_token); + if (token == NULL) { + token = afb_hreq_get_header(hreq, long_key_for_token); + if (token == NULL) { + token = afb_hreq_get_argument(hreq, long_key_for_token); + if (token == NULL) + token = afb_hreq_get_argument(hreq, short_key_for_token); + } + } + } return afb_context_connect(&hreq->xreq.context, uuid, token); }