| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "user-auth.h" |
| 4 | |
| 5 | // Define the mapping between enum values and strings |
| 6 | ENUM_STR_MAP_DEFINE(USER_AUTH_METHOD) = { |
| 7 | {USER_AUTH_METHOD_NONE, "none"}, |
| 8 | {USER_AUTH_METHOD_CLOUD, "NC"}, |
| 9 | {USER_AUTH_METHOD_BEARER, "api-bearer"}, |
| 10 | {USER_AUTH_METHOD_GOD, "god"}, |
| 11 | |
| 12 | // terminator |
| 13 | {0, NULL}, |
| 14 | }; |
| 15 | |
| 16 | // Define the functions to convert between enum values and strings |
| 17 | ENUM_STR_DEFINE_FUNCTIONS(USER_AUTH_METHOD, USER_AUTH_METHOD_NONE, "none") |
| 18 | |
| 19 | bool user_auth_source_is_cloud(const char *source) { |
| 20 | return source && *source && strstartswith(source, "method=NC,"); |
| 21 | } |
| 22 | |
| 23 | void user_auth_to_source_buffer(USER_AUTH *user_auth, BUFFER *source) { |
| 24 | buffer_reset(source); |
| 25 | |
| 26 | buffer_sprintf(source, "method=%s", USER_AUTH_METHOD_2str(user_auth->method)); |
| 27 | buffer_sprintf(source, ",role=%s", user_auth->method == USER_AUTH_METHOD_GOD ? "god" : http_id2user_role(user_auth->user_role)); |
| 28 | buffer_sprintf(source, ",permissions="HTTP_ACCESS_FORMAT, (HTTP_ACCESS_FORMAT_CAST)user_auth->access); |
| 29 | |
| 30 | if(user_auth->client_name[0]) |
| 31 | buffer_sprintf(source, ",user=%s", user_auth->client_name); |
| 32 | |
| 33 | if(!uuid_is_null(user_auth->cloud_account_id.uuid)) { |
| 34 | char uuid_str[UUID_COMPACT_STR_LEN]; |
| 35 | uuid_unparse_lower_compact(user_auth->cloud_account_id.uuid, uuid_str); |
| 36 | buffer_sprintf(source, ",account=%s", uuid_str); |
| 37 | } |
| 38 | |
| 39 | if(user_auth->client_ip[0]) |
| 40 | buffer_sprintf(source, ",ip=%s", user_auth->client_ip); |
| 41 | |
| 42 | if(user_auth->forwarded_for[0]) |
| 43 | buffer_sprintf(source, ",forwarded_for=%s", user_auth->forwarded_for); |
| 44 | } |
| 45 | |
| 46 | bool user_auth_from_source(const char *src, USER_AUTH *parsed) { |
| 47 | char *buf, *token, *saveptr; |
| 48 | |
| 49 | if (!src || !parsed) |
| 50 | return false; |
| 51 | |
| 52 | // Initialize the structure. |
| 53 | memset(parsed, 0, sizeof(*parsed)); |
| 54 | |
| 55 | // Duplicate the source string because strtok_r modifies it. |
| 56 | buf = strdupz(src); |
| 57 | token = strtok_r(buf, ",", &saveptr); |
| 58 | while (token) { |
| 59 | char *equal_sign = strchr(token, '='); |
| 60 | if (equal_sign) { |
| 61 | *equal_sign = '\0'; |
| 62 | const char *key = token; |
| 63 | const char *value = equal_sign + 1; |
| 64 | |
| 65 | if (strcmp(key, "method") == 0) { |
| 66 | parsed->method = USER_AUTH_METHOD_2id(value); |
| 67 | } |
| 68 | else if (strcmp(key, "role") == 0) { |
| 69 | if (strcmp(value, "god") == 0) |
| 70 | parsed->method = USER_AUTH_METHOD_GOD; |
| 71 | else |
| 72 | parsed->user_role = http_user_role2id(value); |
| 73 | } |
| 74 | else if (strcmp(key, "permissions") == 0) |
| 75 | parsed->access = http_access_from_hex_str(value); |
| 76 | |
| 77 | else if (strcmp(key, "user") == 0) { |
| 78 | strncpyz(parsed->client_name, value, CLOUD_CLIENT_NAME_LENGTH - 1); |
| 79 | } |
| 80 | else if (strcmp(key, "account") == 0) { |
| 81 | if (uuid_parse(value, parsed->cloud_account_id.uuid) != 0) |
| 82 | parsed->cloud_account_id = UUID_ZERO; |
| 83 | } |
| 84 | else if (strcmp(key, "ip") == 0) { |
| 85 | strncpyz(parsed->client_ip, value, INET6_ADDRSTRLEN - 1); |
| 86 | } |
| 87 | else if (strcmp(key, "forwarded_for") == 0) { |
| 88 | strncpyz(parsed->forwarded_for, value, INET6_ADDRSTRLEN - 1); |
| 89 | } |
| 90 | } |
| 91 | token = strtok_r(NULL, ",", &saveptr); |
| 92 | } |
| 93 | |
| 94 | freez(buf); |
| 95 | return true; |
| 96 | } |