-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnlockStatus.h
More file actions
222 lines (173 loc) · 7.55 KB
/
UnlockStatus.h
File metadata and controls
222 lines (173 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
==============================================================================
UnlockStatus.h
Created: 13 Jul 2019 8:32:50pm
Author: Edwin Park
==============================================================================
*/
#pragma once
#include "ProductInfo.h"
#include "Versioning.h"
#include <juce_core/juce_core.h>
#include <juce_data_structures/juce_data_structures.h>
using juce::ApplicationProperties;
using juce::CriticalSection;
using juce::int64;
using juce::ScopedLock;
using juce::String;
using juce::Time;
using juce::ValueTree;
using juce::var;
using juce::WebInputStream;
class UnlockStatus {
public:
// UnlockResult
// ==============================================================================
/** This provides some details about the reply that the server gave in a call
to attemptWebserverUnlock().
*/
struct UnlockResult {
/** If an unlock operation fails, this is the error message that the
webserver supplied (or a message saying that the server couldn't be
contacted)
*/
String errorMessage;
/** This is a message that the webserver returned, and which the user should
be shown.
It's not necessarily an error message, e.g. it might say that there's a
new version of the app available or some other status update.
*/
String informativeMessage;
/** If the unlock operation succeeded, this will be set to true. */
bool succeeded;
};
// UnlockStatus
// ========================================================================================
UnlockStatus();
~UnlockStatus();
//==============================================================================
// The following methods can be called by your app:
/** Returns true if the product has been successfully authorised for this
machine.
The reason it returns a variant rather than a bool is just to make it
marginally more tedious for crackers to work around. Hopefully if this
method gets inlined they'll need to hack all the places where you call it,
rather than just the function itself.
Bear in mind that each place where you check this return value will need
to be changed by a cracker in order to unlock your app, so the more places
you call this method, the more hassle it will be for them to find and crack
them all.
*/
inline var isUnlocked() const { return status[unlockedProp]; }
// Expiry time
// =========================================================================================
/** Returns the Time when the keyfile expires.
If a the key file obtained has an expiry time, isUnlocked will return
false and this will return a non-zero time. The interpretation of this is
up to your app but could be used for subscription based models or trial
periods.
*/
inline Time getExpiryTime() const {
return Time(static_cast<int64>(status[expiryTimeProp]));
}
// Auto-populate email
// ================================================================================
void setUserName(const String &userName);
/** Returns the user's name if known. */
String getUserName() const;
/** Optionally allows the app to provide the user's email address if
it is known.
You don't need to call this, but if you do it may save the user
typing it in.
*/
void setUserEmail(const String &userEmail);
/** Returns the user's email address if known. */
String getUserEmail() const;
// Local unlock
// =========================================================================================
/** Attempts to perform an unlock using a block of key-file data provided.
You may wish to use this as a way of allowing a user to unlock your app
by drag-and-dropping a file containing the key data, or by letting them
select such a file. This is often needed for allowing registration on
machines without internet access.
*/
bool applyKeyFile(String keyFileContent);
// Web server unlock
// ======================================================================================
/** Contacts the webserver and attempts to perform a registration with the
given user details.
The return value will either be a success, or a failure with an error
message from the server, so you should show this message to your user.
Because this method blocks while it contacts the server, you must run it
on a background thread, not on the message thread. For an easier way to
create a GUI to do the unlocking, see OnlineUnlockForm.
*/
UnlockResult attemptSignin(const String &userEmail,
const String &userPassword);
/** This method will be called if the user cancels the connection to the
webserver by clicking the cancel button in OnlineUnlockForm::OverlayComp.
The default implementation of this method does nothing but you should use
it to cancel any WebInputStreams that may be connecting.
*/
void userCancelled() {
ScopedLock lock(authLock);
if (stream != nullptr)
stream->cancel();
}
// Save/Load status
// =============================================================================================
/** Attempts to load the status from the state retrieved by getState().
Call this somewhere in your app's startup code.
*/
void load();
/** Triggers a call to saveState which you can use to store the current unlock
status in your app's settings.
*/
void save();
private:
ApplicationProperties appProps;
CriticalSection authLock;
std::unique_ptr<WebInputStream> stream;
ValueTree status;
UnlockResult handleXmlReply(XmlElement);
UnlockResult handleFailedConnection();
static const char *unlockedProp;
static const char *expiryTimeProp;
/** This must check whether a product ID string that the server returned is OK
for unlocking the current app.
*/
bool doesProductMatch(const String &licenseProductId,
const String &licenseProductVersionString) {
auto productVersion =
Version(ProductInfo::getProductVersion().toStdString());
auto licenseRange = VersionRange(licenseProductVersionString.toStdString());
return ProductInfo::getProductId() == licenseProductId &&
licenseRange.includesVersion(productVersion);
}
/** Returns the RSA public key for authenticating responses from
the server for this app.
*/
RSAKey getPublicKey() { return RSAKey(ProductInfo::getPublicKeyString()); }
/** This method must store the given string somewhere in your app's
persistent properties, so it can be retrieved later by getState().
*/
void saveState(const String &state) {
appProps.getUserSettings()->setValue("unlock_status", state);
}
/** This method must retrieve the last state that was provided by the
saveState method.
On first-run, it should just return an empty string.
*/
String getState() {
auto settings = appProps.getUserSettings();
if (settings->containsKey("unlock_status"))
return settings->getValue("unlock_status");
else
return {};
}
String getMessageForConnectionFailure(const char *websiteName,
bool isInternetConnectionWorking);
String getMessageForUnexpectedReply(const char *websiteName);
String readResponseFromWebserver();
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UnlockStatus)
};