Completed
Push — master ( d7e260...fc7aea )
by Artem
10:19
created

ExportsUsers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 126
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A credentials() 0 5 1
A makeHeaders() 0 10 1
A generateRandomKey() 0 8 1
A getEncryptionKey() 0 7 2
A credential() 0 3 1
A encrypt() 0 8 1
A export() 0 13 1
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);
0 ignored issues
show
Bug introduced by
It seems like formatLocals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        /** @scrutinizer ignore-call */ 
38
        $users = $this->formatLocals($this->locals);
Loading history...
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
}