| | | 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 | | |
| | 4 | 11 | | char *generate_auth_token(int user_id, const char *username, const char *role) { |
| | 4 | 12 | | if (!username || !role) { |
| | 2 | 13 | | fprintf(stderr, "generate_auth_token: NULL parameter\n"); |
| | 2 | 14 | | return NULL; |
| | | 15 | | } |
| | | 16 | | |
| | 2 | 17 | | cJSON *json = cJSON_CreateObject(); |
| | | 18 | | |
| | 2 | 19 | | if (!json) { |
| | 0 | 20 | | fprintf(stderr, "generate_auth_token: Failed to create JSON\n"); |
| | 0 | 21 | | return NULL; |
| | | 22 | | } |
| | | 23 | | |
| | 2 | 24 | | cJSON_AddNumberToObject(json, "user_id", user_id); |
| | 2 | 25 | | cJSON_AddStringToObject(json, "username", username); |
| | 2 | 26 | | cJSON_AddStringToObject(json, "role", role); |
| | 2 | 27 | | cJSON_AddNumberToObject(json, "exp", time(NULL) + 86400); |
| | 2 | 28 | | char *json_str = cJSON_PrintUnformatted(json); |
| | 2 | 29 | | cJSON_Delete(json); |
| | | 30 | | |
| | 2 | 31 | | if (!json_str) { |
| | 0 | 32 | | fprintf(stderr, "generate_auth_token: Failed to print JSON\n"); |
| | 0 | 33 | | return NULL; |
| | | 34 | | } |
| | | 35 | | |
| | 2 | 36 | | char *token = malloc(strlen(json_str) + 20); |
| | | 37 | | |
| | 2 | 38 | | if (!token) { |
| | 0 | 39 | | fprintf(stderr, "generate_auth_token: Memory allocation failed\n"); |
| | 0 | 40 | | free(json_str); |
| | 0 | 41 | | return NULL; |
| | | 42 | | } |
| | | 43 | | |
| | 2 | 44 | | sprintf(token, "simple_jwt:%s", json_str); |
| | 2 | 45 | | free(json_str); |
| | | 46 | | // LOG_DEBUG kaldırıldı |
| | 2 | 47 | | return token; |
| | | 48 | | } |
| | | 49 | | |
| | 6 | 50 | | bool validate_auth_token(const char *token_str) { |
| | 6 | 51 | | if (!token_str) { |
| | 1 | 52 | | return false; |
| | | 53 | | } |
| | | 54 | | |
| | 5 | 55 | | if (strncmp(token_str, "simple_jwt:", 11) != 0) { |
| | 2 | 56 | | return false; |
| | | 57 | | } |
| | | 58 | | |
| | 3 | 59 | | const char *json_str = token_str + 11; |
| | 3 | 60 | | cJSON *json = cJSON_Parse(json_str); |
| | | 61 | | |
| | 3 | 62 | | if (!json) { |
| | 1 | 63 | | return false; |
| | | 64 | | } |
| | | 65 | | |
| | 2 | 66 | | cJSON *exp_item = cJSON_GetObjectItem(json, "exp"); |
| | | 67 | | |
| | 2 | 68 | | if (!cJSON_IsNumber(exp_item)) { |
| | 1 | 69 | | cJSON_Delete(json); |
| | 1 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | 1 | 73 | | time_t now = time(NULL); |
| | 1 | 74 | | bool valid = (exp_item->valuedouble > now); |
| | 1 | 75 | | cJSON_Delete(json); |
| | 1 | 76 | | return valid; |
| | | 77 | | } |
| | | 78 | | |
| | 3 | 79 | | int get_user_id_from_token(const char *token_str) { |
| | 3 | 80 | | if (!token_str || strncmp(token_str, "simple_jwt:", 11) != 0) { |
| | 2 | 81 | | return -1; |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | const char *json_str = token_str + 11; |
| | 1 | 85 | | cJSON *json = cJSON_Parse(json_str); |
| | | 86 | | |
| | 1 | 87 | | if (!json) return -1; |
| | | 88 | | |
| | 1 | 89 | | cJSON *user_id_item = cJSON_GetObjectItem(json, "user_id"); |
| | 1 | 90 | | int user_id = -1; |
| | | 91 | | |
| | 1 | 92 | | if (cJSON_IsNumber(user_id_item)) { |
| | 1 | 93 | | user_id = user_id_item->valueint; |
| | | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | cJSON_Delete(json); |
| | 1 | 97 | | return user_id; |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | char *get_role_from_token(const char *token_str) { |
| | 3 | 101 | | if (!token_str || strncmp(token_str, "simple_jwt:", 11) != 0) { |
| | 2 | 102 | | return NULL; |
| | | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | const char *json_str = token_str + 11; |
| | 1 | 106 | | cJSON *json = cJSON_Parse(json_str); |
| | | 107 | | |
| | 1 | 108 | | if (!json) return NULL; |
| | | 109 | | |
| | 1 | 110 | | cJSON *role_item = cJSON_GetObjectItem(json, "role"); |
| | 1 | 111 | | char *role = NULL; |
| | | 112 | | |
| | 1 | 113 | | if (cJSON_IsString(role_item)) { |
| | 1 | 114 | | role = strdup(role_item->valuestring); |
| | | 115 | | } |
| | | 116 | | |
| | 1 | 117 | | cJSON_Delete(json); |
| | 1 | 118 | | return role; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | int verify_user_credentials(const char *username, const char *password, char *out_role) { |
| | 0 | 122 | | if (!username || !password) { |
| | 0 | 123 | | fprintf(stderr, "verify_user_credentials: NULL parameter\n"); |
| | 0 | 124 | | return -1; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 128 | | |
| | 0 | 129 | | if (!conn_wrapper) { |
| | 0 | 130 | | fprintf(stderr, "verify_user_credentials: Failed to acquire DB connection\n"); |
| | 0 | 131 | | return -1; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | char query[512]; |
| | 0 | 135 | | snprintf(query, sizeof(query), |
| | | 136 | | "SELECT id, role FROM users WHERE username = '%s' AND password_hash = '%s';", |
| | | 137 | | username, password); |
| | 0 | 138 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 139 | | |
| | 0 | 140 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 0 | 141 | | fprintf(stderr, "verify_user_credentials: Query failed for user: %s\n", username); |
| | 0 | 142 | | PQclear(res); |
| | 0 | 143 | | db_pool_release(conn_wrapper); |
| | 0 | 144 | | return -1; |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | if (PQntuples(res) == 0) { |
| | 0 | 148 | | fprintf(stderr, "verify_user_credentials: Login failed for user: %s\n", username); |
| | 0 | 149 | | PQclear(res); |
| | 0 | 150 | | db_pool_release(conn_wrapper); |
| | 0 | 151 | | return -1; |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | int uid = atoi(PQgetvalue(res, 0, 0)); |
| | | 155 | | |
| | 0 | 156 | | if (out_role) { |
| | 0 | 157 | | strncpy(out_role, PQgetvalue(res, 0, 1), 19); |
| | 0 | 158 | | out_role[19] = '\0'; |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | PQclear(res); |
| | 0 | 162 | | db_pool_release(conn_wrapper); |
| | 0 | 163 | | printf("verify_user_credentials: User %s (ID: %d) successfully logged in\n", username, uid); |
| | 0 | 164 | | return uid; |
| | | 165 | | } |