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