summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/extensions/api')
-rw-r--r--chrome/browser/extensions/api/identity/identity_api.cc67
-rw-r--r--chrome/browser/extensions/api/identity/identity_api.h40
-rw-r--r--chrome/browser/extensions/api/identity/identity_apitest.cc45
3 files changed, 152 insertions, 0 deletions
diff --git a/chrome/browser/extensions/api/identity/identity_api.cc b/chrome/browser/extensions/api/identity/identity_api.cc
new file mode 100644
index 0000000..1c8a8c2
--- /dev/null
+++ b/chrome/browser/extensions/api/identity/identity_api.cc
@@ -0,0 +1,67 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/identity/identity_api.h"
+
+#include "base/values.h"
+#include "chrome/browser/extensions/extension_function_dispatcher.h"
+#include "chrome/browser/signin/token_service.h"
+#include "chrome/browser/signin/token_service_factory.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
+#include "chrome/common/extensions/extension.h"
+#include "googleurl/src/gurl.h"
+
+namespace extensions {
+
+namespace {
+
+const char kInvalidClientId[] = "Invalid OAuth2 Client ID.";
+const char kInvalidScopes[] = "Invalid OAuth2 scopes.";
+
+} // namespace
+
+GetAuthTokenFunction::GetAuthTokenFunction() {}
+GetAuthTokenFunction::~GetAuthTokenFunction() {}
+
+bool GetAuthTokenFunction::RunImpl() {
+ const Extension* extension = GetExtension();
+ Extension::OAuth2Info oauth2_info = extension->oauth2_info();
+
+ if (oauth2_info.client_id.empty()) {
+ error_ = kInvalidClientId;
+ return false;
+ }
+
+ if (oauth2_info.scopes.size() == 0) {
+ error_ = kInvalidScopes;
+ return false;
+ }
+
+ AddRef(); // Balanced in OnMintTokenSuccess|Failure.
+
+ TokenService* token_service = TokenServiceFactory::GetForProfile(profile());
+
+ flow_.reset(
+ new OAuth2MintTokenFlow(profile()->GetRequestContext(), this));
+ flow_->Start(token_service->GetOAuth2LoginRefreshToken(),
+ extension->id(), oauth2_info.client_id, oauth2_info.scopes);
+
+ return true;
+}
+
+void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) {
+ result_.reset(Value::CreateStringValue(access_token));
+ SendResponse(true);
+ Release(); // Balanced in RunImpl.
+}
+
+void GetAuthTokenFunction::OnMintTokenFailure(
+ const GoogleServiceAuthError& error) {
+ error_ = error.ToString();
+ SendResponse(false);
+ Release(); // Balanced in RunImpl.
+}
+
+} // namespace extensions
diff --git a/chrome/browser/extensions/api/identity/identity_api.h b/chrome/browser/extensions/api/identity/identity_api.h
new file mode 100644
index 0000000..636f830
--- /dev/null
+++ b/chrome/browser/extensions/api/identity/identity_api.h
@@ -0,0 +1,40 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
+#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
+#pragma once
+
+#include <string>
+
+#include "base/memory/weak_ptr.h"
+#include "chrome/browser/extensions/app_notify_channel_setup.h"
+#include "chrome/browser/extensions/extension_function.h"
+#include "chrome/common/net/gaia/oauth2_mint_token_flow.h"
+
+class GoogleServiceAuthError;
+
+namespace extensions {
+
+class GetAuthTokenFunction : public AsyncExtensionFunction,
+ public OAuth2MintTokenFlow::Delegate {
+ public:
+ GetAuthTokenFunction();
+
+ private:
+ virtual ~GetAuthTokenFunction();
+ virtual bool RunImpl() OVERRIDE;
+
+ // OAuth2MintTokenFlow::Delegate implementation:
+ virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE;
+ virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
+
+ scoped_ptr<OAuth2MintTokenFlow> flow_;
+
+ DECLARE_EXTENSION_FUNCTION_NAME("experimental.identity.getAuthToken");
+};
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
diff --git a/chrome/browser/extensions/api/identity/identity_apitest.cc b/chrome/browser/extensions/api/identity/identity_apitest.cc
new file mode 100644
index 0000000..334c8848
--- /dev/null
+++ b/chrome/browser/extensions/api/identity/identity_apitest.cc
@@ -0,0 +1,45 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/identity/identity_api.h"
+#include "chrome/browser/extensions/extension_apitest.h"
+#include "chrome/common/net/gaia/google_service_auth_error.h"
+#include "chrome/common/net/gaia/oauth2_mint_token_flow.h"
+#include "chrome/common/chrome_switches.h"
+
+namespace {
+
+class IdentityInterceptor : public OAuth2MintTokenFlow::InterceptorForTests {
+ public:
+ virtual bool DoIntercept(const OAuth2MintTokenFlow* flow,
+ std::string* access_token,
+ GoogleServiceAuthError* error) OVERRIDE {
+ *access_token = "auth_token";
+ get_auth_token_called_ = true;
+ return true;
+ }
+
+ bool get_auth_token_called() const { return get_auth_token_called_; }
+
+ private:
+ bool get_auth_token_called_;
+};
+
+} // namespace
+
+class ExperimentalApiTest : public ExtensionApiTest {
+ public:
+ void SetUpCommandLine(CommandLine* command_line) {
+ ExtensionApiTest::SetUpCommandLine(command_line);
+ command_line->AppendSwitch(switches::kEnablePlatformApps);
+ command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(ExperimentalApiTest, Identity) {
+ IdentityInterceptor interceptor;
+ OAuth2MintTokenFlow::SetInterceptorForTests(&interceptor);
+ ASSERT_TRUE(RunExtensionTest("identity")) << message_;
+ ASSERT_TRUE(interceptor.get_auth_token_called());
+};