Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding flow for using refresh tokens (Gemini) #6242

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions browser/extensions/api/gemini_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ void GeminiGetAccessTokenFunction::OnCodeResult(bool success) {
Respond(OneArgument(std::make_unique<base::Value>(success)));
}

ExtensionFunction::ResponseAction
GeminiRefreshAccessTokenFunction::Run() {
auto* service = GetGeminiService(browser_context());
bool token_request = service->RefreshAccessToken(base::BindOnce(
&GeminiRefreshAccessTokenFunction::OnRefreshResult, this));

if (!token_request) {
return RespondNow(
Error("Could not make request to refresh access tokens"));
}

return RespondLater();
}

void GeminiRefreshAccessTokenFunction::OnRefreshResult(bool success) {
Respond(OneArgument(std::make_unique<base::Value>(success)));
}

ExtensionFunction::ResponseAction
GeminiGetTickerPriceFunction::Run() {
std::unique_ptr<gemini::GetTickerPrice::Params> params(
Expand Down
12 changes: 12 additions & 0 deletions browser/extensions/api/gemini_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class GeminiGetAccessTokenFunction :
ResponseAction Run() override;
};

class GeminiRefreshAccessTokenFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("gemini.refreshAccessToken", UNKNOWN)

protected:
~GeminiRefreshAccessTokenFunction() override {}
void OnRefreshResult(bool success);

ResponseAction Run() override;
};

