Skip to content

Commit 3393366

Browse files
authored
Merge pull request Azure-Samples#5 from Azure-Samples/release/1.4
Release/1.4
2 parents 1bdd635 + 399656f commit 3393366

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,4 @@ ASALocalRun/
328328

329329
# MFractors (Xamarin productivity tool) working folder
330330
.mfractor/
331+
/MipSdk-FileApi-Cpp-Sample-Basic/file_sample/mip

mipsdk-protectionapi-cpp-sample-basic/action.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
#include "action.h"
2929

30-
#include "mip/mip_init.h"
3130
#include "mip/common_types.h"
3231
#include "mip/protection/protection_profile.h"
3332
#include "mip/protection/protection_engine.h"
@@ -83,16 +82,22 @@ namespace sample {
8382

8483
void sample::file::Action::AddNewProtectionProfile()
8584
{
85+
86+
87+
auto telemetryConfig = std::make_shared<mip::TelemetryConfiguration>();
88+
telemetryConfig->isTelemetryOptedOut = true;
89+
8690
//Create MipContext
8791
mMipContext = mip::MipContext::Create(
8892
mAppInfo,
8993
"mip_data",
9094
mip::LogLevel::Trace,
9195
false,
9296
nullptr /*loggerDelegateOverride*/,
93-
nullptr /*telemetryOverride*/
97+
telemetryConfig /*telemetryOverride*/
9498
);
9599

100+
96101
// Initialize ProtectionProfileSettings using MipContext
97102
ProtectionProfile::Settings profileSettings(mMipContext,
98103
mip::CacheStorageType::OnDiskEncrypted,
@@ -123,45 +128,40 @@ namespace sample {
123128
mEngine = engineFuture.get();
124129
}
125130

126-
std::shared_ptr<mip::ProtectionHandler> Action::CreateProtectionHandler(const ProtectionOptions protectionOptions)
131+
std::shared_ptr<mip::ProtectionHandler> Action::CreateProtectionHandlerForPublishing(const std::shared_ptr<mip::ProtectionDescriptor>& descriptor)
127132
{
128133
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>();
129134
auto handlerFuture = handlerPromise->get_future();
130-
auto descriptor = CreateProtectionDescriptor(protectionOptions);
131-
auto observer = std::make_shared<ProtectionHandlerObserverImpl>();
132135

133-
mEngine->CreateProtectionHandlerFromDescriptorAsync(descriptor,
134-
mip::ProtectionHandlerCreationOptions::None,
135-
observer,
136-
handlerPromise);
136+
auto handlerObserver = std::make_shared<ProtectionHandlerObserverImpl>();
137137

138+
mip::ProtectionHandler::PublishingSettings publishingSettings = mip::ProtectionHandler::PublishingSettings(descriptor);
139+
mEngine->CreateProtectionHandlerForPublishingAsync(publishingSettings, handlerObserver, handlerPromise);
140+
138141
return handlerFuture.get();
139142
}
140143

141-
std::shared_ptr<mip::ProtectionHandler> Action::CreateProtectionHandler(const std::vector<uint8_t>& serializedPublishingLicense) {
144+
std::shared_ptr<mip::ProtectionHandler> Action::CreateProtectionHandlerForConsumption(const std::vector<uint8_t>& serializedPublishingLicense) {
142145
// Note: Applications can optionally require user consent to acquire a protection handler by implementing the
143146
// ConsentDelegate interfaces and passing the object when creating a ProtectionProfile
144147

145148
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>();
146149
auto handlerFuture = handlerPromise->get_future();
147-
shared_ptr<ProtectionHandlerObserverImpl> handleObserver = std::make_shared<ProtectionHandlerObserverImpl>();
150+
shared_ptr<ProtectionHandlerObserverImpl> handlerObserver = std::make_shared<ProtectionHandlerObserverImpl>();
148151

152+
mip::ProtectionHandler::ConsumptionSettings consumptionSettings = mip::ProtectionHandler::ConsumptionSettings(serializedPublishingLicense);
153+
mEngine->CreateProtectionHandlerForConsumptionAsync(consumptionSettings, handlerObserver, handlerPromise);
149154

150-
151-
mEngine->CreateProtectionHandlerFromPublishingLicenseAsync(
152-
serializedPublishingLicense,
153-
mip::ProtectionHandlerCreationOptions::None,
154-
handleObserver,
155-
handlerPromise);
156-
return handlerFuture.get();
155+
auto h = handlerFuture.get();
156+
return h;
157157
}
158158

159159

160160
std::shared_ptr<mip::ProtectionDescriptor> Action::CreateProtectionDescriptor(const ProtectionOptions protectionOptions)
161161
{
162162
if (!protectionOptions.templateId.empty())
163163
{
164-
auto descriptorBuilder = mip::ProtectionDescriptorBuilder::CreateFromTemplate(protectionOptions.templateId);
164+
auto descriptorBuilder = mip::ProtectionDescriptorBuilder::CreateFromTemplate(protectionOptions.templateId);
165165
return descriptorBuilder->Build();
166166
}
167167
return nullptr;
@@ -191,13 +191,18 @@ namespace sample {
191191
}
192192
}
193193

194-
void Action::ProtectString(const std::string& plaintext, std::string& ciphertext, const std::vector<uint8_t>& serializedLicense)
194+
std::vector<uint8_t> Action::ProtectString(const std::string& plaintext, std::string& ciphertext, const std::string& templateId)
195195
{
196196
if (!mEngine) {
197197
AddNewProtectionEngine();
198198
}
199199

200-
auto handler = CreateProtectionHandler(serializedLicense);
200+
ProtectionOptions protectionOptions;
201+
protectionOptions.templateId = templateId;
202+
203+
auto descriptor = CreateProtectionDescriptor(protectionOptions);
204+
205+
auto handler = CreateProtectionHandlerForPublishing(descriptor);
201206
std::vector<uint8_t> outputBuffer;
202207
// std::vector<uint8_t> inputBuffer(static_cast<size_t>(plaintext.size()));
203208
std::vector<uint8_t> inputBuffer(plaintext.begin(), plaintext.end());
@@ -213,6 +218,8 @@ namespace sample {
213218

214219
std::string output(outputBuffer.begin(), outputBuffer.end());
215220
ciphertext = output;
221+
222+
return handler->GetSerializedPublishingLicense();
216223
}
217224

218225
void Action::DecryptString(std::string& plaintext, const std::string& ciphertext, const std::vector<uint8_t>& serializedLicense)
@@ -221,7 +228,7 @@ namespace sample {
221228
AddNewProtectionEngine();
222229
}
223230

224-
auto handler = CreateProtectionHandler(serializedLicense);
231+
auto handler = CreateProtectionHandlerForConsumption(serializedLicense);
225232
std::vector<uint8_t> outputBuffer(static_cast<size_t>(ciphertext.size()));
226233

227234

@@ -240,18 +247,5 @@ namespace sample {
240247
std::string output(outputBuffer.begin(), outputBuffer.end());
241248
plaintext = output;
242249
}
243-
244-
std::vector<uint8_t> Action::GetPublishingLicense(const std::string& templateId)
245-
{
246-
if (!mEngine) {
247-
AddNewProtectionEngine();
248-
}
249-
250-
ProtectionOptions protectionOptions;
251-
protectionOptions.templateId = templateId;
252-
253-
auto handler = CreateProtectionHandler(protectionOptions);
254-
return handler->GetSerializedPublishingLicense();
255-
}
256250
}
257251
}

mipsdk-protectionapi-cpp-sample-basic/action.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ namespace sample {
4848
namespace file {
4949

5050
struct ProtectionOptions {
51-
bool isAdHoc;
51+
bool isAdHoc = false;
5252
std::vector<std::string> owner;
5353
std::vector<std::string> users;
5454
std::vector<std::string> rights;
5555
std::vector<std::string> roles;
5656
std::string templateId;
57-
bool useBufferApi;
57+
bool useBufferApi = false;
5858
};
5959

6060
class Action {
@@ -66,15 +66,15 @@ namespace sample {
6666
~Action();
6767

6868
void ListTemplates(); // List all labels associated engine loaded for user
69-
void ProtectString(const std::string& plaintext, std::string& ciphertext, const std::vector<uint8_t>& serializedLicense);
69+
std::vector<uint8_t> ProtectString(const std::string& plaintext, std::string& ciphertext, const std::string& templateId);
7070
void DecryptString(std::string& plaintext, const std::string& ciphertext, const std::vector<uint8_t>& serializedLicense);
71-
std::vector<uint8_t> GetPublishingLicense(const std::string& templateId);
71+
void ShowProtection(const std::vector<uint8_t>& serializedLicense);
7272

7373
private:
7474
void AddNewProtectionProfile(); // Private function for adding and loading mip::FileProfile
7575
void AddNewProtectionEngine(); // Private function for adding/loading mip::FileEngine for specified user
76-
std::shared_ptr<mip::ProtectionHandler> CreateProtectionHandler(const ProtectionOptions protectionOptions); // Creates mip::FileHandler for specified file
77-
shared_ptr<mip::ProtectionHandler> CreateProtectionHandler(const vector<uint8_t>& serializedPublishingLicense);
76+
std::shared_ptr<mip::ProtectionHandler> CreateProtectionHandlerForPublishing(const std::shared_ptr<mip::ProtectionDescriptor>& descriptor); // Creates mip::FileHandler for specified file
77+
shared_ptr<mip::ProtectionHandler> CreateProtectionHandlerForConsumption(const vector<uint8_t>& serializedPublishingLicense);
7878
std::shared_ptr<mip::ProtectionDescriptor> CreateProtectionDescriptor(const ProtectionOptions protectionOptions);
7979

8080
std::shared_ptr<sample::auth::AuthDelegateImpl> mAuthDelegate; // AuthDelegateImpl object that will be used throughout the sample to store auth details.

mipsdk-protectionapi-cpp-sample-basic/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ int main()
9191
return 0;
9292
}
9393

94-
// Generate a new publishing license.
95-
auto publishingLicense = action.GetPublishingLicense(templateToApply);
94+
// Generate a new protection descriptor and store publishing license
95+
9696

9797
// Prompt the user to enter a file. A labeled copy of this file will be created.
9898
cout << "Enter some text to encrypt: ";
@@ -102,7 +102,7 @@ int main()
102102
cout << "Applying Label ID " + templateToApply + " to: " << endl << plaintext << endl;
103103

104104
// Protect the input string using the previously generated PL.
105-
action.ProtectString(plaintext, ciphertext, publishingLicense);
105+
auto publishingLicense = action.ProtectString(plaintext, ciphertext, templateToApply);
106106

107107
cout << "Protected output: " << endl << ciphertext << endl;
108108
cout << endl << "Decrypting string: " << endl << endl;

0 commit comments

Comments
 (0)