| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "function-bearer_get_token.h" |
| 4 | #include "../v2/api_v2_calls.h" |
| 5 | |
| 6 | struct bearer_token_request { |
| 7 | nd_uuid_t claim_id; |
| 8 | nd_uuid_t machine_guid; |
| 9 | nd_uuid_t node_id; |
| 10 | HTTP_USER_ROLE user_role; |
| 11 | HTTP_ACCESS access; |
| 12 | nd_uuid_t cloud_account_id; |
| 13 | STRING *client_name; |
| 14 | }; |
| 15 | |
| 16 | static bool bearer_parse_json_payload(json_object *jobj, void *data, BUFFER *error) { |
| 17 | const char *path = ""; |
| 18 | struct bearer_token_request *rq = data; |
| 19 | JSONC_PARSE_TXT2UUID_OR_ERROR_AND_RETURN(jobj, path, "claim_id", rq->claim_id, error, JSONC_REQUIRED); |
| 20 | JSONC_PARSE_TXT2UUID_OR_ERROR_AND_RETURN(jobj, path, "machine_guid", rq->machine_guid, error, JSONC_REQUIRED); |
| 21 | JSONC_PARSE_TXT2UUID_OR_ERROR_AND_RETURN(jobj, path, "node_id", rq->node_id, error, JSONC_REQUIRED); |
| 22 | JSONC_PARSE_TXT2ENUM_OR_ERROR_AND_RETURN(jobj, path, "user_role", http_user_role2id, rq->user_role, error, JSONC_REQUIRED); |
| 23 | JSONC_PARSE_ARRAY_OF_TXT2BITMAP_OR_ERROR_AND_RETURN(jobj, path, "access", http_access2id_one, rq->access, error, JSONC_REQUIRED); |
| 24 | JSONC_PARSE_TXT2UUID_OR_ERROR_AND_RETURN(jobj, path, "cloud_account_id", rq->cloud_account_id, error, JSONC_REQUIRED); |
| 25 | JSONC_PARSE_TXT2STRING_OR_ERROR_AND_RETURN(jobj, path, "client_name", rq->client_name, error, JSONC_REQUIRED); |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | int function_bearer_get_token(BUFFER *wb, const char *function __maybe_unused, BUFFER *payload, const char *source) { |
| 30 | if(!user_auth_source_is_cloud(source)) |
| 31 | return rrd_call_function_error( |
| 32 | wb, "Bearer tokens can only be provided via NC.", HTTP_RESP_BAD_REQUEST); |
| 33 | |
| 34 | int code; |
| 35 | struct bearer_token_request rq = { 0 }; |
| 36 | CLEAN_JSON_OBJECT *jobj = json_parse_function_payload_or_error(wb, payload, &code, bearer_parse_json_payload, &rq); |
| 37 | if(!jobj || code != HTTP_RESP_OK) { |
| 38 | string_freez(rq.client_name); |
| 39 | return code; |
| 40 | } |
| 41 | |
| 42 | char claim_id[UUID_STR_LEN]; |
| 43 | uuid_unparse_lower(rq.claim_id, claim_id); |
| 44 | |
| 45 | char machine_guid[UUID_STR_LEN]; |
| 46 | uuid_unparse_lower(rq.machine_guid, machine_guid); |
| 47 | |
| 48 | char node_id[UUID_STR_LEN]; |
| 49 | uuid_unparse_lower(rq.node_id, node_id); |
| 50 | |
| 51 | int rc = bearer_get_token_json_response(wb, localhost, claim_id, machine_guid, node_id, |
| 52 | rq.user_role, rq.access, rq.cloud_account_id, |
| 53 | string2str(rq.client_name)); |
| 54 | |
| 55 | string_freez(rq.client_name); |
| 56 | return rc; |
| 57 | } |
| 58 | |
| 59 | int call_function_bearer_get_token(RRDHOST *host, struct web_client *w, const char *claim_id, const char *machine_guid, const char *node_id) { |
| 60 | CLEAN_BUFFER *payload = buffer_create(0, NULL); |
| 61 | buffer_json_initialize(payload, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_MINIFY); |
| 62 | buffer_json_member_add_string(payload, "claim_id", claim_id); |
| 63 | buffer_json_member_add_string(payload, "machine_guid", machine_guid); |
| 64 | buffer_json_member_add_string(payload, "node_id", node_id); |
| 65 | buffer_json_member_add_string(payload, "user_role", http_id2user_role(w->user_auth.user_role)); |
| 66 | http_access2buffer_json_array(payload, "access", w->user_auth.access); |
| 67 | buffer_json_member_add_uuid(payload, "cloud_account_id", w->user_auth.cloud_account_id.uuid); |
| 68 | buffer_json_member_add_string(payload, "client_name", w->user_auth.client_name); |
| 69 | buffer_json_finalize(payload); |
| 70 | |
| 71 | CLEAN_BUFFER *source = buffer_create(0, NULL); |
| 72 | user_auth_to_source_buffer(&w->user_auth, source); |
| 73 | |
| 74 | char transaction_str[UUID_COMPACT_STR_LEN]; |
| 75 | uuid_unparse_lower_compact(w->transaction, transaction_str); |
| 76 | return rrd_function_run(host, w->response.data, 10, |
| 77 | w->user_auth.access, RRDFUNCTIONS_BEARER_GET_TOKEN, true, |
| 78 | transaction_str, NULL, NULL, |
| 79 | NULL, NULL, |
| 80 | NULL, NULL, |
| 81 | payload, buffer_tostring(source), true); |
| 82 | } |