@cryptotaxi247 / netdata-1 / commits / 748de8de2

add expiration to bearer token response (#15392)

Costa Tsaousis committed Jul 14, 2023 at 11:27 UTC 748de8de2d9aa7f360e70fbc002c2083ebcea217
1 file changed +8 -3
web/api/web_api_v2.c
+8 -3
@@ -3,6 +3,8 @@
3 #include "web_api_v2.h"
4 #include "../rtc/webrtc.h"
5
6 +#define BEARER_TOKEN_EXPIRATION 86400
7 +
8 struct bearer_token {
9 time_t created_s;
10 time_t expires_s;
@@ -26,7 +28,7 @@ static void bearer_token_cleanup(void) {
28 dictionary_garbage_collect(netdata_authorized_bearers);
29 }
30
29 -static void bearer_get_token(uuid_t *uuid) {
31 +static time_t bearer_get_token(uuid_t *uuid) {
32 static SPINLOCK spinlock = NETDATA_SPINLOCK_INITIALIZER;
33 static bool initialized = false;
34
@@ -50,10 +52,12 @@ static void bearer_get_token(uuid_t *uuid) {
52 z = dictionary_set(netdata_authorized_bearers, uuid_str, &t, sizeof(t));
53 if(!z->created_s) {
54 z->created_s = now_monotonic_sec();
53 - z->expires_s = z->created_s + 86400;
55 + z->expires_s = z->created_s + BEARER_TOKEN_EXPIRATION;
56 }
57
58 bearer_token_cleanup();
59 +
60 + return now_realtime_sec() + BEARER_TOKEN_EXPIRATION;
61 }
62
63 #define HTTP_REQUEST_AUTHORIZATION_BEARER "\r\nAuthorization: Bearer "
@@ -192,7 +196,7 @@ int api_v2_bearer_token(RRDHOST *host __maybe_unused, struct web_client *w __may
196 }
197
198 uuid_t uuid;
195 - bearer_get_token(&uuid);
199 + time_t expires_s = bearer_get_token(&uuid);
200
201 BUFFER *wb = w->response.data;
202 buffer_flush(wb);
@@ -200,6 +204,7 @@ int api_v2_bearer_token(RRDHOST *host __maybe_unused, struct web_client *w __may
204 buffer_json_member_add_string(wb, "mg", localhost->machine_guid);
205 buffer_json_member_add_boolean(wb, "bearer_protection", netdata_is_protected_by_bearer);
206 buffer_json_member_add_uuid(wb, "token", &uuid);
207 + buffer_json_member_add_time_t(wb, "expiration", expires_s);
208 buffer_json_finalize(wb);
209
210 return HTTP_RESP_OK;