Skip to content

Commit b4cc90a

Browse files
committed
API Key Identification and Token Service update
- Added security mode selection to the Context with Basic Authorization and API Key Identification modes - Token Service cleaned up and completed with all supported operations - Various minor cleanups
1 parent 92adc72 commit b4cc90a

File tree

12 files changed

+159
-44
lines changed

12 files changed

+159
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TestResults
77

88
## Ignore Visual Studio temporary files, build results, and
99
## files generated by popular Visual Studio add-ons.
10+
NetLicensingClient/NetLicensingClient.xml
1011

1112
# User-specific files
1213
*.suo

NetLicensingClient-demo/NetLicensingClient-demo.cs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,39 @@ class NetLicensingClient_demo
1212
{
1313
static void Main(string[] args)
1414
{
15-
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; // Trust the self-signed certificate at NetLicensing.labs64.com.
15+
// ServicePointManager.ServerCertificateValidationCallback = delegate { return true; // Trust any (self-signed) certificate };
16+
1617
Context context = new Context();
1718
context.baseUrl = "https://netlicensing.labs64.com";
1819
context.username = "demo";
1920
context.password = "demo";
21+
context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
2022

2123
String demoProductNumber = "P001demo";
2224
String demoProductModuleNumber = "M001demo";
2325
String demoLicensingModel = "TimeLimitedEvaluation";
24-
2526
String demoLicenseTemplate1_Number = "E001demo";
2627
String demoLicenseTemplate1_Name = "Demo Evaluation Period";
2728
String demoLicenseTemplate1_Type = "FEATURE";
2829
Decimal demoLicenseTemplate1_Price = 12.50M;
2930
String demoLicenseTemplate1_Currency = "EUR";
3031
Boolean demoLicenseTemplate1_Automatic = false;
3132
Boolean demoLicenseTemplate1_Hidden = false;
32-
33-
3433
String demoLicenseeNumber = "I001demo";
35-
3634
String demoLicenseNumber = "L001demoTV";
3735

38-
String demoTokenType = "SHOP";
39-
40-
4136
try
4237
{
4338

39+
#region ****************** Lists
40+
4441
List<String> licenseTypes = UtilityService.listLicenseTypes(context);
4542
ConsoleWriter.WriteList("License Types:", licenseTypes);
4643

4744
List<String> licensingModels = UtilityService.listLicensingModels(context);
4845
ConsoleWriter.WriteList("Licensing Models:", licensingModels);
4946

47+
#endregion
5048

5149
#region ****************** Product
5250

@@ -156,7 +154,6 @@ static void Main(string[] args)
156154

157155
#endregion
158156

159-
160157
#region ****************** Licensee
161158

162159
Licensee newLicensee = new Licensee();
@@ -171,7 +168,7 @@ static void Main(string[] args)
171168
ConsoleWriter.WriteMsg("Deleted licensee!");
172169

173170
licensees = LicenseeService.list(context, null);
174-
ConsoleWriter.WriteList("Got the following licensees:", licensees);
171+
ConsoleWriter.WriteList("Got the following licensees after delete:", licensees);
175172

176173
licensee = LicenseeService.create(context, demoProductNumber, newLicensee);
177174
ConsoleWriter.WriteEntity("Added licensee again:", licensee);
@@ -188,7 +185,6 @@ static void Main(string[] args)
188185
ConsoleWriter.WriteList("Got the following licensees:", licensees);
189186

190187
#endregion
191-
192188

193189
#region ****************** License
194190
License newLicense = new License();
@@ -225,22 +221,44 @@ static void Main(string[] args)
225221

226222
#endregion
227223

224+
#region ****************** Token
225+
226+
Token newToken = new Token();
227+
newToken.tokenType = Constants.Token.TYPE_APIKEY;
228+
Token apiKey = TokenService.create(context, newToken);
229+
ConsoleWriter.WriteEntity("Created API Key:", apiKey);
230+
context.apiKey = apiKey.number;
231+
232+
newToken.tokenType = Constants.Token.TYPE_SHOP;
233+
newToken.tokenProperties.Add(Constants.Licensee.LICENSEE_NUMBER, demoLicenseeNumber);
234+
context.securityMode = SecutiryMode.APIKEY_IDENTIFICATION;
235+
Token shopToken = TokenService.create(context, newToken);
236+
context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
237+
ConsoleWriter.WriteEntity("Got the following shop token:", shopToken);
238+
239+
List<Token> tokens = TokenService.list(context, Constants.Token.TYPE_SHOP, null);
240+
ConsoleWriter.WriteList("Got the following shop tokens:", tokens);
241+
242+
TokenService.deactivate(context, shopToken.number);
243+
ConsoleWriter.WriteMsg("Deactivated shop token!");
244+
245+
tokens = TokenService.list(context, Constants.Token.TYPE_SHOP, null);
246+
ConsoleWriter.WriteList("Got the following shop tokens after deactivate:", tokens);
247+
248+
#endregion
249+
228250
#region ****************** Validate
229251

230252
ValidationResult validationResult = LicenseeService.validate(context, demoLicenseeNumber, demoProductNumber);
231253
ConsoleWriter.WriteEntity("Validation result for created licensee:", validationResult);
232-
#endregion
233-
234-
#region ****************** Token
235254

236-
Token token = TokenService.generate(context, demoTokenType, demoLicenseeNumber);
237-
ConsoleWriter.WriteEntity("Got the following token:", token);
255+
context.securityMode = SecutiryMode.APIKEY_IDENTIFICATION;
256+
validationResult = LicenseeService.validate(context, demoLicenseeNumber, demoProductNumber);
257+
context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
258+
ConsoleWriter.WriteEntity("Validation repeated with API Key:", validationResult);
238259

239260
#endregion
240261

241-
242-
243-
244262
}
245263
catch (NetLicensingException e)
246264
{
@@ -256,7 +274,11 @@ static void Main(string[] args)
256274
{
257275
try
258276
{
259-
// Cleanup - delete test product with all its related items
277+
// Cleanup:
278+
context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
279+
// deactivate api key
280+
TokenService.deactivate(context, context.apiKey);
281+
// delete test product with all its related items
260282
ProductService.delete(context, demoProductNumber, true);
261283
}
262284
catch (NetLicensingException e)

NetLicensingClient/Context.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,48 @@
66

77
namespace NetLicensingClient
88
{
9+
10+
/// <summary>
11+
/// Enumerates possible security modes for accessing the NetLicensing API
12+
/// See https://www.labs64.de/confluence/x/pwCo#NetLicensingAPI%28RESTful%29-Security for details.
13+
/// </summary>
14+
public enum SecutiryMode
15+
{
16+
BASIC_AUTHENTICATION,
17+
APIKEY_IDENTIFICATION
18+
};
19+
920
/// <summary>
1021
/// Holds the common context for all calls to the NetLicensingAPI RESTful, in particular server URL and login credentials.
1122
/// </summary>
1223
public class Context
1324
{
1425
/// <summary>
15-
/// Server URL base of NetLicensingAPI RESTful. Normally should be "https://NetLicensing.labs64.com".
26+
/// Server URL base of the NetLicensing RESTful API. Normally should be "https://netlicensing.labs64.com".
1627
/// </summary>
1728
public String baseUrl { get; set; }
1829

1930
/// <summary>
20-
/// Login name of the user sending the requests.
31+
/// Login name of the user sending the requests when securityMode = BASIC_AUTHENTICATION.
2132
/// </summary>
2233
public String username { get; set; }
2334

2435
/// <summary>
25-
/// Password of the user sending the requests.
36+
/// Password of the user sending the requests when securityMode = BASIC_AUTHENTICATION.
2637
/// </summary>
2738
public String password { get; set; }
2839

40+
/// <summary>
41+
/// API Key used to identify the request sender when securityMode = APIKEY_IDENTIFICATION.
42+
/// </summary>
43+
public String apiKey { get; set; }
44+
45+
/// <summary>
46+
/// Determines the security mode used for accessing the NetLicensing API.
47+
/// See https://www.labs64.de/confluence/x/pwCo#NetLicensingAPI%28RESTful%29-Security for details.
48+
/// </summary>
49+
public SecutiryMode securityMode { get; set; }
50+
2951
/// <summary>
3052
/// External number of the vendor.
3153
/// </summary>

NetLicensingClient/Entities/Constants.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class Constants
1515
internal const String CASCADE = "forceCascade";
1616
internal const String PRICE = "price";
1717
internal const String CURRENCY = "currency";
18+
internal const String FILTER = "filter";
19+
internal const String APIKEY_USER = "apiKey";
1820

1921
public class Vendor
2022
{
@@ -52,7 +54,7 @@ public class Licensee
5254
{
5355
internal const String ENDPOINT_PATH = "licensee";
5456
internal const String TYPE_NAME = "Licensee";
55-
internal const String LICENSEE_NUMBER = "licenseeNumber";
57+
public const String LICENSEE_NUMBER = "licenseeNumber";
5658
}
5759

5860
public class License
@@ -74,8 +76,10 @@ public class Token
7476
internal const String ENDPOINT_PATH = "token";
7577
internal const String TYPE_NAME = "Token";
7678
internal const String TOKEN_NUMBER = "tokenNumber";
77-
internal const String TYPE_DEFAULT = "DEFAULT";
78-
internal const String TYPE_SHOP = "SHOP";
79+
internal const String TOKEN_TYPE = "tokenType";
80+
public const String TYPE_DEFAULT = "DEFAULT";
81+
public const String TYPE_SHOP = "SHOP";
82+
public const String TYPE_APIKEY = "APIKEY";
7983
}
8084

8185
public class PaymentMethod

NetLicensingClient/Entities/Token.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace NetLicensingClient.Entities
1111
/// </summary>
1212
public class Token : BaseEntity
1313
{
14+
/// <summary>
15+
/// Type of the token. See NetLicensingAPI JavaDoc for details:
16+
/// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/common/domain/entity/Token.html
17+
/// </summary>
18+
public String tokenType { get; set; }
19+
1420
/// <summary>
1521
/// Custom properties of token. See NetLicensingAPI JavaDoc for details:
1622
/// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/common/domain/entity/Token.html

NetLicensingClient/LicenseService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public static License get(Context context, String number)
4949
public static List<License> list(Context context, String filter)
5050
{
5151
Dictionary<String, String> parameters = new Dictionary<String, String>();
52-
if (filter != null && filter.Length > 0)
52+
if (!String.IsNullOrEmpty(filter))
5353
{
54-
parameters.Add("filter", filter);
54+
parameters.Add(Constants.FILTER, filter);
5555
}
5656

5757
netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.License.ENDPOINT_PATH, parameters);

NetLicensingClient/LicenseTemplateService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public static LicenseTemplate get(Context context, String number)
4242
public static List<LicenseTemplate> list(Context context, String filter)
4343
{
4444
Dictionary<String, String> parameters = new Dictionary<String, String>();
45-
if (filter != null && filter.Length > 0)
45+
if (!String.IsNullOrEmpty(filter))
4646
{
47-
parameters.Add("filter", filter);
47+
parameters.Add(Constants.FILTER, filter);
4848
}
4949

5050
netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.LicenseTemplate.ENDPOINT_PATH, parameters);

NetLicensingClient/LicenseeService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public static Licensee get(Context context, String number)
4242
public static List<Licensee> list(Context context, String filter)
4343
{
4444
Dictionary<String, String> parameters = new Dictionary<String, String>();
45-
if (filter != null && filter.Length > 0)
45+
if (!String.IsNullOrEmpty(filter))
4646
{
47-
parameters.Add("filter", filter);
47+
parameters.Add(Constants.FILTER, filter);
4848
}
4949

5050
netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Licensee.ENDPOINT_PATH, parameters);
@@ -85,7 +85,7 @@ public static void delete(Context context, String number, Boolean forceCascade)
8585
public static ValidationResult validate(Context context, String number, String productNumber)
8686
{
8787
Dictionary<String, String> parameters = new Dictionary<String, String>();
88-
if (productNumber != null || productNumber.Length > 0)
88+
if (!String.IsNullOrEmpty(productNumber))
8989
{
9090
parameters.Add("productNumber", productNumber);
9191
}

NetLicensingClient/ProductModuleService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public static ProductModule get(Context context, String number)
4242
public static List<ProductModule> list(Context context, String filter)
4343
{
4444
Dictionary<String, String> parameters = new Dictionary<String, String>();
45-
if (filter != null && filter.Length > 0)
45+
if (!String.IsNullOrEmpty(filter))
4646
{
47-
parameters.Add("filter", filter);
47+
parameters.Add(Constants.FILTER, filter);
4848
}
4949

5050
netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.ProductModule.ENDPOINT_PATH, parameters);

NetLicensingClient/ProductService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public static Product get(Context context, String number)
4141
public static List<Product> list(Context context, String filter)
4242
{
4343
Dictionary<String, String> parameters = new Dictionary<String, String>();
44-
if (filter != null && filter.Length > 0)
44+
if (!String.IsNullOrEmpty(filter))
4545
{
46-
parameters.Add("filter", filter);
46+
parameters.Add(Constants.FILTER, filter);
4747
}
4848

4949
netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Product.ENDPOINT_PATH, null);

NetLicensingClient/RestController/NetLicensingAPI.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,18 @@ public static netlicensing request(Context context, Method method, String path,
7474
// TODO: error - unsupported method
7575
break;
7676
}
77-
request.Credentials = new NetworkCredential(context.username, context.password);
77+
switch (context.securityMode)
78+
{
79+
case SecutiryMode.BASIC_AUTHENTICATION:
80+
request.Credentials = new NetworkCredential(context.username, context.password);
81+
break;
82+
case SecutiryMode.APIKEY_IDENTIFICATION:
83+
request.Credentials = new NetworkCredential(Constants.APIKEY_USER, context.apiKey);
84+
break;
85+
default:
86+
// TODO: error - unsupported security mode
87+
break;
88+
}
7889
request.PreAuthenticate = true;
7990
request.Accept = "application/xml";
8091
request.SendChunked = false;

0 commit comments

Comments
 (0)