|
8 | 8 |
|
9 | 9 | #include "callbacks.h"
|
10 | 10 | #include <stdio.h>
|
| 11 | +#include <stdlib.h> |
11 | 12 | #include <string.h>
|
12 |
| -#include <stdlib.h> |
| 13 | +#include "mcl_core/mcl_file_util.h" |
| 14 | +#include "mcl_core/mcl_string_util.h" |
| 15 | +#include "mcl_core/mcl_memory.h" |
13 | 16 |
|
14 |
| -static const char *credentials_file_name = "credentialsFile.txt"; |
| 17 | +static const char *credentials_file_name = "credentials.txt"; |
15 | 18 |
|
16 |
| -mcl_error_t custom_load_function(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri) |
| 19 | +#define MAX_DIGIT_FOR_PARAMETER_LENGTH 4 |
| 20 | + |
| 21 | +static mcl_error_t read_parameter_from_file(char **parameter, void *file_descriptor); |
| 22 | +static mcl_error_t save_parameter_to_file(const char *parameter, void *file_descriptor); |
| 23 | + |
| 24 | +mcl_error_t custom_load_function_shared_secret(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri) |
| 25 | +{ |
| 26 | + // It is strongly recommended to use callback functions with encryption for loading/saving credentials. |
| 27 | + void *file_descriptor = NULL; |
| 28 | + mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "r", &file_descriptor); |
| 29 | + |
| 30 | + if (MCL_OK == code) |
| 31 | + { |
| 32 | + code = read_parameter_from_file(client_id, file_descriptor); |
| 33 | + } |
| 34 | + |
| 35 | + if (MCL_OK == code) |
| 36 | + { |
| 37 | + code = read_parameter_from_file(client_secret, file_descriptor); |
| 38 | + } |
| 39 | + |
| 40 | + if (MCL_OK == code) |
| 41 | + { |
| 42 | + code = read_parameter_from_file(registration_access_token, file_descriptor); |
| 43 | + } |
| 44 | + |
| 45 | + if (MCL_OK == code) |
| 46 | + { |
| 47 | + code = read_parameter_from_file(registration_uri, file_descriptor); |
| 48 | + } |
| 49 | + |
| 50 | + if (MCL_OK != code) |
| 51 | + { |
| 52 | + MCL_FREE(*client_id); |
| 53 | + MCL_FREE(*client_secret); |
| 54 | + MCL_FREE(*registration_access_token); |
| 55 | + MCL_FREE(*registration_uri); |
| 56 | + |
| 57 | + code = MCL_CREDENTIALS_NOT_LOADED; |
| 58 | + } |
| 59 | + |
| 60 | + (void) mcl_file_util_fclose(file_descriptor); |
| 61 | + |
| 62 | + return code; |
| 63 | +} |
| 64 | + |
| 65 | +mcl_error_t custom_save_function_shared_secret(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri) |
| 66 | +{ |
| 67 | + void *file_descriptor = NULL; |
| 68 | + mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "w", &file_descriptor); |
| 69 | + |
| 70 | + if (MCL_OK == code) |
| 71 | + { |
| 72 | + save_parameter_to_file(client_id, file_descriptor); |
| 73 | + } |
| 74 | + |
| 75 | + if (MCL_OK == code) |
| 76 | + { |
| 77 | + save_parameter_to_file(client_secret, file_descriptor); |
| 78 | + } |
| 79 | + |
| 80 | + if (MCL_OK == code) |
| 81 | + { |
| 82 | + save_parameter_to_file(registration_access_token, file_descriptor); |
| 83 | + } |
| 84 | + |
| 85 | + if (MCL_OK == code) |
| 86 | + { |
| 87 | + save_parameter_to_file(registration_uri, file_descriptor); |
| 88 | + } |
| 89 | + |
| 90 | + if (MCL_OK != code) |
| 91 | + { |
| 92 | + // In real application, user may try to save more aggressively instead of giving up. |
| 93 | + code = MCL_CREDENTIALS_NOT_SAVED; |
| 94 | + } |
| 95 | + |
| 96 | + (void) mcl_file_util_fclose(file_descriptor); |
| 97 | + |
| 98 | + return code; |
| 99 | +} |
| 100 | + |
| 101 | +mcl_error_t custom_load_function_rsa(char **client_id, char **public_key, char **private_key, char **registration_access_token, char **registration_uri) |
17 | 102 | {
|
18 | 103 | // It is strongly recommended to use callback functions with encryption for loading/saving credentials.
|
19 |
| - FILE *fd = fopen(credentials_file_name, "r"); |
| 104 | + void *file_descriptor = NULL; |
| 105 | + mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "r", &file_descriptor); |
20 | 106 |
|
21 |
| - if (NULL == fd) |
| 107 | + if (MCL_OK == code) |
22 | 108 | {
|
23 |
| - *client_id = NULL; |
24 |
| - *client_secret = NULL; |
25 |
| - *registration_access_token = NULL; |
26 |
| - *registration_uri = NULL; |
27 |
| - printf("[INFO] : Absence of the file means we are not onboarded yet, initial access token will be used to onboard.\n"); |
28 |
| - return MCL_CREDENTIALS_NOT_LOADED; |
| 109 | + code = read_parameter_from_file(client_id, file_descriptor); |
29 | 110 | }
|
30 | 111 |
|
31 |
| - char string_length[5] = {0}; |
32 |
| - int size_to_allocate; |
| 112 | + if (MCL_OK == code) |
| 113 | + { |
| 114 | + code = read_parameter_from_file(public_key, file_descriptor); |
| 115 | + } |
33 | 116 |
|
34 |
| - fgets(string_length, 5, fd); |
35 |
| - size_to_allocate = atoi(string_length) + 1; |
36 |
| - *client_id = calloc(size_to_allocate, 1); |
37 |
| - fgets(*client_id, size_to_allocate, fd); |
| 117 | + if (MCL_OK == code) |
| 118 | + { |
| 119 | + code = read_parameter_from_file(private_key, file_descriptor); |
| 120 | + } |
38 | 121 |
|
39 |
| - fgets(string_length, 5, fd); |
40 |
| - size_to_allocate = atoi(string_length) + 1; |
41 |
| - *client_secret = calloc(size_to_allocate, 1); |
42 |
| - fgets(*client_secret, size_to_allocate, fd); |
| 122 | + if (MCL_OK == code) |
| 123 | + { |
| 124 | + code = read_parameter_from_file(registration_access_token, file_descriptor); |
| 125 | + } |
43 | 126 |
|
44 |
| - fgets(string_length, 5, fd); |
45 |
| - size_to_allocate = atoi(string_length) + 1; |
46 |
| - *registration_access_token = calloc(size_to_allocate, 1); |
47 |
| - fgets(*registration_access_token, size_to_allocate, fd); |
| 127 | + if (MCL_OK == code) |
| 128 | + { |
| 129 | + code = read_parameter_from_file(registration_uri, file_descriptor); |
| 130 | + } |
48 | 131 |
|
49 |
| - fgets(string_length, 5, fd); |
50 |
| - size_to_allocate = atoi(string_length) + 1; |
51 |
| - *registration_uri = calloc(size_to_allocate, 1); |
52 |
| - fgets(*registration_uri, size_to_allocate, fd); |
| 132 | + if (MCL_OK != code) |
| 133 | + { |
| 134 | + MCL_FREE(*client_id); |
| 135 | + MCL_FREE(*public_key); |
| 136 | + MCL_FREE(*private_key); |
| 137 | + MCL_FREE(*registration_access_token); |
| 138 | + MCL_FREE(*registration_uri); |
53 | 139 |
|
54 |
| - fclose(fd); |
55 |
| - return MCL_OK; |
| 140 | + code = MCL_CREDENTIALS_NOT_LOADED; |
| 141 | + } |
| 142 | + |
| 143 | + (void) mcl_file_util_fclose(file_descriptor); |
| 144 | + |
| 145 | + return code; |
56 | 146 | }
|
57 | 147 |
|
58 |
| -mcl_error_t custom_save_function(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri) |
| 148 | +mcl_error_t custom_save_function_rsa(const char *client_id, const char *public_key, const char *private_key, const char *registration_access_token, const char *registration_uri) |
59 | 149 | {
|
60 |
| - FILE *fd = fopen(credentials_file_name, "w"); |
| 150 | + void *file_descriptor = NULL; |
| 151 | + mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "w", &file_descriptor); |
| 152 | + |
| 153 | + if (MCL_OK == code) |
| 154 | + { |
| 155 | + save_parameter_to_file(client_id, file_descriptor); |
| 156 | + } |
| 157 | + |
| 158 | + if (MCL_OK == code) |
| 159 | + { |
| 160 | + save_parameter_to_file(public_key, file_descriptor); |
| 161 | + } |
| 162 | + |
| 163 | + if (MCL_OK == code) |
| 164 | + { |
| 165 | + save_parameter_to_file(private_key, file_descriptor); |
| 166 | + } |
61 | 167 |
|
62 |
| - if (NULL == fd) |
| 168 | + if (MCL_OK == code) |
63 | 169 | {
|
64 |
| - printf("[ERROR] : File cannot be opened or truncated to save the credentials.\n"); |
65 |
| - return MCL_CREDENTIALS_NOT_SAVED; |
| 170 | + save_parameter_to_file(registration_access_token, file_descriptor); |
66 | 171 | }
|
67 | 172 |
|
68 |
| - char string_length[5] = {0}; |
| 173 | + if (MCL_OK == code) |
| 174 | + { |
| 175 | + save_parameter_to_file(registration_uri, file_descriptor); |
| 176 | + } |
69 | 177 |
|
70 |
| - snprintf(string_length, 5, "%04u", (unsigned int) strlen(client_id)); |
71 |
| - fputs(string_length, fd); |
72 |
| - fputs(client_id, fd); |
| 178 | + if (MCL_OK != code) |
| 179 | + { |
| 180 | + // In real application, user may try to save more aggressively instead of giving up. |
| 181 | + code = MCL_CREDENTIALS_NOT_SAVED; |
| 182 | + } |
73 | 183 |
|
74 |
| - snprintf(string_length, 5, "%04u", (unsigned int) strlen(client_secret)); |
75 |
| - fputs(string_length, fd); |
76 |
| - fputs(client_secret, fd); |
| 184 | + (void) mcl_file_util_fclose(file_descriptor); |
77 | 185 |
|
78 |
| - snprintf(string_length, 5, "%04u", (unsigned int) strlen(registration_access_token)); |
79 |
| - fputs(string_length, fd); |
80 |
| - fputs(registration_access_token, fd); |
| 186 | + return code; |
| 187 | +} |
| 188 | + |
| 189 | +static mcl_error_t read_parameter_from_file(char **parameter, void *file_descriptor) |
| 190 | +{ |
| 191 | + mcl_error_t code; |
| 192 | + char length_string[MAX_DIGIT_FOR_PARAMETER_LENGTH + 1] = {0}; |
| 193 | + int length; |
| 194 | + |
| 195 | + *parameter = NULL; |
| 196 | + code = mcl_file_util_fgets(length_string, MAX_DIGIT_FOR_PARAMETER_LENGTH + 1, file_descriptor); |
| 197 | + |
| 198 | + if (MCL_OK == code) |
| 199 | + { |
| 200 | + length = atoi(length_string); |
| 201 | + *parameter = MCL_CALLOC(length + 1, 1); |
| 202 | + |
| 203 | + if (NULL != *parameter) |
| 204 | + { |
| 205 | + mcl_size_t actual_count = 0; |
| 206 | + mcl_file_util_fread(*parameter, 1, length, file_descriptor, &actual_count); |
| 207 | + |
| 208 | + if (actual_count != (unsigned) length) |
| 209 | + { |
| 210 | + code = MCL_FAIL; |
| 211 | + MCL_FREE(*parameter); |
| 212 | + } |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + code = MCL_OUT_OF_MEMORY; |
| 217 | + } |
| 218 | + } |
81 | 219 |
|
82 |
| - snprintf(string_length, 5, "%04u", (unsigned int) strlen(registration_uri)); |
83 |
| - fputs(string_length, fd); |
84 |
| - fputs(registration_uri, fd); |
| 220 | + return code; |
| 221 | +} |
| 222 | + |
| 223 | +static mcl_error_t save_parameter_to_file(const char *parameter, void *file_descriptor) |
| 224 | +{ |
| 225 | + mcl_error_t code; |
| 226 | + char length_string[MAX_DIGIT_FOR_PARAMETER_LENGTH + 1] = {0}; |
| 227 | + mcl_size_t parameter_length = mcl_string_util_strlen(parameter); |
| 228 | + |
| 229 | + code = mcl_string_util_snprintf(length_string, MAX_DIGIT_FOR_PARAMETER_LENGTH + 1, "%04u", (unsigned int) parameter_length); |
| 230 | + |
| 231 | + if (MCL_OK == code) |
| 232 | + { |
| 233 | + code = mcl_file_util_fwrite(length_string, 1, MAX_DIGIT_FOR_PARAMETER_LENGTH, file_descriptor); |
| 234 | + } |
| 235 | + |
| 236 | + if (MCL_OK == code) |
| 237 | + { |
| 238 | + code = mcl_file_util_fwrite(parameter, 1, parameter_length, file_descriptor); |
| 239 | + } |
85 | 240 |
|
86 |
| - fclose(fd); |
87 |
| - return MCL_OK; |
| 241 | + return code; |
88 | 242 | }
|
0 commit comments