| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Envelope |
| 4 |
* |
| 5 |
* @package ContentControl\Vendor\TrustedLogin\Client |
| 6 |
* |
| 7 |
* @copyright 2021 Katz Web Services, Inc. |
| 8 |
* |
| 9 |
* @license GPL-2.0-or-later |
| 10 |
* Modified by code-atlantic on 18-September-2023 using Strauss. |
| 11 |
* @see https://github.com/BrianHenryIE/strauss |
| 12 |
*/ |
| 13 |
namespace ContentControl\Vendor\TrustedLogin; |
| 14 |
|
| 15 |
// Exit if accessed directly |
| 16 |
if ( ! defined('ABSPATH') ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
use \Exception; |
| 21 |
use \WP_Error; |
| 22 |
use \WP_User; |
| 23 |
use \WP_Admin_Bar; |
| 24 |
|
| 25 |
/** |
| 26 |
* The TrustedLogin all-in-one drop-in class. |
| 27 |
*/ |
| 28 |
final class Envelope { |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Config $config |
| 32 |
*/ |
| 33 |
private $config; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Encryption |
| 37 |
*/ |
| 38 |
private $encryption; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var string API key set in software. |
| 42 |
*/ |
| 43 |
private $api_key; |
| 44 |
|
| 45 |
/** |
| 46 |
* Envelope constructor. |
| 47 |
* |
| 48 |
* @param Config $config |
| 49 |
* @param Encryption $encryption |
| 50 |
*/ |
| 51 |
public function __construct( Config $config, Encryption $encryption ) { |
| 52 |
$this->config = $config; |
| 53 |
$this->api_key = $this->config->get_setting( 'auth/api_key' ); |
| 54 |
$this->encryption = $encryption; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param string $secret_id |
| 59 |
* @param string $site_identifier_hash |
| 60 |
* @param string $access_key |
| 61 |
* |
| 62 |
* @return array|WP_Error |
| 63 |
*/ |
| 64 |
public function get( $secret_id, $site_identifier_hash, $access_key = '' ) { |
| 65 |
|
| 66 |
if ( ! is_string( $secret_id ) ) { |
| 67 |
return new \WP_Error( 'secret_not_string', 'The secret ID must be a string:' . print_r( $secret_id, true ) ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! is_string( $site_identifier_hash ) ) { |
| 71 |
return new \WP_Error( 'site_identifier_not_string', 'The site identifier must be a string:' . print_r( $site_identifier_hash, true ) ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! is_string( $access_key ) ) { |
| 75 |
return new \WP_Error( 'access_key_not_string', 'The access key must be a string: ' . print_r( $access_key, true ) ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! function_exists( 'sodium_bin2hex' ) ) { |
| 79 |
return new \WP_Error( 'sodium_bin2hex_not_available', 'The sodium_bin2hex function is not available.' ); |
| 80 |
} |
| 81 |
|
| 82 |
$e_keys = $this->encryption->generate_keys(); |
| 83 |
|
| 84 |
if ( is_wp_error( $e_keys ) ){ |
| 85 |
return $e_keys; |
| 86 |
} |
| 87 |
|
| 88 |
$nonce = $this->encryption->get_nonce(); |
| 89 |
|
| 90 |
if ( is_wp_error( $nonce ) ){ |
| 91 |
return $nonce; |
| 92 |
} |
| 93 |
|
| 94 |
$encrypted_identifier = $this->encryption->encrypt( $site_identifier_hash, $nonce, $e_keys->privateKey ); |
| 95 |
|
| 96 |
if ( is_wp_error( $encrypted_identifier ) ) { |
| 97 |
return $encrypted_identifier; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Adds custom metadata to be synced via TrustedLogin |
| 102 |
* |
| 103 |
* WARNING: Metadata is transferred and stored in plain text, and **must not contain any sensitive or identifiable information**! |
| 104 |
* |
| 105 |
* @since 1.0.0 |
| 106 |
* |
| 107 |
* @param array $metadata |
| 108 |
* @param Config $config Current TrustedLogin configuration |
| 109 |
*/ |
| 110 |
$metadata = apply_filters( 'trustedlogin/' . $this->config->ns() . '/envelope/meta', array(), $this->config ); |
| 111 |
|
| 112 |
return array( |
| 113 |
'secretId' => $secret_id, |
| 114 |
'identifier' => $encrypted_identifier, |
| 115 |
'siteUrl' => get_site_url(), |
| 116 |
'publicKey' => $this->api_key, |
| 117 |
'accessKey' => $access_key, |
| 118 |
'wpUserId' => get_current_user_id(), |
| 119 |
'expiresAt' => $this->config->get_expiration_timestamp( null, true ), |
| 120 |
'version' => Client::VERSION, |
| 121 |
'nonce' => \sodium_bin2hex( $nonce ), |
| 122 |
'clientPublicKey' => \sodium_bin2hex( $e_keys->publicKey ), |
| 123 |
'metaData' => $metadata, |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|