-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ClientCertificateCredential (#3578)
* Add ClientCertificateCredential * Update unit test * cspell * Update Readme * Cosmetic fixes * Changelog to mention env cred update * Fix warning * cspell * Tell CI to install openssl * openssl for all Windows * update dependency manifest * Re-phrase changelog * Clang warnings * Clang warning * Clang warning - 2 * Ubuntu18 warning * Update sdk/identity/azure-identity/CHANGELOG.md Co-authored-by: Victor Vazquez <[email protected]> * PR feedback Co-authored-by: Anton Kolesnyk <[email protected]> Co-authored-by: Victor Vazquez <[email protected]>
- Loading branch information
1 parent
d1be7c8
commit 5cb6086
Showing
17 changed files
with
793 additions
and
15 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
96 changes: 96 additions & 0 deletions
96
sdk/identity/azure-identity/inc/azure/identity/client_certificate_credential.hpp
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,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
/** | ||
* @file | ||
* @brief Client Certificate Credential and options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "azure/identity/dll_import_export.hpp" | ||
|
||
#include <azure/core/credentials/credentials.hpp> | ||
#include <azure/core/credentials/token_credential_options.hpp> | ||
#include <azure/core/url.hpp> | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
namespace Azure { namespace Identity { | ||
namespace _detail { | ||
class TokenCredentialImpl; | ||
} // namespace _detail | ||
|
||
/** | ||
* @brief Options for client certificate authentication. | ||
* | ||
*/ | ||
struct ClientCertificateCredentialOptions final : public Core::Credentials::TokenCredentialOptions | ||
{ | ||
}; | ||
|
||
/** | ||
* @brief Client Certificate Credential authenticates with the Azure services using a Tenant ID, | ||
* Client ID and a client certificate. | ||
* | ||
*/ | ||
class ClientCertificateCredential final : public Core::Credentials::TokenCredential { | ||
private: | ||
std::unique_ptr<_detail::TokenCredentialImpl> m_tokenCredentialImpl; | ||
Core::Url m_requestUrl; | ||
std::string m_requestBody; | ||
std::string m_tokenHeaderEncoded; | ||
std::string m_tokenPayloadStaticPart; | ||
void* m_pkey; | ||
|
||
public: | ||
/** | ||
* @brief Constructs a Client Secret Credential. | ||
* | ||
* @param tenantId Tenant ID. | ||
* @param clientId Client ID. | ||
* @param clientCertificatePath Client certificate path. | ||
* @param options Options for token retrieval. | ||
*/ | ||
explicit ClientCertificateCredential( | ||
std::string const& tenantId, | ||
std::string const& clientId, | ||
std::string const& clientCertificatePath, | ||
Core::Credentials::TokenCredentialOptions const& options | ||
= Core::Credentials::TokenCredentialOptions()); | ||
|
||
/** | ||
* @brief Constructs a Client Secret Credential. | ||
* | ||
* @param tenantId Tenant ID. | ||
* @param clientId Client ID. | ||
* @param clientCertificatePath Client certificate path. | ||
* @param options Options for token retrieval. | ||
*/ | ||
explicit ClientCertificateCredential( | ||
std::string const& tenantId, | ||
std::string const& clientId, | ||
std::string const& clientCertificatePath, | ||
ClientCertificateCredentialOptions const& options); | ||
|
||
/** | ||
* @brief Destructs `%ClientCertificateCredential`. | ||
* | ||
*/ | ||
~ClientCertificateCredential() override; | ||
|
||
/** | ||
* @brief Gets an authentication token. | ||
* | ||
* @param tokenRequestContext A context to get the token in. | ||
* @param context A context to control the request lifetime. | ||
* | ||
* @throw Azure::Core::Credentials::AuthenticationException Authentication error occurred. | ||
*/ | ||
Core::Credentials::AccessToken GetToken( | ||
Core::Credentials::TokenRequestContext const& tokenRequestContext, | ||
Core::Context const& context) const override; | ||
}; | ||
|
||
}} // namespace Azure::Identity |
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
42 changes: 42 additions & 0 deletions
42
sdk/identity/azure-identity/samples/client_certificate_credential.cpp
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,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <iostream> | ||
|
||
#include <azure/identity/client_certificate_credential.hpp> | ||
|
||
#include <azure/service/client.hpp> | ||
|
||
// These functions should be getting the real Tenant ID, Client ID, and the Client Certificate to | ||
// authenticate. | ||
std::string GetTenantId() { return std::string(); } | ||
std::string GetClientId() { return std::string(); } | ||
std::string GetClientCertificatePath() { return std::string(); } | ||
|
||
int main() | ||
{ | ||
try | ||
{ | ||
// Step 1: Initialize Client Certificate Credential. | ||
auto clientCertificateCredential | ||
= std::make_shared<Azure::Identity::ClientCertificateCredential>( | ||
GetTenantId(), GetClientId(), GetClientCertificatePath()); | ||
|
||
// Step 2: Pass the credential to an Azure Service Client. | ||
Azure::Service::Client azureServiceClient("serviceUrl", clientCertificateCredential); | ||
|
||
// Step 3: Start using the Azure Service Client. | ||
azureServiceClient.DoSomething(Azure::Core::Context::ApplicationContext); | ||
|
||
std::cout << "Success!" << std::endl; | ||
} | ||
catch (const Azure::Core::Credentials::AuthenticationException& exception) | ||
{ | ||
// Step 4: Handle authentication errors, if needed | ||
// (invalid credential parameters, insufficient permissions). | ||
std::cout << "Authentication error: " << exception.what() << std::endl; | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.