Skip to content

Commit

Permalink
Copy challenge*Key methods to enterprise.platformKeys.
Browse files Browse the repository at this point in the history
Up to this point the challengeMachineKey and challengeUserKey methods
were only available through enterprise.platformKeysPrivate because the
web APIs required to meaningfully use these methods had not been
published (and now have).

The methods will remain in enterprise.platformKeysPrivate until all
users have been migrated to use enterprise.platformKeys.

The implementation is identical and has been abstracted so it can be
invoked from either entry point.

BUG=chromium:329341
TEST=unit tests extended to test both entry points

Review URL: https://codereview.chromium.org/1685103003

Cr-Commit-Position: refs/heads/master@{#377845}
  • Loading branch information
dkrahn authored and Commit bot committed Feb 26, 2016
1 parent 1f40e53 commit 3f7776e
Show file tree
Hide file tree
Showing 11 changed files with 1,062 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ const char kErrorInternal[] = "Internal Error.";
const char kErrorInvalidX509Cert[] =
"Certificate is not a valid X.509 certificate.";

std::vector<char> VectorFromString(const std::string& s) {
return std::vector<char>(s.begin(), s.end());
}

std::string StringFromVector(const std::vector<char>& v) {
return std::string(v.begin(), v.end());
}

} // namespace

EnterprisePlatformKeysInternalGenerateKeyFunction::
Expand Down Expand Up @@ -236,4 +244,85 @@ void EnterprisePlatformKeysInternalGetTokensFunction::OnGotTokens(
Respond(ArgumentList(api_epki::GetTokens::Results::Create(token_ids)));
}

EnterprisePlatformKeysChallengeMachineKeyFunction::
EnterprisePlatformKeysChallengeMachineKeyFunction()
: default_impl_(new EPKPChallengeMachineKey), impl_(default_impl_.get()) {}

EnterprisePlatformKeysChallengeMachineKeyFunction::
EnterprisePlatformKeysChallengeMachineKeyFunction(
EPKPChallengeMachineKey* impl_for_testing)
: impl_(impl_for_testing) {}

EnterprisePlatformKeysChallengeMachineKeyFunction::
~EnterprisePlatformKeysChallengeMachineKeyFunction() = default;

ExtensionFunction::ResponseAction
EnterprisePlatformKeysChallengeMachineKeyFunction::Run() {
scoped_ptr<api_epk::ChallengeMachineKey::Params> params(
api_epk::ChallengeMachineKey::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
ChallengeKeyCallback callback = base::Bind(
&EnterprisePlatformKeysChallengeMachineKeyFunction::OnChallengedKey,
this);
// base::Unretained is safe on impl_ since its life-cycle matches |this| and
// |callback| holds a reference to |this|.
base::Closure task = base::Bind(
&EPKPChallengeMachineKey::Run, base::Unretained(impl_),
scoped_refptr<UIThreadExtensionFunction>(AsUIThreadExtensionFunction()),
callback, StringFromVector(params->challenge));
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, task);
return RespondLater();
}

void EnterprisePlatformKeysChallengeMachineKeyFunction::OnChallengedKey(
bool success,
const std::string& data) {
if (success) {
Respond(ArgumentList(
api_epk::ChallengeMachineKey::Results::Create(VectorFromString(data))));
} else {
Respond(Error(data));
}
}

EnterprisePlatformKeysChallengeUserKeyFunction::
EnterprisePlatformKeysChallengeUserKeyFunction()
: default_impl_(new EPKPChallengeUserKey), impl_(default_impl_.get()) {}

EnterprisePlatformKeysChallengeUserKeyFunction::
EnterprisePlatformKeysChallengeUserKeyFunction(
EPKPChallengeUserKey* impl_for_testing)
: impl_(impl_for_testing) {}

EnterprisePlatformKeysChallengeUserKeyFunction::
~EnterprisePlatformKeysChallengeUserKeyFunction() = default;

ExtensionFunction::ResponseAction
EnterprisePlatformKeysChallengeUserKeyFunction::Run() {
scoped_ptr<api_epk::ChallengeUserKey::Params> params(
api_epk::ChallengeUserKey::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
ChallengeKeyCallback callback = base::Bind(
&EnterprisePlatformKeysChallengeUserKeyFunction::OnChallengedKey, this);
// base::Unretained is safe on impl_ since its life-cycle matches |this| and
// |callback| holds a reference to |this|.
base::Closure task = base::Bind(
&EPKPChallengeUserKey::Run, base::Unretained(impl_),
scoped_refptr<UIThreadExtensionFunction>(AsUIThreadExtensionFunction()),
callback, StringFromVector(params->challenge), params->register_key);
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, task);
return RespondLater();
}

