Jake Vanderwerf
5 days ago 266aa37c48222993bf7bdad6834e31bd08736f5e
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
<?php
namespace JVBase\integrations;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class CredentialsManager
{
    private static ?CredentialsManager $instance = null;
    private string $encryption_key;
    private string $option_prefix = 'jvb_integration_';
    private string $user_meta_prefix = 'jvb_integration_';
 
    private function __construct()
    {
        $this->encryption_key = $this->getEncryptionKey();
    }
 
    public static function getInstance(): CredentialsManager
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
 
    /**
     * Store encrypted credentials
     */
    public function storeCredentials(string $service, array $credentials, ?int $userID = null): bool
    {
        $credentials['updated_at'] = time();
        if ($userID) {
            return $this->storeUserCredentials($service, $credentials, $userID);
        }
        $encrypted_data = $this->encrypt(json_encode($credentials));
        return update_option($this->option_prefix . $service, $encrypted_data);
    }
 
    public function storeUserCredentials(string $service, array $credentials, int $userID): bool
    {
        $encrypted_data = $this->encrypt(json_encode($credentials));
        return update_user_meta($userID, $this->user_meta_prefix . $service, $encrypted_data);
    }
 
    /**
     * Retrieve and decrypt credentials
     */
    public function getCredentials(string $service, ?int $userID = null): array
    {
        if ($userID) {
            return $this->getUserCredentials($service, $userID);
        }
 
        $encrypted_data = get_option($this->option_prefix . $service, '');
 
        if (empty($encrypted_data)) {
            return [];
        }
 
        $decrypted_data = $this->decrypt($encrypted_data);
        return $decrypted_data ? json_decode($decrypted_data, true) : [];
    }
 
    public function getUserCredentials(string $service, int $userID): array
    {
        $encrypted_data = get_user_meta($userID, $this->user_meta_prefix . $service, true);
 
        if (empty($encrypted_data)) {
            return [];
        }
 
        $decrypted_data = $this->decrypt($encrypted_data);
        return $decrypted_data ? json_decode($decrypted_data, true) : [];
    }
 
    /**
     * Delete credentials
     */
    public function deleteCredentials(string $service, ?int $userID = null): bool
    {
 
        if ($userID) {
            return $this->deleteUserCredentials($service, $userID);
        }
        return delete_option($this->option_prefix . $service);
    }
 
    public function deleteUserCredentials(string $service, int $userID): bool
    {
        return delete_user_meta($userID, $this->user_meta_prefix . $service);
    }
 
    /**
     * Check if credentials exist
     */
    public function hasCredentials(string $service, ?int $userID = null): bool
    {
        if ($userID) {
            return $this->hasUserCredentials($service, $userID);
        }
        return !empty(get_option($this->option_prefix . $service, ''));
    }
 
    public function hasUserCredentials(string $service, int $userID): bool
    {
        $encrypted_data = get_user_meta($userID, $this->user_meta_prefix . $service, true);
        return !empty($encrypted_data);
    }
 
    /**
     * Get or create encryption key
     */
    private function getEncryptionKey(): string
    {
        $key = get_option('jvb_encryption_key');
 
        if (!$key) {
            $key = base64_encode(random_bytes(32));
            update_option('jvb_encryption_key', $key);
        }
 
        return base64_decode($key);
    }
 
    /**
     * Encrypt data
     */
    private function encrypt(string $data): string
    {
        $iv = random_bytes(16);
        $encrypted = openssl_encrypt($data, 'AES-256-CBC', $this->encryption_key, 0, $iv);
        return base64_encode($iv . $encrypted);
    }
 
    /**
     * Decrypt data
     */
    private function decrypt(string $data): ?string
    {
        $data = base64_decode($data);
        $iv = substr($data, 0, 16);
        $encrypted = substr($data, 16);
 
        return openssl_decrypt($encrypted, 'AES-256-CBC', $this->encryption_key, 0, $iv);
    }
 
    /**
     * Get all users with credentials for a service
     */
    public function getUsersWithCredentials(string $service): array
    {
        global $wpdb;
 
        $meta_key = $this->user_meta_prefix . $service;
        $results = $wpdb->get_results($wpdb->prepare(
            "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s",
            $meta_key
        ));
 
        return wp_list_pluck($results, 'user_id');
    }
}