class GeminiGetTickerPriceFunction :
public ExtensionFunction {
public:
Expand Down
18 changes: 18 additions & 0 deletions common/extensions/api/gemini.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@
}
]
},
{
"name": "refreshAccessToken",
"type": "function",
"description": "Facilitates fetching a new access token",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "success",
"type": "boolean",
"description": "Indicates the access token was refreshed successfully"
}
]
}
]
},
{
"name": "getTickerPrice",
"type": "function",
Expand Down
6 changes: 5 additions & 1 deletion components/brave_new_tab_ui/containers/newTab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,11 @@ class NewTabPage extends React.Component<Props, State> {
fetchGeminiBalances = () => {
chrome.gemini.getAccountBalances((balances: Record<string, string>, authInvalid: boolean) => {
if (authInvalid) {
this.setGeminiAuthInvalid()
chrome.gemini.refreshAccessToken((success: boolean) => {
if (!success) {
this.setGeminiAuthInvalid()
}
})
return
}

Expand Down
1 change: 1 addition & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ declare namespace chrome.binance {
declare namespace chrome.gemini {
const getClientUrl: (callback: (clientUrl: string) => void) => {}
const getAccessToken: (callback: (success: boolean) => void) => {}
const refreshAccessToken: (callback: (success: boolean) => void) => {}
const getTickerPrice: (asset: string, callback: (price: string) => void) => {}
const getAccountBalances: (callback: (balances: Record<string, string>, authInvalid: boolean) => void) => {}
const getDepositInfo: (asset: string, callback: (depositAddress: string, depositTag: string) => void) => {}
Expand Down
22 changes: 20 additions & 2 deletions components/gemini/browser/gemini_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void GeminiService::SetAuthToken(const std::string& auth_token) {
auth_token_ = auth_token;
}

bool GeminiService::GetAccessToken(GetAccessTokenCallback callback) {
bool GeminiService::GetAccessToken(AccessTokenCallback callback) {
auto internal_callback = base::BindOnce(&GeminiService::OnGetAccessToken,
base::Unretained(this), std::move(callback));
GURL base_url = GetURLWithPath(oauth_host_, oauth_path_access_token);
Expand All @@ -166,8 +166,26 @@ bool GeminiService::GetAccessToken(GetAccessTokenCallback callback) {
std::move(internal_callback), true, false, "");
}

bool GeminiService::RefreshAccessToken(AccessTokenCallback callback) {
auto internal_callback = base::BindOnce(&GeminiService::OnGetAccessToken,
base::Unretained(this), std::move(callback));
GURL base_url = GetURLWithPath(oauth_host_, oauth_path_access_token);

base::Value dict(base::Value::Type::DICTIONARY);
dict.SetStringKey("client_id", client_id_);
dict.SetStringKey("client_secret", client_secret_);
dict.SetStringKey("refresh_token", refresh_token_);
dict.SetStringKey("grant_type", "refresh_token");
std::string request_body = CreateJSONRequestBody(dict);

auth_token_.clear();
return OAuthRequest(
base_url, "POST", request_body,
std::move(internal_callback), true, false, "");
}

void GeminiService::OnGetAccessToken(
GetAccessTokenCallback callback,
AccessTokenCallback callback,
const int status, const std::string& body,
const std::map<std::string, std::string>& headers) {
std::string access_token;
Expand Down
7 changes: 4 additions & 3 deletions components/gemini/browser/gemini_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GeminiService : public KeyedService {
~GeminiService() override;

// Callbacks
using GetAccessTokenCallback = base::OnceCallback<void(bool)>;
using AccessTokenCallback = base::OnceCallback<void(bool)>;
using GetTickerPriceCallback = base::OnceCallback<void(const std::string&)>;
using URLRequestCallback =
base::OnceCallback<void(const int, const std::string&,
Expand All @@ -71,7 +71,8 @@ class GeminiService : public KeyedService {

std::string GetOAuthClientUrl();
void SetAuthToken(const std::string& auth_token);
bool GetAccessToken(GetAccessTokenCallback callback);
bool GetAccessToken(AccessTokenCallback callback);
bool RefreshAccessToken(AccessTokenCallback callback);
bool GetTickerPrice(const std::string& asset,
GetTickerPriceCallback callback);
bool GetAccountBalances(GetAccountBalancesCallback callback);
Expand Down Expand Up @@ -106,7 +107,7 @@ class GeminiService : public KeyedService {
const std::string& refresh_token);
void ResetAccessTokens();

void OnGetAccessToken(GetAccessTokenCallback callback,
void OnGetAccessToken(AccessTokenCallback callback,
const int status, const std::string& body,
const std::map<std::string, std::string>& headers);
void OnTickerPrice(GetTickerPriceCallback callback,
Expand Down
47 changes: 47 additions & 0 deletions components/gemini/browser/gemini_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ class GeminiAPIBrowserTest : public InProcessBrowserTest {
wait_for_request_->Run();
}

void WaitForRefreshAccessToken(bool expected_success) {
if (wait_for_request_) {
return;
}
expected_success_ = expected_success;
wait_for_request_.reset(new base::RunLoop);
wait_for_request_->Run();
}

void OnGetOrderQuote(const std::string& quote_id,
const std::string& quantity, const std::string& fee,
const std::string& price, const std::string& total_price,
Expand Down Expand Up @@ -351,6 +360,44 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetAccessTokenServerError) {
WaitForGetAccessToken(false);
}

#if !defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, RefreshAccessToken) {
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
auto* service = GetGeminiService();
service->SetAuthToken("abc123");
ASSERT_TRUE(service->RefreshAccessToken(
base::BindOnce(
&GeminiAPIBrowserTest::OnGetAccessToken,
base::Unretained(this), true)));
WaitForRefreshAccessToken(true);
}
#endif

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, RefreshTokenUnauthorized) {
ResetHTTPSServer(base::BindRepeating(&HandleRequestUnauthorized));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
auto* service = GetGeminiService();
service->SetAuthToken("abc123");
ASSERT_TRUE(service->RefreshAccessToken(
base::BindOnce(
&GeminiAPIBrowserTest::OnGetAccessToken,
base::Unretained(this), false)));
WaitForRefreshAccessToken(false);
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, RefreshTokenServerError) {
ResetHTTPSServer(base::BindRepeating(&HandleRequestServerError));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
auto* service = GetGeminiService();
service->SetAuthToken("abc123");
ASSERT_TRUE(service->RefreshAccessToken(
base::BindOnce(
&GeminiAPIBrowserTest::OnGetAccessToken,
base::Unretained(this), false)));
WaitForRefreshAccessToken(false);
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuote) {
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
Expand Down