KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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
21
22#include <json_conversions.h>
23#include <kiplatform/secrets.h>
24
25#include <nlohmann/json.hpp>
26
27
28void to_json( nlohmann::json& aJson, const OAUTH_TOKEN_SET& aTokens )
29{
30 aJson = nlohmann::json{
31 { "access_token", aTokens.access_token },
32 { "refresh_token", aTokens.refresh_token },
33 { "id_token", aTokens.id_token },
34 { "token_type", aTokens.token_type },
35 { "scope", aTokens.scope },
36 { "expires_at", aTokens.expires_at }
37 };
38}
39
40
41void from_json( const nlohmann::json& aJson, OAUTH_TOKEN_SET& aTokens )
42{
43 aTokens = OAUTH_TOKEN_SET();
44 aJson.at( "access_token" ).get_to( aTokens.access_token );
45 aJson.at( "refresh_token" ).get_to( aTokens.refresh_token );
46 aJson.at( "id_token" ).get_to( aTokens.id_token );
47 aJson.at( "token_type" ).get_to( aTokens.token_type );
48 aJson.at( "scope" ).get_to( aTokens.scope );
49 aJson.at( "expires_at" ).get_to( aTokens.expires_at );
50}
51
52
53bool PLATFORM_SECRET_BACKEND::StoreSecret( const wxString& aService, const wxString& aKey,
54 const wxString& aSecret )
55{
56 return KIPLATFORM::SECRETS::StoreSecret( aService, aKey, aSecret );
57}
58
59
60bool PLATFORM_SECRET_BACKEND::GetSecret( const wxString& aService, const wxString& aKey,
61 wxString& aSecret ) const
62{
63 return KIPLATFORM::SECRETS::GetSecret( aService, aKey, aSecret );
64}
65
66
67bool PLATFORM_SECRET_BACKEND::DeleteSecret( const wxString& aService, const wxString& aKey )
68{
69 return KIPLATFORM::SECRETS::DeleteSecret( aService, aKey );
70}
71
72
73SECURE_TOKEN_STORE::SECURE_TOKEN_STORE( std::unique_ptr<OAUTH_SECRET_BACKEND> aBackend ) :
74 m_backend( std::move( aBackend ) )
75{
76}
77
78
79bool SECURE_TOKEN_STORE::StoreTokens( const wxString& aProviderId, const wxString& aAccountId,
80 const OAUTH_TOKEN_SET& aTokens )
81{
82 const wxString secret = wxString::FromUTF8( nlohmann::json( aTokens ).dump().c_str() );
83 return m_backend->StoreSecret( MakeServiceName( aProviderId ), aAccountId, secret );
84}
85
86
87std::optional<OAUTH_TOKEN_SET> SECURE_TOKEN_STORE::LoadTokens( const wxString& aProviderId,
88 const wxString& aAccountId ) const
89{
90 wxString secret;
91
92 if( !m_backend->GetSecret( MakeServiceName( aProviderId ), aAccountId, secret )
93 || secret.IsEmpty() )
94 {
95 return std::nullopt;
96 }
97
98 try
99 {
100 return nlohmann::json::parse( secret.ToStdString() ).get<OAUTH_TOKEN_SET>();
101 }
102 catch( ... )
103 {
104 return std::nullopt;
105 }
106}
107
108
109bool SECURE_TOKEN_STORE::DeleteTokens( const wxString& aProviderId, const wxString& aAccountId )
110{
111 return m_backend->DeleteSecret( MakeServiceName( aProviderId ), aAccountId );
112}
113
114
115wxString SECURE_TOKEN_STORE::MakeServiceName( const wxString& aProviderId )
116{
117 return wxS( "org.kicad.remote_provider." ) + aProviderId;
118}
bool GetSecret(const wxString &aService, const wxString &aKey, wxString &aSecret) const override
bool StoreSecret(const wxString &aService, const wxString &aKey, const wxString &aSecret) override
bool DeleteSecret(const wxString &aService, const wxString &aKey) override
std::unique_ptr< OAUTH_SECRET_BACKEND > m_backend
bool DeleteTokens(const wxString &aProviderId, const wxString &aAccountId)
std::optional< OAUTH_TOKEN_SET > LoadTokens(const wxString &aProviderId, const wxString &aAccountId) const
static wxString MakeServiceName(const wxString &aProviderId)
SECURE_TOKEN_STORE(std::unique_ptr< OAUTH_SECRET_BACKEND > aBackend=std::make_unique< PLATFORM_SECRET_BACKEND >())
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)
STL namespace.
void from_json(const nlohmann::json &aJson, OAUTH_TOKEN_SET &aTokens)
void to_json(nlohmann::json &aJson, const OAUTH_TOKEN_SET &aTokens)
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.