Skip to content
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
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"string_view": "cpp",
"text_encoding": "cpp"
}
}
78 changes: 51 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ target_link_libraries(your_target PRIVATE BankID::bankid_lib)
```cpp
#include "bankid.h"

const std::string socialSecurityNumber = "1234567891"

// Configure SSL for test environment
BankID::SSLConfig sslConfig(BankID::Environment::TEST,
"certs/bankid_cert.pem",
"certs/bankid_key.pem");
BankID::SSLConfig sslConfig(BankID::Environment::TEST);

// Enable/disable debug logging
const bool showDebug = true;
Expand All @@ -275,38 +275,62 @@ const bool showDebug = true;
BankID::Session session(sslConfig, showDebug);

// Initialize the session
if (!session.initialize()) {
std::cerr << "Failed to initialize BankID session" << std::endl;
return -1;
if (!session.initialize())
{
std::cerr << "Failed to initialize BankID session" << std::endl;
return -1;
}

// Start authentication
BankID::API::AuthConfig authConfig("192.168.1.1"); // End user IP
auto authResult = session.auth(authConfig);
BankID::Requirement requirement;
requirement.personalNumber = socialSecurityNumber; // Example personal number from command line

if (authResult.has_value()) {
std::cout << "Authentication started. Order ref: "
<< authResult->orderRef << std::endl;
// Start authentication
BankID::API::AuthConfig authConfig("192.168.1.1");
authConfig.setRequirement(requirement);

// Poll for completion
BankID::API::CollectConfig collectConfig(authResult->orderRef);
auto authResult = session.auth(authConfig);

while (true) {
auto collectResult = session.collect(collectConfig);
if (collectResult.has_value()) {
if (collectResult->status == "COMPLETED") {
std::cout << "Authentication completed!" << std::endl;
break;
} else if (collectResult->status == "FAILED") {
std::cout << "Authentication failed!" << std::endl;
break;
}
if (authResult.has_value())
{
std::cout << "Authentication started. Order ref: "
<< authResult->orderRef << std::endl;

// Poll for completion
BankID::API::CollectConfig collectConfig(authResult->orderRef);

while (true)
{
auto collectResult = session.collect(collectConfig);
if (collectResult.has_value())
{
if (collectResult->status == BankID::API::CollectStatus::COMPLETE)
{
std::cout << "Authentication completed successfully!" << std::endl;
std::cout << "Order ref: " << collectResult->orderRef << std::endl;
if (collectResult->completionData)
{
std::cout << "Completion data: " << collectResult->completionData->user->givenName.value_or("N/A") << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
break;
}
else if (collectResult->status == BankID::API::CollectStatus::PENDING)
{
std::cout << "Authentication still pending..." << std::endl;
}
else if (collectResult->status == BankID::API::CollectStatus::FAILED)
{
std::cout << "Authentication falses!" << std::endl;
break;
}
}
} else {
std::cerr << "Authentication failed: " << authResult.error().message << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
else
{
std::cerr << "Authentication failed: " << authResult.error().details << std::endl;
}

```

### Advanced Authentication with Requirements
Expand Down
2 changes: 1 addition & 1 deletion bankid/includes/api/payment.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace BankID::API
/**
* Constructor for PaymentConfig
* @param endUserIp The IP address of the end user (required)
* @param userVisibleTransaction Transaction information displayed to user (required)
* @param userVisibleTransaction Transaction information displayed to user (required)
*/
PaymentConfig(const std::string &endUserIp, const UserVisibleTransaction &userVisibleTransaction)
: m_endUserIp(endUserIp), m_userVisibleTransaction(userVisibleTransaction) {}
Expand Down
2 changes: 1 addition & 1 deletion bankid/includes/api/sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace BankID::API
/**
* Constructor for SignConfig
* @param endUserIp The IP address of the end user (required)
* @param userVisibleData Text displayed to the user during signing (required)
* @param userVisibleData Text displayed to the user during signing (required) BASE 64-encoded
*/
SignConfig(const std::string &endUserIp, const std::string &userVisibleData)
: m_endUserIp(endUserIp), m_userVisibleData(userVisibleData) {}
Expand Down
10 changes: 8 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ add_executable(bankid_tests
test_ssl.cpp
test_auth.cpp
test_sign.cpp
# test_payment.cpp
test_payment.cpp
# test_other_payment.cpp
# test_phone_auth.cpp
# test_phone_sign.cpp
# test_collect.cpp
# test_cancel.cpp
test_cancel.cpp
)

