-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6306 from brave/gemini-pkce
Using PKCE flow for Gemini Authentication, adding shared oauth utility
- Loading branch information
Showing
16 changed files
with
183 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import("//brave/components/crypto_exchange/browser/buildflags/buildflags.gni") | ||
|
||
assert(crypto_exchanges_enabled) | ||
|
||
source_set("browser") { | ||
# Remove when https://github.com/brave/brave-browser/issues/10640 is resolved | ||
check_includes = false | ||
|
||
sources = [ | ||
"crypto_exchange_oauth_util.cc", | ||
"crypto_exchange_oauth_util.h", | ||
] | ||
|
||
deps = [ | ||
"//base", | ||
"//crypto", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import("//build/buildflag_header.gni") | ||
import("//brave/components/crypto_exchange/browser/buildflags/buildflags.gni") | ||
|
||
buildflag_header("buildflags") { | ||
header = "buildflags.h" | ||
flags = [ | ||
"CRYPTO_EXCHANGES_ENABLED=$crypto_exchanges_enabled", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import("//brave/build/config.gni") | ||
import("//brave/components/binance/browser/buildflags/buildflags.gni") | ||
import("//brave/components/gemini/browser/buildflags/buildflags.gni") | ||
|
||
declare_args() { | ||
crypto_exchanges_enabled = binance_enabled || gemini_enabled | ||
} |
57 changes: 57 additions & 0 deletions
57
components/crypto_exchange/browser/crypto_exchange_oauth_util.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include <string> | ||
|
||
#include "brave/components/crypto_exchange/browser/crypto_exchange_oauth_util.h" | ||
|
||
#include "base/base64.h" | ||
#include "base/strings/string_number_conversions.h" | ||
#include "crypto/random.h" | ||
#include "crypto/sha2.h" | ||
|
||
namespace crypto_exchange { | ||
|
||
std::string GetCryptoRandomString(bool hex_encode) { | ||
const size_t kSeedByteLength = 32; | ||
uint8_t random_seed_bytes[kSeedByteLength]; | ||
crypto::RandBytes(random_seed_bytes, kSeedByteLength); | ||
|
||
if (!hex_encode) { | ||
std::string encoded_string; | ||
base::Base64Encode( | ||
reinterpret_cast<char*>(random_seed_bytes), &encoded_string); | ||
return encoded_string; | ||
} | ||
|
||
return base::HexEncode( | ||
reinterpret_cast<char*>(random_seed_bytes), kSeedByteLength); | ||
} | ||
|
||
std::string GetCodeChallenge( | ||
const std::string& code_verifier, bool strip_chars) { | ||
std::string code_challenge; | ||
char raw[crypto::kSHA256Length] = {0}; | ||
crypto::SHA256HashString(code_verifier, | ||
raw, | ||
crypto::kSHA256Length); | ||
base::Base64Encode(base::StringPiece(raw, | ||
crypto::kSHA256Length), | ||
&code_challenge); | ||
|
||
if (strip_chars) { | ||
std::replace(code_challenge.begin(), code_challenge.end(), '+', '-'); | ||
std::replace(code_challenge.begin(), code_challenge.end(), '/', '_'); | ||
|
||
code_challenge.erase(std::find_if(code_challenge.rbegin(), | ||
code_challenge.rend(), [](int ch) { | ||
return ch != '='; | ||
}).base(), code_challenge.end()); | ||
} | ||
|
||
return code_challenge; | ||
} | ||
|
||
} // namespace crypto_exchange |
20 changes: 20 additions & 0 deletions
20
components/crypto_exchange/browser/crypto_exchange_oauth_util.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_CRYPTO_EXCHANGE_BROWSER_CRYPTO_EXCHANGE_OAUTH_UTIL_H_ | ||
#define BRAVE_COMPONENTS_CRYPTO_EXCHANGE_BROWSER_CRYPTO_EXCHANGE_OAUTH_UTIL_H_ | ||
|
||
#include <string> | ||
|
||
namespace crypto_exchange { | ||
|
||
std::string GetCryptoRandomString(bool hex_encode); | ||
|
||
std::string GetCodeChallenge( | ||
const std::string& code_verifier, bool strip_chars); | ||
|
||
} | ||
|
||
#endif // BRAVE_COMPONENTS_CRYPTO_EXCHANGE_BROWSER_CRYPTO_EXCHANGE_OAUTH_UTIL_H_ |
32 changes: 32 additions & 0 deletions
32
components/crypto_exchange/browser/crypto_exchange_oauth_util_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/crypto_exchange/browser/crypto_exchange_oauth_util.h" | ||
|
||
#include "chrome/test/base/chrome_render_view_host_test_harness.h" | ||
|
||
// npm run test -- brave_unit_tests --filter=CryptoExchangeOauthUtilTest.* | ||
|
||
namespace { | ||
|
||
typedef testing::Test CryptoExchangeOauthUtilTest; | ||
|
||
TEST_F(CryptoExchangeOauthUtilTest, GetCodeChallengeStripChars) { | ||
std::string verifier = | ||
"FA87A1758E149A8BCD3A6D43DEAFAA013BCE2F132639ADA66C5BF101"; | ||
ASSERT_EQ( | ||
"1vw-WOmdXSW7OHQPgnuMsZjhaQKxi3LO5L7uX0YEtHs", | ||
crypto_exchange::GetCodeChallenge(verifier, true)); | ||
} | ||
|
||
TEST_F(CryptoExchangeOauthUtilTest, GetCodeChallengeNoStripChars) { | ||
std::string verifier = | ||
"aGVsbG9fd29ybGRfdGhpc19pc19hX3Rlc3Q="; | ||
ASSERT_EQ( | ||
"mTWSN0meBbs9rauVM4rSmWDYVKTWFhkFeECqn6W2ZC0=", | ||
crypto_exchange::GetCodeChallenge(verifier, false)); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.