KiCad PCB EDA Suite
Loading...
Searching...
No Matches
remote_provider_models.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
23#include <paths.h>
25#include <wx_filename.h>
26#include <wx/intl.h>
27
28
29std::optional<REMOTE_PROVIDER_OAUTH_SERVER_METADATA>
30REMOTE_PROVIDER_OAUTH_SERVER_METADATA::FromJson( const nlohmann::json& aJson, bool aAllowInsecureLocalhost,
31 wxString& aError )
32{
34
35 try
36 {
37 metadata.issuer = RemoteProviderJsonString( aJson, "issuer" );
38 metadata.authorization_endpoint = RemoteProviderJsonString( aJson, "authorization_endpoint" );
39 metadata.token_endpoint = RemoteProviderJsonString( aJson, "token_endpoint" );
40 metadata.revocation_endpoint = RemoteProviderJsonString( aJson, "revocation_endpoint" );
41 }
42 catch( const std::exception& e )
43 {
44 aError = wxString::Format( _( "Unable to parse OAuth metadata: %s" ), wxString::FromUTF8( e.what() ) );
45 return std::nullopt;
46 }
47
48 if( metadata.authorization_endpoint.IsEmpty() || metadata.token_endpoint.IsEmpty() )
49 {
50 aError = _( "OAuth metadata must include authorization_endpoint and token_endpoint." );
51 return std::nullopt;
52 }
53
54 if( !ValidateRemoteUrlSecurity( metadata.authorization_endpoint, aAllowInsecureLocalhost, aError,
55 _( "authorization_endpoint" ) ) )
56 {
57 return std::nullopt;
58 }
59
60 if( !ValidateRemoteUrlSecurity( metadata.token_endpoint, aAllowInsecureLocalhost, aError, _( "token_endpoint" ) ) )
61 {
62 return std::nullopt;
63 }
64
65 if( !ValidateRemoteUrlSecurity( metadata.revocation_endpoint, aAllowInsecureLocalhost, aError,
66 _( "revocation_endpoint" ) ) )
67 {
68 return std::nullopt;
69 }
70
71 return metadata;
72}
73
74
76{
77 wxFileName schemaFile( PATHS::GetStockDataPath( true ), wxS( "kicad-remote-symbol-manifest-v1.schema.json" ) );
78 schemaFile.AppendDir( wxS( "schemas" ) );
79 schemaFile.Normalize( FN_NORMALIZE_FLAGS );
80 return schemaFile;
81}
82
83
84std::optional<REMOTE_PROVIDER_PART_MANIFEST>
85REMOTE_PROVIDER_PART_MANIFEST::FromJson( const nlohmann::json& aJson, bool aAllowInsecureLocalhost, wxString& aError )
86{
87 return FromJson( aJson, DefaultSchemaPath(), aAllowInsecureLocalhost, aError );
88}
89
90
91std::optional<REMOTE_PROVIDER_PART_MANIFEST> REMOTE_PROVIDER_PART_MANIFEST::FromJson( const nlohmann::json& aJson,
92 const wxFileName& aSchemaFile,
93 bool aAllowInsecureLocalhost,
94 wxString& aError )
95{
96 if( !aSchemaFile.IsFileReadable() )
97 {
98 aError = wxString::Format( _( "Remote provider manifest schema '%s' is not readable." ),
99 aSchemaFile.GetFullPath() );
100 return std::nullopt;
101 }
102
104 JSON_SCHEMA_VALIDATOR validator( aSchemaFile );
105 validator.Validate( aJson, handler );
106
107 if( handler.HasErrors() )
108 {
109 aError = wxString::Format( _( "Remote provider manifest failed schema validation: %s" ), handler.FirstError() );
110 return std::nullopt;
111 }
112
114
115 try
116 {
117 manifest.part_id = RemoteProviderJsonString( aJson, "part_id" );
118 manifest.display_name = RemoteProviderJsonString( aJson, "display_name" );
119 manifest.summary = RemoteProviderJsonString( aJson, "summary" );
120 manifest.license = RemoteProviderJsonString( aJson, "license" );
121
122 for( const nlohmann::json& assetJson : aJson.at( "assets" ) )
123 {
125 asset.asset_type = RemoteProviderJsonString( assetJson, "asset_type" );
126 asset.name = RemoteProviderJsonString( assetJson, "name" );
127 asset.target_library = RemoteProviderJsonString( assetJson, "target_library" );
128 asset.target_name = RemoteProviderJsonString( assetJson, "target_name" );
129 asset.content_type = RemoteProviderJsonString( assetJson, "content_type" );
130 asset.size_bytes = assetJson.at( "size_bytes" ).get<long long>();
131 asset.sha256 = RemoteProviderJsonString( assetJson, "sha256" );
132 asset.download_url = RemoteProviderJsonString( assetJson, "download_url" );
133 asset.required = assetJson.at( "required" ).get<bool>();
134
135 if( !ValidateRemoteUrlSecurity( asset.download_url, aAllowInsecureLocalhost, aError,
136 _( "assets[].download_url" ) ) )
137 {
138 return std::nullopt;
139 }
140
141 manifest.assets.push_back( asset );
142 }
143 }
144 catch( const std::exception& e )
145 {
146 aError =
147 wxString::Format( _( "Unable to parse remote provider manifest: %s" ), wxString::FromUTF8( e.what() ) );
148 return std::nullopt;
149 }
150
151 return manifest;
152}
Collects JSON-schema validation errors so the caller can inspect them after a validation pass.
nlohmann::json Validate(const nlohmann::json &aJson, nlohmann::json_schema::error_handler &aErrorHandler, const nlohmann::json_uri &aInitialUri=nlohmann::json_uri("#")) const
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition paths.cpp:233
#define _(s)
wxString RemoteProviderJsonString(const nlohmann::json &aObject, const char *aKey)
Extract an optional string value from a JSON object, returning an empty wxString when the key is abse...
bool ValidateRemoteUrlSecurity(const wxString &aUrl, bool aAllowInsecureLocalhost, wxString &aError, const wxString &aLabel)
Validate that aUrl uses HTTPS, or HTTP on a loopback address when aAllowInsecureLocalhost is true.
static std::optional< REMOTE_PROVIDER_OAUTH_SERVER_METADATA > FromJson(const nlohmann::json &aJson, bool aAllowInsecureLocalhost, wxString &aError)
std::vector< REMOTE_PROVIDER_PART_ASSET > assets
static std::optional< REMOTE_PROVIDER_PART_MANIFEST > FromJson(const nlohmann::json &aJson, bool aAllowInsecureLocalhost, wxString &aError)
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39