KiCad PCB EDA Suite
Loading...
Searching...
No Matches
oauth_pkce.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 <oauth/oauth_pkce.h>
21
22#include <random>
23#include <vector>
24
25#include <picosha2.h>
26#include <wx/base64.h>
27
28
29namespace
30{
31const char TOKEN_CHARS[] =
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
33
34
35wxString randomToken( size_t aLength )
36{
37 static std::random_device rd;
38 static std::mt19937_64 gen( rd() );
39 std::uniform_int_distribution<size_t> dist( 0, sizeof( TOKEN_CHARS ) - 2 );
40
41 wxString token;
42 token.reserve( aLength );
43
44 for( size_t ii = 0; ii < aLength; ++ii )
45 token.Append( TOKEN_CHARS[dist( gen )] );
46
47 return token;
48}
49
50
51wxString base64UrlEncode( const std::vector<unsigned char>& aBytes )
52{
53 wxString encoded = wxBase64Encode( aBytes.data(), aBytes.size() );
54 encoded.Replace( wxS( "+" ), wxS( "-" ) );
55 encoded.Replace( wxS( "/" ), wxS( "_" ) );
56 encoded.Replace( wxS( "=" ), wxEmptyString );
57 encoded.Replace( wxS( "\n" ), wxEmptyString );
58 return encoded;
59}
60} // namespace
61
62
64{
65 return randomToken( 64 );
66}
67
68
70{
71 return randomToken( 32 );
72}
73
74
75wxString OAUTH_PKCE::CreateCodeChallenge( const wxString& aVerifier )
76{
77 const std::string verifier = aVerifier.ToStdString();
78 std::vector<unsigned char> digest( picosha2::k_digest_size );
79
80 picosha2::hash256( verifier.begin(), verifier.end(), digest.begin(), digest.end() );
81
82 return base64UrlEncode( digest );
83}
static wxString CreateCodeChallenge(const wxString &aVerifier)
static wxString GenerateCodeVerifier()
static wxString GenerateState()