< Summary - Backend C Tests - Coverage Report (WSL)

Information
Class: jwt_c
Assembly: src.backend.security
File(s): ./src/backend/security/jwt.c
Line coverage
62%
Covered lines: 59
Uncovered lines: 35
Coverable lines: 94
Total lines: 165
Line coverage: 62.7%
Branch coverage
58%
Covered branches: 27
Total branches: 46
Branch coverage: 58.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/18/2026 - 10:50:55 PM Line coverage: 62.7% (59/94) Branch coverage: 58.6% (27/46) Total lines: 165 2/18/2026 - 10:50:55 PM Line coverage: 62.7% (59/94) Branch coverage: 58.6% (27/46) Total lines: 165

File(s)

./src/backend/security/jwt.c

#LineLine coverage
 1#include "jwt.h"
 2#include "db_connection.h"
 3#include <cjson/cJSON.h>
 4#include <stdio.h>
 5#include <string.h>
 6#include <stdlib.h>
 7#include <time.h>
 8
 9
 10
 411char *generate_auth_token(int user_id, const char *username, const char *role) {
 412  if (!username || !role) {
 213    fprintf(stderr, "generate_auth_token: NULL parameter\n");
 214    return NULL;
 15  }
 16
 217  cJSON *json = cJSON_CreateObject();
 18
 219  if (!json) {
 020    fprintf(stderr, "generate_auth_token: Failed to create JSON\n");
 021    return NULL;
 22  }
 23
 224  cJSON_AddNumberToObject(json, "user_id", user_id);
 225  cJSON_AddStringToObject(json, "username", username);
 226  cJSON_AddStringToObject(json, "role", role);
 227  cJSON_AddNumberToObject(json, "exp", time(NULL) + 86400);
 228  char *json_str = cJSON_PrintUnformatted(json);
 229  cJSON_Delete(json);
 30
 231  if (!json_str) {
 032    fprintf(stderr, "generate_auth_token: Failed to print JSON\n");
 033    return NULL;
 34  }
 35
 236  char *token = malloc(strlen(json_str) + 20);
 37
 238  if (!token) {
 039    fprintf(stderr, "generate_auth_token: Memory allocation failed\n");
 040    free(json_str);
 041    return NULL;
 42  }
 43
 244  sprintf(token, "simple_jwt:%s", json_str);
 245  free(json_str);
 46  // LOG_DEBUG kaldırıldı
 247  return token;
 48}
 49
 650bool validate_auth_token(const char *token_str) {
 651  if (!token_str) {
 152    return false;
 53  }
 54
 555  if (strncmp(token_str, "simple_jwt:", 11) != 0) {
 256    return false;
 57  }
 58
 359  const char *json_str = token_str + 11;
 360  cJSON *json = cJSON_Parse(json_str);
 61
 362  if (!json) {
 163    return false;
 64  }
 65
 266  cJSON *exp_item = cJSON_GetObjectItem(json, "exp");
 67
 268  if (!cJSON_IsNumber(exp_item)) {
 169    cJSON_Delete(json);
 170    return false;
 71  }
 72
 173  time_t now = time(NULL);
 174  bool valid = (exp_item->valuedouble > now);
 175  cJSON_Delete(json);
 176  return valid;
 77}
 78
 379int get_user_id_from_token(const char *token_str) {
 380  if (!token_str || strncmp(token_str, "simple_jwt:", 11) != 0) {
 281    return -1;
 82  }
 83
 184  const char *json_str = token_str + 11;
 185  cJSON *json = cJSON_Parse(json_str);
 86
 187  if (!json) return -1;
 88
 189  cJSON *user_id_item = cJSON_GetObjectItem(json, "user_id");
 190  int user_id = -1;
 91
 192  if (cJSON_IsNumber(user_id_item)) {
 193    user_id = user_id_item->valueint;
 94  }
 95
 196  cJSON_Delete(json);
 197  return user_id;
 98}
 99
 3100char *get_role_from_token(const char *token_str) {
 3101  if (!token_str || strncmp(token_str, "simple_jwt:", 11) != 0) {
 2102    return NULL;
 103  }
 104
 1105  const char *json_str = token_str + 11;
 1106  cJSON *json = cJSON_Parse(json_str);
 107
 1108  if (!json) return NULL;
 109
 1110  cJSON *role_item = cJSON_GetObjectItem(json, "role");
 1111  char *role = NULL;
 112
 1113  if (cJSON_IsString(role_item)) {
 1114    role = strdup(role_item->valuestring);
 115  }
 116
 1117  cJSON_Delete(json);
 1118  return role;
 119}
 120
 0121int verify_user_credentials(const char *username, const char *password, char *out_role) {
 0122  if (!username || !password) {
 0123    fprintf(stderr, "verify_user_credentials: NULL parameter\n");
 0124    return -1;
 125  }
 126
 0127  DBConnection *conn_wrapper = db_pool_acquire();
 128
 0129  if (!conn_wrapper) {
 0130    fprintf(stderr, "verify_user_credentials: Failed to acquire DB connection\n");
 0131    return -1;
 132  }
 133
 134  char query[512];
 0135  snprintf(query, sizeof(query),
 136           "SELECT id, role FROM users WHERE username = '%s' AND password_hash = '%s';",
 137           username, password);
 0138  PGresult *res = PQexec(conn_wrapper->pg_conn, query);
 139
 0140  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
 0141    fprintf(stderr, "verify_user_credentials: Query failed for user: %s\n", username);
 0142    PQclear(res);
 0143    db_pool_release(conn_wrapper);
 0144    return -1;
 145  }
 146
 0147  if (PQntuples(res) == 0) {
 0148    fprintf(stderr, "verify_user_credentials: Login failed for user: %s\n", username);
 0149    PQclear(res);
 0150    db_pool_release(conn_wrapper);
 0151    return -1;
 152  }
 153
 0154  int uid = atoi(PQgetvalue(res, 0, 0));
 155
 0156  if (out_role) {
 0157    strncpy(out_role, PQgetvalue(res, 0, 1), 19);
 0158    out_role[19] = '\0';
 159  }
 160
 0161  PQclear(res);
 0162  db_pool_release(conn_wrapper);
 0163  printf("verify_user_credentials: User %s (ID: %d) successfully logged in\n", username, uid);
 0164  return uid;
 165}

Methods/Properties