Skip to content

Commit

Permalink
v1.1 - adding support to User endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Ente committed Sep 10, 2024
1 parent 2d89057 commit 68b0f52
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 86 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v1.1

* added support to User authentication endpoints `login`, `register`, `associate`, `dissociate`, `getUsers`, `changePassword`, `resetPasswordToken`, `removeUser` <!-- I simply forgot this endpoint group exists aswell or it has been added newly. -->
* added support to Customer endpoint `get_customer_licenses_by_secret`

<!-- additionally removed some unused code and fixed 2 things-->

## v1.0

* added support to Data endpoints `addDataObject`, `listDataObject`, `incrementIntValue`, `decrementIntValue`, `setStringValue`, `setIntValue`, `removeDataObject`, `uploadValues`
Expand Down
5 changes: 3 additions & 2 deletions Cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Cryptolens {
public const CRYPTOLENS_LICENSE = "License";

public const CRYPTOLENS_DATA = "Data";

public const CRYPTOLENS_USER = "User";

private string $token;

private int $version = 2;

private int $productId;

public static int $output;
Expand Down Expand Up @@ -88,6 +88,7 @@ public static function loader(){
require_once dirname(__FILE__) . "/classes/Subscription.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Customer.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Analytics.cryptolens.php";
require_once dirname(__FILE__) . "/classes/User.cryptolens.php";
@require_once dirname(__FILE__) . "/classes/License.cryptolens.php";


Expand Down
58 changes: 1 addition & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ In a real values for you can be obtained as follows:
### Offline license validation

The API also allows you to validate license files via the `License.cryptolens.php` file.
To properly configure this function, please convert the XML-styled public key into PEM format (PKC#1) and save it into the `classes/*` directory as `key.pub` (if `License(<Cryptolens $cryptolens>, <string $pathToKey>)` $pathToKey is set, the path is overwritten by specified one).
To properly configure this function, please convert the XML-styled public key into PEM format (PKC#1) and save it into the `classes/*` directory as `key.pub` (if `License(<Cryptolens $cryptolens>, <string $pathToKey>)` $pathToKey is set, the path is overwritten by specified one). Currently, only "other languages" formatted license files can be processed by this API client.

You can convert your key on a site like this one [here](https://the-x.cn/en-US/certificate/XmlToPem.aspx) or [using this repository](https://github.com/MisterDaneel/PemToXml)

Expand Down Expand Up @@ -116,59 +116,3 @@ use Cryptolens_PHP_Client\Cryptolens;
```

to automatically load the required classes.


## Endpoints

* Key
* [x] activate
* [x] deactivate
* [x] create_key
* [x] create_trial_key
* [x] create_key_from_template
* [x] get_key
* [x] add_feature
* [x] block_key
* [x] extend_license
* [x] remove_feature
* [x] unblock_key
* [x] machine_lock_limit
* [ ] change_notes\*
* [ ] change_reseller\*
* [ ] change_customer\*
* [ ] Offline Verification
* Customer
* [x] add_customer
* [x] edit_customer
* [x] remove_customer
* [x] get_customer_licenses (does not support the GetCustomerLicensesBySecret Method, yet)
* [x] get_customers
* Data Object
* Product
* [x] get_keys
* [x] get_products
* Auth
* [x] key_lock **(Use with caution\*\*)**
* Payment Form
* [x] create_session
* Analytics
* [x] register_event
* [x] register_events
* [x] get_events
* [x] get_object_log
* [x] get_web_api_log
* Message
* [x] get_messages
* [x] create_message
* [x] remove_message
* Subscription
* [x] record_usage
* Reseller
* [x] add_reseller
* [x] edit_reseller
* [x] remove_reseller
* [x] get_resellers
* [x] get_reseller_customers

* = Considered with less priority, therefore this endpoint will not be implemented, yet.
* ** = This method creates, retrieves or contains sensitive information (e.g. Access Tokens)
22 changes: 22 additions & 0 deletions classes/Customer.cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ public function get_customer_licenses(int $customerId, bool $detailed = false, b
}
}

/**
* `get_customer_licenses_by_secret()` - Allows you to retrieve all the customers licenses
*
* @param integer $secret The secret of the user to retrieve the licenses from
* @param bool $detailed If set to true, the license will be returned as a License object, default is just returning the serial key (default: false)
* @param bool $metadata If set to false, additional information such as number of activated devices will not be returned (default: true)
* @return array|false Either an array containing the licenses or false
*/
public function get_customer_licenses_by_secret(int $secret, bool $detailed = false, bool $metadata = true){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, array("Secret" => $secret, "Detailed" => $detailed, "Metdata" => $metadata));
$c = Helper::connection($parms, "getCustomerLicensesBySecret", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `get_customers()` - Allows you to retrieve all customers
*
Expand Down
14 changes: 13 additions & 1 deletion classes/Endpoints.cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Endpoints {
"editCustomer" => "https://api.cryptolens.io/api/customer/EditCustomer",
"removeCustomer" => "https://api.cryptolens.io/api/customer/RemoveCustomer",
"getCustomerLicenses" => "https://api.cryptolens.io/api/customer/GetCustomerLicenses",
"getCustomerLicensesBySecret" => "https://api.cryptolens.io/api/customer/GetCustomerLicensesBySecret",
"getCustomers" => "https://api.cryptolens.io/api/customer/GetCustomers",
# Analytics
"registerEvent" => "https://api.cryptolens.io/api/ai/RegisterEvent",
Expand All @@ -54,7 +55,16 @@ class Endpoints {
"setStringValue" => "https://api.cryptolens.io/api/data/SetStringValue",
"setIntValue" => "https://api.cryptolens.io/api/data/SetIntValue",
"removeDataObject" => "https://api.cryptolens.io/api/data/RemoveDataObject",
"uploadValues" => "https://api.cryptolens.io/api/data/UploadValuesToKey"
"uploadValues" => "https://api.cryptolens.io/api/data/UploadValuesToKey",
# User
"login" => "https://api.cryptolens.io/api/userauth/Login",
"register" => "https://api.cryptolens.io/api/userauth/Register",
"associate" => "https://api.cryptolens.io/api/userauth/Associate",
"dissociate" => "https://api.cryptolens.io/api/userauth/Dissociate",
"getUsers" => "https://api.cryptolens.io/api/userauth/GetUsers",
"changePassword" => "https://api.cryptolens.io/api/userauth/ChangePassword",
"resetPasswordToken" => "https://api.cryptolens.io/api/userauth/ResetPasswordToken",
"removeUser" => "https://api.cryptolens.io/api/userauth/RemoveUser"
];

public static array $no_response_check = [
Expand All @@ -65,6 +75,8 @@ class Endpoints {
"getResellers",
"getResellerCustomers",
"getCustomerLicenses",
"getCustomerLicensesBySecret",
"getUsers",
"getCustomers",
"getEvents",
"getWebAPILog",
Expand Down
3 changes: 1 addition & 2 deletions classes/Errors.cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
*/
class Errors extends \Exception {
public function __construct($message, $code = 0, \Exception $previous = null){
#$this->error_rep($message);
parent::__construct($message, $code, $previous);
}
public function __toString(){
public function __toString(): string{
return __CLASS__ . ": [($this->code}]: {$this->message}\n";
}

Expand Down
3 changes: 1 addition & 2 deletions classes/Helper.cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static function build_params($token, $product_id, $key = null, $machineid
}
$parms[$key] = $value;
}
#print_r(var_dump($parms));
} else {
echo "\$additional_flags is not parsed as an array!";
}
Expand All @@ -41,7 +40,7 @@ public static function build_params($token, $product_id, $key = null, $machineid
foreach($parms as $i => $x){
if (is_array($x)) {
// detect array an skip
continue; // Überspringe die Kodierung für diesen Durchlauf
continue;
}
if($first) { $first = false; } else { $postfields .= '&';}

Expand Down
87 changes: 69 additions & 18 deletions classes/License.cryptolens.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,114 @@
<?php
namespace Cryptolens_PHP_Client {
use OpenSSLAsymmetricKey;

class License {
private Cryptolens $cryptolens;
class License
{

private string $group;

private string $publicKeyFile;

private bool $publicKeyIsXMLFormat;

public function __construct(Cryptolens $cryptolens){
$this->cryptolens = $cryptolens;
public function __construct(bool $publicKeyIsXMLFormat = false, string $publicKeyFile = null)
{
$this->group = Cryptolens::CRYPTOLENS_LICENSE;

if ($publicKeyFile != null) {
$this->publicKeyFile = $publicKeyFile;
} else {
$this->publicKeyFile = dirname(__FILE__, 1) . "/key.pub";
}

$this->publicKeyIsXMLFormat = $publicKeyIsXMLFormat;
}

public function validateLicense(string $licenseKey, string $signature){
/**
* `validateLicense()`- Validates a licenseKey against given signature.
* @param string $licenseKey The license key
* @param string $signature The signature
* @return bool True if signature is valid and false otherwise
*/
public function validateLicense(string $licenseKey, string $signature)
{
$licenseKey = base64_decode($licenseKey);
$signature = base64_decode($signature);

$verified = openssl_verify($licenseKey, $signature, $this->getPublicKey(), OPENSSL_ALGO_SHA256);
if($verified == 1){
$verified = openssl_verify($licenseKey, $signature, $this->getPublicKey($this->publicKeyFile, $this->publicKeyIsXMLFormat), OPENSSL_ALGO_SHA256);
if ($verified == 1) {
return true;
} else {
return false;
}
}
}

public function validateLicenseFromFileContent(string $fileContent){
/**
* `validateLicenseFromFileContent` - Validates the licenseKey against signature from given license file content
* @param string $fileContent The file content as string
* @return bool True if signature is valid and false otherwise
*/
public function validateLicenseFromFileContent(string $fileContent)
{
$fileContent = json_decode($fileContent, true);
$licenseKey = base64_decode($fileContent["licenseKey"]);
$signature = base64_decode($fileContent["signature"]);

$verified = openssl_verify($licenseKey, $signature, $this->getPublicKey(), OPENSSL_ALGO_SHA256);
if($verified == 1){
$verified = openssl_verify($licenseKey, $signature, $this->getPublicKey($this->publicKeyFile, $this->publicKeyIsXMLFormat), OPENSSL_ALGO_SHA256);
if ($verified == 1) {
return true;
} else {
return false;
}
}

public function validateLicenseFromFile(string $fileContent){
$fileContent = json_decode(file_get_contents($fileContent), true);
/**
* `validateLicenseFromFile()` - Validates the licenseKey against a signature from given license key file path
* @param string $fileContent The file path of the license
* @return bool True if signature is valid and false otherwise
*/
public function validateLicenseFromFile(string $fileContent)
{
$fileContent = $this->readLicenseFile($fileContent);
$licenseKey = base64_decode($fileContent["licenseKey"]);
$signature = base64_decode($fileContent["signature"]);

$verified = openssl_verify($licenseKey, $signature, $this->getPublicKey(), OPENSSL_ALGO_SHA256);
if($verified == 1){
$verified = openssl_verify($licenseKey, $signature, $this->getPublicKey($this->publicKeyFile, $this->publicKeyIsXMLFormat), OPENSSL_ALGO_SHA256);
if ($verified == 1) {
return true;
} else {
return false;
}
}

public function getPublicKey(string $pathToKey = null){
if($pathToKey == null){
/**
* `getPublicKey()` - Reads the public key from your specified file or from default ./classes/key.pub
* @param string $pathToKey
* @return bool|\OpenSSLAsymmetricKey
*/
public function getPublicKey(string $pathToKey = null, bool $xml = false)
{
if ($pathToKey == null) {
$pathToKey = dirname(__FILE__, 1) . "/key.pub";
}
return openssl_pkey_get_public(file_get_contents($pathToKey));


if ($xml == true) {
return openssl_pkey_get_public(file_get_contents($pathToKey));
} else {
return openssl_pkey_get_public(file_get_contents($pathToKey));
}
}

/**
*
* @param string $filePath
* @return mixed
*/
public function readLicenseFile(string $filePath)
{
return json_decode(file_get_contents($filePath), true);
}
}
}

Expand Down
39 changes: 38 additions & 1 deletion classes/Results.endpoints.cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ class Results extends Endpoints {
"Metadata",
"Result",
"Message"
]
],
"getCustomerLicensesBySecret" => [
"LicenseKeys",
"Metadata",
"Result",
"Message"
]
],
"Data" => [
"addDataObject" => [
Expand Down Expand Up @@ -193,6 +199,37 @@ class Results extends Endpoints {
"Result",
"Message"
]
],
"User" => [
"login" => [
"Result",
"Message"
],
"register" => [
"Result",
"Message"
],
"associate" => [
"Result",
"Message"
],
"dissociate" => [
"Result",
"Message"
],
"getUsers" => [
"Users",
"Result",
"Message"
],
"changePassword" => [
"Result",
"Message"
],
"removeUser" => [
"Result",
"Message"
]
]

];
Expand Down
Loading

0 comments on commit 68b0f52

Please sign in to comment.