void EnterprisePlatformKeysChallengeUserKeyFunction::OnChallengedKey(
bool success,
const std::string& data) {
if (success) {
Respond(ArgumentList(
api_epk::ChallengeUserKey::Results::Create(VectorFromString(data))));
} else {
Respond(Error(data));
}
}

} // namespace extensions
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/extensions/api/enterprise_platform_keys_private/enterprise_platform_keys_private_api.h"
#include "extensions/browser/extension_function.h"

namespace net {
class X509Certificate;
Expand All @@ -20,7 +21,7 @@ typedef std::vector<scoped_refptr<X509Certificate> > CertificateList;
namespace extensions {

class EnterprisePlatformKeysInternalGenerateKeyFunction
: public ChromeUIThreadExtensionFunction {
: public UIThreadExtensionFunction {
private:
~EnterprisePlatformKeysInternalGenerateKeyFunction() override;
ResponseAction Run() override;
Expand All @@ -35,7 +36,7 @@ class EnterprisePlatformKeysInternalGenerateKeyFunction
};

class EnterprisePlatformKeysGetCertificatesFunction
: public ChromeUIThreadExtensionFunction {
: public UIThreadExtensionFunction {
private:
~EnterprisePlatformKeysGetCertificatesFunction() override;
ResponseAction Run() override;
Expand All @@ -50,7 +51,7 @@ class EnterprisePlatformKeysGetCertificatesFunction
};

class EnterprisePlatformKeysImportCertificateFunction
: public ChromeUIThreadExtensionFunction {
: public UIThreadExtensionFunction {
private:
~EnterprisePlatformKeysImportCertificateFunction() override;
ResponseAction Run() override;
Expand All @@ -64,7 +65,7 @@ class EnterprisePlatformKeysImportCertificateFunction
};

class EnterprisePlatformKeysRemoveCertificateFunction
: public ChromeUIThreadExtensionFunction {
: public UIThreadExtensionFunction {
private:
~EnterprisePlatformKeysRemoveCertificateFunction() override;
ResponseAction Run() override;
Expand All @@ -78,7 +79,7 @@ class EnterprisePlatformKeysRemoveCertificateFunction
};

class EnterprisePlatformKeysInternalGetTokensFunction
: public ChromeUIThreadExtensionFunction {
: public UIThreadExtensionFunction {
private:
~EnterprisePlatformKeysInternalGetTokensFunction() override;
ResponseAction Run() override;
Expand All @@ -92,6 +93,52 @@ class EnterprisePlatformKeysInternalGetTokensFunction
ENTERPRISE_PLATFORMKEYSINTERNAL_GETTOKENS);
};

class EnterprisePlatformKeysChallengeMachineKeyFunction
: public UIThreadExtensionFunction {
public:
EnterprisePlatformKeysChallengeMachineKeyFunction();
explicit EnterprisePlatformKeysChallengeMachineKeyFunction(
EPKPChallengeMachineKey* impl_for_testing);

private:
~EnterprisePlatformKeysChallengeMachineKeyFunction() override;
ResponseAction Run() override;

// Called when the challenge operation is complete. If the operation succeeded
// |success| will be true and |data| will contain the challenge response data.
// Otherwise |success| will be false and |data| is an error message.
void OnChallengedKey(bool success, const std::string& data);

scoped_ptr<EPKPChallengeMachineKey> default_impl_;
EPKPChallengeMachineKey* impl_;

DECLARE_EXTENSION_FUNCTION("enterprise.platformKeys.challengeMachineKey",
ENTERPRISE_PLATFORMKEYS_CHALLENGEMACHINEKEY);
};

class EnterprisePlatformKeysChallengeUserKeyFunction
: public UIThreadExtensionFunction {
public:
EnterprisePlatformKeysChallengeUserKeyFunction();
explicit EnterprisePlatformKeysChallengeUserKeyFunction(
EPKPChallengeUserKey* impl_for_testing);

private:
~EnterprisePlatformKeysChallengeUserKeyFunction() override;
ResponseAction Run() override;

// Called when the challenge operation is complete. If the operation succeeded
// |success| will be true and |data| will contain the challenge response data.
// Otherwise |success| will be false and |data| is an error message.
void OnChallengedKey(bool success, const std::string& data);

scoped_ptr<EPKPChallengeUserKey> default_impl_;
EPKPChallengeUserKey* impl_;

DECLARE_EXTENSION_FUNCTION("enterprise.platformKeys.challengeUserKey",
ENTERPRISE_PLATFORMKEYS_CHALLENGEUSERKEY);
};

} // namespace extensions

#endif // CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_PLATFORM_KEYS_ENTERPRISE_PLATFORM_KEYS_API_H_
Loading

0 comments on commit 3f7776e

Please sign in to comment.