Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the token id as well as the token type to generate auth cache hashes #148

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/ocpp/v201/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ TriggerReasonEnum stop_reason_to_trigger_reason_enum(const ReasonEnum& stop_reas
/// \return
std::string sha256(const std::string& str);

/// @brief Return a SHA256 hash generated from a combination of the \p token type and id
/// @param token the token to generate the hash for
/// @return A SHA256 hash string
std::string generate_token_hash(const IdToken& token);

} // namespace utils
} // namespace v201
} // namespace ocpp
Expand Down
10 changes: 5 additions & 5 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ AuthorizeResponse ChargePoint::validate_token(const IdToken id_token, const std:
return this->authorize_req(id_token, certificate, ocsp_request_data);
}

const auto hashed_id_token = utils::sha256(id_token.idToken.get());
const auto hashed_id_token = utils::generate_token_hash(id_token);
const auto cache_entry = this->database_handler->get_auth_cache_entry(hashed_id_token);

if (!cache_entry.has_value()) {
EVLOG_info << "AuthCache enabled but not entry found: Sending Authorize.req";
response = this->authorize_req(id_token, certificate, ocsp_request_data);
this->database_handler->insert_auth_cache_entry(utils::sha256(id_token.idToken.get()), response.idTokenInfo);
this->database_handler->insert_auth_cache_entry(hashed_id_token, response.idTokenInfo);
return response;
}

Expand All @@ -287,7 +287,7 @@ AuthorizeResponse ChargePoint::validate_token(const IdToken id_token, const std:
EVLOG_info << "Entry found in AuthCache but cacheExpiryDate exceeded: Sending Authorize.req";
this->database_handler->delete_auth_cache_entry(hashed_id_token);
response = this->authorize_req(id_token, certificate, ocsp_request_data);
this->database_handler->insert_auth_cache_entry(utils::sha256(id_token.idToken.get()), response.idTokenInfo);
this->database_handler->insert_auth_cache_entry(hashed_id_token, response.idTokenInfo);
return response;
}

Expand All @@ -299,7 +299,7 @@ AuthorizeResponse ChargePoint::validate_token(const IdToken id_token, const std:
}

response = this->authorize_req(id_token, certificate, ocsp_request_data);
this->database_handler->insert_auth_cache_entry(utils::sha256(id_token.idToken.get()), response.idTokenInfo);
this->database_handler->insert_auth_cache_entry(hashed_id_token, response.idTokenInfo);
return response;
}

Expand Down Expand Up @@ -1233,7 +1233,7 @@ void ChargePoint::handle_start_transaction_event_response(CallResult<Transaction
if (id_token.type != IdTokenEnum::Central and
this->device_model->get_optional_value<bool>(ControllerComponentVariables::AuthCacheCtrlrEnabled)
.value_or(true)) {
this->database_handler->insert_auth_cache_entry(utils::sha256(id_token.idToken.get()),
this->database_handler->insert_auth_cache_entry(utils::generate_token_hash(id_token),
msg.idTokenInfo.value());
}
if (msg.idTokenInfo.value().status != AuthorizationStatusEnum::Accepted) {
Expand Down
6 changes: 5 additions & 1 deletion lib/ocpp/v201/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ std::string sha256(const std::string& str) {
return ss.str();
}

std::string generate_token_hash(const IdToken& token) {
return sha256(conversions::id_token_enum_to_string(token.type) + token.idToken.get());
}

} // namespace utils
} // namespace v201
} // namespace ocpp
} // namespace ocpp