# Link libraries
Expand Down Expand Up @@ -125,6 +125,12 @@ set_tests_properties(AuthTests PROPERTIES WORKING_DIRECTORY $<TARGET_FILE_DIR:ba
add_test(NAME SignTests COMMAND bankid_tests --gtest_filter=SignTest.*)
set_tests_properties(SignTests PROPERTIES WORKING_DIRECTORY $<TARGET_FILE_DIR:bankid_tests>)

add_test(NAME PaymentTests COMMAND bankid_tests --gtest_filter=PaymentTest.*)
set_tests_properties(PaymentTests PROPERTIES WORKING_DIRECTORY $<TARGET_FILE_DIR:bankid_tests>)

add_test(NAME CancelTests COMMAND bankid_tests --gtest_filter=CancelTest.*)
set_tests_properties(CancelTests PROPERTIES WORKING_DIRECTORY $<TARGET_FILE_DIR:bankid_tests>)

# Set environment variables for tests to find shared libraries
if(BUILD_SHARED_LIBS AND UNIX)
set_tests_properties(SSLConfigTest AuthTests SignTests PROPERTIES
Expand Down
99 changes: 99 additions & 0 deletions tests/test_cancel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "bankid.h"
#include "api/auth.h"
#include "api/responses.h"

class CancelTest : public ::testing::Test
{
public:
CancelTest()
: sslConfig(BankID::Environment::TEST),
session(std::make_unique<BankID::Session>(sslConfig))
{
}

protected:
void TearDown() override
{
session.reset();
}

BankID::SSLConfig sslConfig;
std::unique_ptr<BankID::Session> session;
};

TEST_F(CancelTest, CancelAuth)
{
BankID::API::AuthConfig config("127.0.0.1");

EXPECT_TRUE(config.getReturnUrl().value_or("").empty());
EXPECT_TRUE(config.getUserVisibleData().value_or("").empty());

// Make the API call
auto response = session->auth(config);

EXPECT_TRUE(response.has_value());

// Check the response
const auto &orderResponse = response.value();
EXPECT_EQ(orderResponse.httpStatus, 200);
EXPECT_FALSE(orderResponse.orderRef.empty());

BankID::API::CancelConfig cancelConfig(orderResponse.orderRef);
auto cancelResponse = session->cancel(cancelConfig);
EXPECT_TRUE(cancelResponse.has_value());
EXPECT_EQ(cancelResponse.value().httpStatus, 200);
}

TEST_F(CancelTest, CancelSign)
{
BankID::API::SignConfig config("127.0.0.1", BankID::Base64::encode("Test Sign Data"));

EXPECT_TRUE(config.getReturnUrl().value_or("").empty());
EXPECT_FALSE(config.getUserVisibleData().empty());

// Make the API call
auto response = session->sign(config);

EXPECT_TRUE(response.has_value());

// Check the response
const auto &orderResponse = response.value();
EXPECT_EQ(orderResponse.httpStatus, 200);
EXPECT_FALSE(orderResponse.orderRef.empty());

BankID::API::CancelConfig cancelConfig(orderResponse.orderRef);
auto cancelResponse = session->cancel(cancelConfig);
EXPECT_TRUE(cancelResponse.has_value());
EXPECT_EQ(cancelResponse.value().httpStatus, 200);
}

TEST_F(CancelTest, CancelPayment)
{

BankID::API::UserVisibleTransaction transaction;
transaction.transactionType = "card";
transaction.recipient.name = "Test Recipient";
transaction.money = BankID::API::PaymentMoney{"100,00", BankID::API::CurrencyCode::EUR};

BankID::API::PaymentConfig config("127.0.0.1", transaction);

EXPECT_TRUE(config.getReturnUrl().value_or("").empty());
EXPECT_TRUE(config.getUserVisibleData().value_or("").empty());

// Make the API call
auto response = session->payment(config);

EXPECT_TRUE(response.has_value());

// Check the response
const auto &orderResponse = response.value();
EXPECT_EQ(orderResponse.httpStatus, 200);
EXPECT_FALSE(orderResponse.orderRef.empty());

BankID::API::CancelConfig cancelConfig(orderResponse.orderRef);
auto cancelResponse = session->cancel(cancelConfig);
EXPECT_TRUE(cancelResponse.has_value());
EXPECT_EQ(cancelResponse.value().httpStatus, 200);
}
Loading