ace_auth_plug/token-cache.c
/* access is desired read/write access
 * granted is what Mosquitto auth-plug actually granted
 */
access is desired read/write access
granted is what Mosquitto auth-plug actually granted
access is desired read / write access granted is what Mosquitto auth - plug actually granted
void token_cache(const char *token, const char *topic, int access[2], time_t exp)
{
	char hex[SHA256_DIGEST_LENGTH];
	struct tokencache *a, *tmp;
	time_t now;
	if (!token) {
                return;
        }
	now = time(NULL);
	if (exp-now <= 0) {
		return;
	}
	_log(LOG_DEBUG, "Received parameters token: %s", token);
	_log(LOG_DEBUG, "Received parameters topic:%s", topic);
	_log(LOG_DEBUG, "Received parameters exp: %s", ctime(&exp));
	strncpy(hex, token, sizeof(hex)); 
	hex[sizeof(hex)-1]='\0';
	HASH_FIND_STR(cache, hex, a);
	if (a) {
		a->cachedTokenParams.active = true;
		free(a->cachedTokenParams.topic);
		a->cachedTokenParams.topic = malloc(strlen(topic));
		strcpy(a->cachedTokenParams.topic, topic); 
		for(int i=0; i<2;i++){
			a->cachedTokenParams.access[i] = access[i];
		}
		a->cachedTokenParams.exp = exp;
	} else {
		a = (struct tokencache *)malloc(sizeof(struct tokencache));		
		strcpy(a->hex, hex);
		a->cachedTokenParams.active = true;
		a->cachedTokenParams.topic = malloc(strlen(topic));
                strcpy(a->cachedTokenParams.topic, topic);
		a->cachedTokenParams.exp = exp;
		for(int i=0; i<2;i++){
                        a->cachedTokenParams.access[i] = access[i];
                }
		HASH_ADD_STR(cache, hex, a);
		_log(LOG_DEBUG, "Token cache cached  [%s] for %s)", hex, topic);
	}
	HASH_ITER(hh, cache, a, tmp) {
		if (now > a->cachedTokenParams.exp) {
			_log(LOG_DEBUG, " Cleanup [%s]", a->hex);
			HASH_DEL(cache, a);
			free(a);
		}
	}
	return;
}
void token_cache ( const char * token , const char * topic , int access [ 2 ] , time_t exp ) { char hex [ SHA256_DIGEST_LENGTH ] ; struct tokencache * a , * tmp ; time_t now ; if ( ! token ) { return ; } now = time ( NULL ) ; if ( exp - now <= 0 ) { return ; } _log ( LOG_DEBUG , " " , token ) ; _log ( LOG_DEBUG , " " , topic ) ; _log ( LOG_DEBUG , " " , ctime ( & exp ) ) ; strncpy ( hex , token , sizeof ( hex ) ) ; hex [ sizeof ( hex ) - 1 ] = ' \0 ' ; HASH_FIND_STR ( cache , hex , a ) ; if ( a ) { a -> cachedTokenParams . active = true ; free ( a -> cachedTokenParams . topic ) ; a -> cachedTokenParams . topic = malloc ( strlen ( topic ) ) ; strcpy ( a -> cachedTokenParams . topic , topic ) ; for ( int i = 0 ; i < 2 ; i ++ ) { a -> cachedTokenParams . access [ i ] = access [ i ] ; } a -> cachedTokenParams . exp = exp ; } else { a = ( struct tokencache * ) malloc ( sizeof ( struct tokencache ) ) ; strcpy ( a -> hex , hex ) ; a -> cachedTokenParams . active = true ; a -> cachedTokenParams . topic = malloc ( strlen ( topic ) ) ; strcpy ( a -> cachedTokenParams . topic , topic ) ; a -> cachedTokenParams . exp = exp ; for ( int i = 0 ; i < 2 ; i ++ ) { a -> cachedTokenParams . access [ i ] = access [ i ] ; } HASH_ADD_STR ( cache , hex , a ) ; _log ( LOG_DEBUG , " " , hex , topic ) ; } HASH_ITER ( hh , cache , a , tmp ) { if ( now > a -> cachedTokenParams . exp ) { _log ( LOG_DEBUG , " " , a -> hex ) ; HASH_DEL ( cache , a ) ; free ( a ) ; } } return ; }
access is desired read/write access
 granted is what Mosquitto auth-plug actually granted
access is desired read / write access granted is what Mosquitto auth - plug actually granted
//token length based on sha256 digest //No token, nothing to cache //Sanity check for expiration time //all cached tokens are active /*
	 * Check whole cache for items which need deleting. Important with
	 * clients who show up once only (mosquitto_[sp]ub with variable clientIDs)
	 */