Completed
Push — master ( 65a5eb...3ef99c )
by Bernhard
02:44 queued 01:12
created

MacOsPasswordRetrievalStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * \Wicked\Timely\PushServices\Authentication\MacOsPasswordRetrievalStrategy
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    wick-ed
15
 * @copyright 2020 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\PushServices\Authentication;
21
22
/**
23
 * Date helper
24
 *
25
 * @author    wick-ed
26
 * @copyright 2020 Bernhard Wick
27
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
28
 * @link      https://github.com/wick-ed/timely
29
 */
30
class MacOsPasswordRetrievalStrategy extends UnixPasswordRetrievalStrategy
31
{
32
    /**
33
     * Constants used within this class
34
     *
35
     * @var string
36
     */
37
    const KEYCHAIN_SAVE = 'timely jira access';
38
39
    /**
40
     * Will retrieve a stored password from the OSX keychain
41
     *
42
     * @return string
43
     *
44
     * @throws \Exception
45
     */
46
    public function getPassword()
47
    {
48
        $password = rtrim(shell_exec("security find-generic-password -s '".self::KEYCHAIN_SAVE."' -w"));
49
        if (empty($password)) {
50
            $password = parent::getPassword();
51
            if (empty($password)) {
52
                throw new \Exception(sprintf('Could not retrieve password.'));
53
            }
54
            shell_exec('security add-generic-password -a "'.$this->configuration->getJiraUser().'" -s "'.self::KEYCHAIN_SAVE.'" -w "'.$password.'"');
55
        }
56
        return $password;
57
    }
58
}
59