Skip to content

Commit

Permalink
Fixes brave/brave-browser#10978 - Adding flow for using refresh token…
Browse files Browse the repository at this point in the history
… (Gemini)
  • Loading branch information
ryanml committed Jul 29, 2020
1 parent a06d79b commit 5169f74
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
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

0 comments on commit 5169f74

Please sign in to comment.