Skip to content

Commit

Permalink
Use hash for storing local list items too. Fix SQL style issue in ini…
Browse files Browse the repository at this point in the history
…t_core.sql

Signed-off-by: Marc Emmers <[email protected]>
  • Loading branch information
marcemmers committed Aug 24, 2023
1 parent 3dbb0b1 commit 4a160c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
11 changes: 4 additions & 7 deletions config/v201/init_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ CREATE TABLE IF NOT EXISTS AUTH_LIST_VERSION (
);

CREATE TABLE IF NOT EXISTS AUTH_LIST (
ID_TOKEN TEXT NOT NULL,
ID_TOKEN_TYPE TEXT NOT NULL,
ID_TOKEN_INFO TEXT NOT NULL,
PRIMARY KEY (ID_TOKEN, ID_TOKEN_TYPE)
ID_TOKEN_HASH TEXT PRIMARY KEY NOT NULL,
ID_TOKEN_INFO TEXT NOT NULL
);

INSERT
OR IGNORE INTO AUTH_LIST_VERSION (ID, VERSION)
VALUES (0, 0);
INSERT OR IGNORE INTO AUTH_LIST_VERSION (ID, VERSION) VALUES
(0, 0);

23 changes: 8 additions & 15 deletions lib/ocpp/v201/database_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ocpp/common/sqlite_statement.hpp>
#include <ocpp/v201/database_handler.hpp>
#include <ocpp/v201/types.hpp>
#include <ocpp/v201/utils.hpp>

namespace fs = std::filesystem;

Expand Down Expand Up @@ -201,13 +202,11 @@ int32_t DatabaseHandler::get_local_authorization_list_version() {
void DatabaseHandler::insert_or_update_local_authorization_list_entry(const IdToken& id_token,
const IdTokenInfo& id_token_info) {
// add or replace
std::string sql = "INSERT OR REPLACE INTO AUTH_LIST (ID_TOKEN, ID_TOKEN_TYPE, ID_TOKEN_INFO) "
"VALUES (@id_token, @id_token_type, @id_token_info)";
std::string sql = "INSERT OR REPLACE INTO AUTH_LIST (ID_TOKEN_HASH, ID_TOKEN_INFO) "
"VALUES (@id_token_hash, @id_token_info)";
SQLiteStatement stmt(this->db, sql);

stmt.bind_text("@id_token", id_token.idToken.get(), SQLiteString::Transient);
stmt.bind_text("@id_token_type", v201::conversions::id_token_enum_to_string(id_token.type),
SQLiteString::Transient);
stmt.bind_text("@id_token_hash", utils::generate_token_hash(id_token), SQLiteString::Transient);
stmt.bind_text("@id_token_info", json(id_token_info).dump(), SQLiteString::Transient);

if (stmt.step() != SQLITE_DONE) {
Expand All @@ -229,13 +228,10 @@ void DatabaseHandler::insert_or_update_local_authorization_list(
}

void DatabaseHandler::delete_local_authorization_list_entry(const IdToken& id_token) {
std::string sql = "DELETE FROM AUTH_LIST WHERE ID_TOKEN = @id_token AND "
"ID_TOKEN_TYPE = @id_token_type;";
std::string sql = "DELETE FROM AUTH_LIST WHERE ID_TOKEN_HASH = @id_token_hash;";
SQLiteStatement stmt(this->db, sql);

stmt.bind_text("@id_token", id_token.idToken.get(), SQLiteString::Transient);
stmt.bind_text("@id_token_type", v201::conversions::id_token_enum_to_string(id_token.type),
SQLiteString::Transient);
stmt.bind_text("@id_token_hash", utils::generate_token_hash(id_token), SQLiteString::Transient);

if (stmt.step() != SQLITE_DONE) {
EVLOG_error << "Could not delete from table: " << sqlite3_errmsg(db);
Expand All @@ -244,13 +240,10 @@ void DatabaseHandler::delete_local_authorization_list_entry(const IdToken& id_to

std::optional<IdTokenInfo> DatabaseHandler::get_local_authorization_list_entry(const IdToken& id_token) {
try {
std::string sql = "SELECT ID_TOKEN_INFO FROM AUTH_LIST WHERE ID_TOKEN = @id_token AND "
"ID_TOKEN_TYPE = @id_token_type;";
std::string sql = "SELECT ID_TOKEN_INFO FROM AUTH_LIST WHERE ID_TOKEN_HASH = @id_token_hash;";
SQLiteStatement stmt(this->db, sql);

stmt.bind_text("@id_token", id_token.idToken.get(), SQLiteString::Transient);
stmt.bind_text("@id_token_type", v201::conversions::id_token_enum_to_string(id_token.type),
SQLiteString::Transient);
stmt.bind_text("@id_token_hash", utils::generate_token_hash(id_token), SQLiteString::Transient);

if (stmt.step() != SQLITE_ROW) {
return std::nullopt;
Expand Down

0 comments on commit 4a160c1

Please sign in to comment.