KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_secure_token_store.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <map>
23
24#include <json_common.h>
25
28
29
30namespace
31{
32class MEMORY_SECRET_BACKEND : public OAUTH_SECRET_BACKEND
33{
34public:
35 bool StoreSecret( const wxString& aService, const wxString& aKey, const wxString& aSecret ) override
36 {
37 m_entries[aService + wxS( "::" ) + aKey] = aSecret;
38 return true;
39 }
40
41 bool GetSecret( const wxString& aService, const wxString& aKey, wxString& aSecret ) const override
42 {
43 auto it = m_entries.find( aService + wxS( "::" ) + aKey );
44
45 if( it == m_entries.end() )
46 return false;
47
48 aSecret = it->second;
49 return !aSecret.IsEmpty();
50 }
51
52 bool DeleteSecret( const wxString& aService, const wxString& aKey ) override
53 {
54 m_entries.erase( aService + wxS( "::" ) + aKey );
55 return true;
56 }
57
58private:
59 std::map<wxString, wxString> m_entries;
60};
61} // namespace
62
63
64BOOST_AUTO_TEST_SUITE( SecureTokenStoreTests )
65
66BOOST_AUTO_TEST_CASE( StoresLoadsAndDeletesTokens )
67{
68 auto backend = std::make_unique<MEMORY_SECRET_BACKEND>();
69 SECURE_TOKEN_STORE store( std::move( backend ) );
70
71 OAUTH_TOKEN_SET tokens;
72 tokens.access_token = wxS( "access-token" );
73 tokens.refresh_token = wxS( "refresh-token" );
74 tokens.id_token = wxS( "id-token" );
75 tokens.token_type = wxS( "Bearer" );
76 tokens.scope = wxS( "openid profile parts.read" );
77 tokens.expires_at = 1777777777;
78
79 BOOST_REQUIRE( store.StoreTokens( wxS( "provider-acme" ), wxS( "[email protected]" ), tokens ) );
80
81 std::optional<OAUTH_TOKEN_SET> loaded =
82 store.LoadTokens( wxS( "provider-acme" ), wxS( "[email protected]" ) );
83
84 BOOST_REQUIRE( loaded.has_value() );
85 BOOST_CHECK_EQUAL( loaded->access_token, wxString( "access-token" ) );
86 BOOST_CHECK_EQUAL( loaded->refresh_token, wxString( "refresh-token" ) );
87 BOOST_CHECK_EQUAL( loaded->scope, wxString( "openid profile parts.read" ) );
88
89 BOOST_REQUIRE( store.DeleteTokens( wxS( "provider-acme" ), wxS( "[email protected]" ) ) );
90 BOOST_CHECK( !store.LoadTokens( wxS( "provider-acme" ), wxS( "[email protected]" ) ).has_value() );
91}
92
93BOOST_AUTO_TEST_CASE( TokenStoreDoesNotSerializeIntoProviderSettings )
94{
96 REMOTE_PROVIDER_ENTRY provider;
97 provider.provider_id = wxS( "provider-acme" );
98 provider.metadata_url = wxS( "https://provider.example.test/.well-known/kicad-remote-provider" );
99 provider.last_account_label = wxS( "Acme User" );
100 provider.last_auth_status = wxS( "signed_in" );
101 settings.providers.push_back( provider );
102
103 const std::string dumped = nlohmann::json( settings ).dump();
104
105 BOOST_CHECK( dumped.find( "access-token" ) == std::string::npos );
106 BOOST_CHECK( dumped.find( "refresh-token" ) == std::string::npos );
107 BOOST_CHECK( dumped.find( "id-token" ) == std::string::npos );
108}
109
bool DeleteTokens(const wxString &aProviderId, const wxString &aAccountId)
std::optional< OAUTH_TOKEN_SET > LoadTokens(const wxString &aProviderId, const wxString &aAccountId) const
bool StoreTokens(const wxString &aProviderId, const wxString &aAccountId, const OAUTH_TOKEN_SET &aTokens)
bool DeleteSecret(const wxString &aService, const wxString &aKey)
bool StoreSecret(const wxString &aService, const wxString &aKey, const wxString &aSecret)
bool GetSecret(const wxString &aService, const wxString &aKey, wxString &aSecret)
wxString provider_id
wxString last_account_label
wxString last_auth_status
wxString metadata_url
std::vector< REMOTE_PROVIDER_ENTRY > providers
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(StoresLoadsAndDeletesTokens)
BOOST_CHECK_EQUAL(result, "25.4")