Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

ProviderHelper::encryptClientSecret()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\helpers;
10
11
use Craft;
12
use craft\helpers\StringHelper;
13
use flipbox\patron\Patron;
14
use League\OAuth2\Client\Provider\AbstractProvider;
15
use ReflectionClass;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class ProviderHelper
22
{
23
    /**
24
     * @param $provider
25
     * @return string
26
     */
27
    public static function displayName($provider): string
28
    {
29
        $reflect = new ReflectionClass(
30
            $provider
31
        );
32
33
        // Split capital letters
34
        $parts = preg_split("/(?<=[a-z])(?![a-z])/", $reflect->getShortName(), -1, PREG_SPLIT_NO_EMPTY);
35
36
        // Assemble
37
        return StringHelper::toString($parts, ' ');
38
    }
39
40
    /**
41
     * @param AbstractProvider $provider
42
     * @param array $properties
43
     * @return array
44
     */
45
    public static function getProtectedProperties(
46
        AbstractProvider $provider,
47
        array $properties
48
    ): array {
49
        $reflection = new ReflectionClass($provider);
50
51
        $values = [];
52
53
        foreach ($properties as $property) {
54
            $values[] = static::getProtectedProperty(
55
                $provider,
56
                $reflection,
57
                $property
58
            );
59
        }
60
61
        return $values;
62
    }
63
64
    /**
65
     * @param string $value
66
     * @param bool $checkSettings
67
     * @return string
68
     */
69
    public static function encryptClientSecret(string $value, bool $checkSettings = true): string
70
    {
71
        if ($checkSettings === true && Patron::getInstance()->getSettings()->getEncryptStorageData() != true) {
72
            return $value;
73
        }
74
75
        return base64_encode(Craft::$app->getSecurity()->encryptByKey($value));
76
    }
77
78
    /**
79
     * @param string $value
80
     * @param bool $checkSettings
81
     * @return string
82
     */
83
    public static function decryptClientSecret(string $value, bool $checkSettings = true): string
84
    {
85
        if ($checkSettings === true && Patron::getInstance()->getSettings()->getEncryptStorageData() != true) {
86
            return $value;
87
        }
88
89
        try {
90
            return Craft::$app->getSecurity()->decryptByKey(
91
                base64_decode($value)
92
            );
93
        } catch (\Exception $e) {
94
            Patron::error(
95
                Craft::t(
96
                    'patron',
97
                    "Unable to decrypt client secret '{secret}'. Message: '{message}'",
98
                    [
99
                        'secret' => $value,
100
                        'message' => $e->getMessage()
101
                    ]
102
                )
103
            );
104
        }
105
106
        return $value;
107
    }
108
109
110
    /**
111
     * @param AbstractProvider $provider
112
     * @param ReflectionClass $reflectionClass
113
     * @param string $property
114
     * @return mixed
115
     */
116
    public static function getProtectedProperty(
117
        AbstractProvider $provider,
118
        ReflectionClass $reflectionClass,
119
        string $property
120
    ) {
121
        $reflectionProperty = $reflectionClass->getProperty($property);
122
        $reflectionProperty->setAccessible(true);
123
        return $reflectionProperty->getValue($provider);
124
    }
125
}
126