From 4e34e7f6057d8775fcf18d65f8b878fedcc57b0e Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 3 Feb 2021 21:18:07 +0000 Subject: [PATCH] [mod_verto] auth-expires --- src/mod/endpoints/mod_verto/mod_verto.c | 28 +++++++++++++++++++++++++ src/mod/endpoints/mod_verto/mod_verto.h | 3 ++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/mod/endpoints/mod_verto/mod_verto.c b/src/mod/endpoints/mod_verto/mod_verto.c index 8174690fd1..f387e6ee8d 100644 --- a/src/mod/endpoints/mod_verto/mod_verto.c +++ b/src/mod/endpoints/mod_verto/mod_verto.c @@ -1056,7 +1056,9 @@ static switch_bool_t check_auth(jsock_t *jsock, cJSON *params, int *code, char * } else { switch_xml_t x_param, x_params; const char *use_passwd = NULL, *verto_context = NULL, *verto_dialplan = NULL; + time_t now = switch_epoch_time_now(NULL); + jsock->logintime = now; jsock->id = switch_core_strdup(jsock->pool, id); jsock->domain = switch_core_strdup(jsock->pool, domain); jsock->uid = switch_core_sprintf(jsock->pool, "%s@%s", id, domain); @@ -1103,6 +1105,18 @@ static switch_bool_t check_auth(jsock_t *jsock, cJSON *params, int *code, char * switch_event_add_header_string(jsock->vars, SWITCH_STACK_BOTTOM, var, val); switch_event_add_header_string(jsock->user_vars, SWITCH_STACK_BOTTOM, var, val); switch_mutex_unlock(jsock->flag_mutex); + + if (!strcmp(var, "login-expires")) { + uint32_t tmp = atol(val); + + if (tmp > now) { + jsock->exptime = tmp; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Login expire time for %s set to %ld seconds\n", jsock->uid, tmp - now); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid expire time for %s. Defaulting to 300 sec\n", jsock->uid); + jsock->exptime = now + 300; + } + } } } @@ -1950,11 +1964,21 @@ static void client_run(jsock_t *jsock) while(jsock->profile->running) { int pflags, poll_time = 50; + time_t now; if (!jsock->ws) { die("%s Setup Error\n", jsock->name); } pflags = kws_wait_sock(jsock->ws, poll_time, KS_POLL_READ); + if (jsock->exptime) { + now = switch_epoch_time_now(NULL); + + if (now >= jsock->exptime) { + die("%s Authentication Expired\n", jsock->uid); + } + + } + if (jsock->drop) { die("%s Dropping Connection\n", jsock->name); } if (pflags < 0 && (errno != EINTR)) { die_errnof("%s POLL FAILED with %d", jsock->name, pflags); } if (pflags == 0) {/* socket poll timeout */ jsock_check_event_queue(jsock); idle += poll_time;} else {idle = 0;} @@ -4403,6 +4427,10 @@ static switch_bool_t login_func(const char *method, cJSON *params, jsock_t *jsoc *response = cJSON_CreateObject(); cJSON_AddItemToObject(*response, "message", cJSON_CreateString("logged in")); + if (jsock->exptime) { + cJSON_AddItemToObject(*response, "auth-expires", cJSON_CreateNumber(jsock->exptime)); + } + switch_mutex_lock(jsock->flag_mutex); if ((var = switch_event_get_header(jsock->vars, "moderator")) && switch_true(var)) { cJSON_AddItemToObject(*response, "moderator", cJSON_CreateTrue()); diff --git a/src/mod/endpoints/mod_verto/mod_verto.h b/src/mod/endpoints/mod_verto/mod_verto.h index 112fb09ae6..6d48a922c3 100644 --- a/src/mod/endpoints/mod_verto/mod_verto.h +++ b/src/mod/endpoints/mod_verto/mod_verto.h @@ -140,7 +140,8 @@ struct jsock_s { char remote_host[256]; int remote_port; int family; - + time_t exptime; + time_t logintime; struct verto_profile_s *profile; switch_thread_rwlock_t *rwlock;