1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Sync; |
4
|
|
|
|
5
|
|
|
use Illuminate\Encryption\Encrypter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait ExportsUsers |
9
|
|
|
* |
10
|
|
|
* @package Slides\Connector\Auth\Sync |
11
|
|
|
*/ |
12
|
|
|
trait ExportsUsers |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The supported cipher algorithm. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $cipher = 'AES-256-CBC'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The randomly generated encryption/decryption key. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $encryptionKey; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Export local users to a file in the compressed GZIP format. |
30
|
|
|
* |
31
|
|
|
* @param string $path |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function export(string $path) |
36
|
|
|
{ |
37
|
|
|
$users = $this->formatLocals($this->locals); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$data = json_encode($users); |
40
|
|
|
|
41
|
|
|
$data = gzencode($data); |
42
|
|
|
|
43
|
|
|
$data = $this->encrypt($data); |
44
|
|
|
|
45
|
|
|
$data = $this->makeHeaders($data); |
46
|
|
|
|
47
|
|
|
file_put_contents($path, $data); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get a generated encryption key. |
52
|
|
|
* |
53
|
|
|
* @return string|null |
54
|
|
|
*/ |
55
|
|
|
public function getEncryptionKey() |
56
|
|
|
{ |
57
|
|
|
if(!$this->encryptionKey) { |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return base64_encode($this->encryptionKey); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Encrypt a data and retrieve the key. |
66
|
|
|
* |
67
|
|
|
* @param string $data |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
private function encrypt(string &$data): string |
72
|
|
|
{ |
73
|
|
|
$encrypter = new Encrypter( |
74
|
|
|
$this->generateRandomKey(), |
75
|
|
|
$this->cipher |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $encrypter->encrypt($data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Generate a random key for the dump. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
* |
86
|
|
|
* @throws |
87
|
|
|
*/ |
88
|
|
|
private function generateRandomKey(): string |
89
|
|
|
{ |
90
|
|
|
$this->encryptionKey = random_bytes(16); |
91
|
|
|
|
92
|
|
|
// Create a signature of remote service credentials |
93
|
|
|
$sign = hash('sha256', $this->credential('public') . $this->credential('secret')); |
94
|
|
|
|
95
|
|
|
return $this->encryptionKey . ':' . substr($sign, 0, 15); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Make the headers. |
100
|
|
|
* |
101
|
|
|
* @param string $payload |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
private function makeHeaders(string $payload): string |
106
|
|
|
{ |
107
|
|
|
$headers = [ |
108
|
|
|
'public' => $this->credential('public'), |
109
|
|
|
'modes' => $this->modes |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$headers = serialize($headers); |
113
|
|
|
|
114
|
|
|
return base64_encode($headers) . '/' . $payload; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The credentials. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
private function credentials(): array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
'public' => config('connector.credentials.auth.public'), |
126
|
|
|
'secret' => config('connector.credentials.auth.secret') |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Retrieve a credential value |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function credential(string $key) |
136
|
|
|
{ |
137
|
|
|
return array_get($this->credentials(), $key); |
138
|
|
|
} |
139
|
|
|
} |