Commit ebfa39ba authored by Jozef Kralik's avatar Jozef Kralik Committed by Kishen Maloor

cloud: set expires_in to UINT16_MAX when is greater than UINT16_MAX

parent 13a474b3
Pipeline #1262 passed with stage
in 4 minutes and 56 seconds
......@@ -129,6 +129,25 @@ cloud_start_process(oc_cloud_context_t *ctx)
_oc_signal_event_loop();
}
static uint16_t
check_expires_in(int64_t expires_in)
{
if (expires_in <= 0) {
return 0;
}
if (expires_in > 60*60) {
// if time is more than 1h then set expires to (expires_in - 10min).
expires_in = expires_in - 10*60;
} else if (expires_in > 4*60) {
// if time is more than 240sec then set expires to (expires_in - 2min).
expires_in = expires_in - 2*60;
} else if (expires_in > 20) {
// if time is more than 20sec then set expires to (expires_in - 10sec).
expires_in = expires_in - 10;
}
return expires_in > UINT16_MAX ? UINT16_MAX : (uint16_t)expires_in;
}
static int
_register_handler(oc_cloud_context_t *ctx, oc_client_response_t *data)
{
......@@ -180,12 +199,10 @@ _register_handler(oc_cloud_context_t *ctx, oc_client_response_t *data)
}
int64_t expires_in = 0;
if (oc_rep_get_int(payload, EXPIRESIN_KEY, &expires_in) && expires_in > 0 &&
expires_in <= UINT16_MAX) {
oc_rep_get_int(payload, EXPIRESIN_KEY, &expires_in);
ctx->expires_in = check_expires_in(expires_in);
if (ctx->expires_in > 0) {
ctx->store.status |= OC_CLOUD_TOKEN_EXPIRY;
ctx->expires_in = (uint16_t)expires_in;
} else {
ctx->expires_in = 0;
}
cloud_store_dump_async(&ctx->store);
......@@ -410,13 +427,11 @@ _refresh_token_handler(oc_cloud_context_t *ctx, oc_client_response_t *data)
goto error;
}
ctx->expires_in = 0;
if (oc_rep_get_int(payload, EXPIRESIN_KEY, &expires_in)) {
if (expires_in > 0 && expires_in <= UINT16_MAX) {
ctx->expires_in = (uint16_t)expires_in;
oc_rep_get_int(payload, EXPIRESIN_KEY, &expires_in);
ctx->expires_in = check_expires_in(expires_in);
if (ctx->expires_in > 0) {
ctx->store.status |= OC_CLOUD_TOKEN_EXPIRY;
}
}
cloud_store_dump_async(&ctx->store);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment