Completed
Push — master ( dbfa6d...65a5eb )
by Bernhard
16s queued 12s
created

MacOsPasswordRetrievalStrategy::promptSilent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 14
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * \Wicked\Timely\PushServices\PushServiceInterface
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
use JiraRestApi\Configuration\ConfigurationInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * Date helper
27
 *
28
 * @author    wick-ed
29
 * @copyright 2020 Bernhard Wick
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/wick-ed/timely
32
 */
33
class MacOsPasswordRetrievalStrategy implements PasswordRetrievalStrategyInterface
34
{
35
    /**
36
     * Constants used within this class
37
     *
38
     * @var string
39
     */
40
    const KEYCHAIN_NAME = 'osxkeychain';
41
    const KEYCHAIN_SAVE = 'timely jira access';
42
43
    /**
44
     * @var OutputInterface
45
     */
46
    protected $output;
47
48
    /**
49
     * @var ConfigurationInterface
50
     */
51
    protected $configuration;
52
53
    /**
54
     * MacOsPasswordRetrievalStrategy constructor.
55
     *
56
     * @param OutputInterface $output
57
     * @param ConfigurationInterface $configuration
58
     */
59
    public function __construct(OutputInterface $output, ConfigurationInterface $configuration)
60
    {
61
        $this->output = $output;
62
        $this->configuration = $configuration;
63
    }
64
65
    /**
66
     * Prompt silent
67
     *
68
     * Interactively prompts for input without echoing to the terminal.
69
     * Requires a bash shell or Windows and won't work with
70
     * safe_mode settings (Uses `shell_exec`)
71
     *
72
     * Source: http://www.sitepoint.com/interactive-cli-password-prompt-in-php/
73
     *
74
     * @param OutputInterface $output Console output interface
75
     * @param string          $prompt The message to the user
76
     *
77
     * @return string
78
     */
79
    protected function promptSilent(OutputInterface $output, $prompt = "Enter Password:")
80
    {
81
        $command = "/usr/bin/env bash -c 'echo OK'";
82
        if (rtrim(shell_exec($command)) !== 'OK') {
83
            trigger_error("Can't invoke bash");
84
            return '';
85
        }
86
        $command = "/usr/bin/env bash -c 'read -s -p \""
87
            . addslashes($prompt)
88
            . "\" mypassword && echo \$mypassword'";
89
        $password = rtrim(shell_exec($command));
90
        $output->writeln('');
91
        return $password;
92
    }
93
94
    /**
95
     * Will retrieve a stored password from the OSX keychain
96
     *
97
     * @return string
98
     */
99
    public function getPassword()
100
    {
101
        $password = rtrim(shell_exec("security find-generic-password -s '".self::KEYCHAIN_SAVE."' -w"));
102
        if (empty($password)) {
103
            $password = $this->promptSilent($this->output, 'Please enter password for your jira account "'.$this->configuration->getJiraUser().'":');
104
            if (empty($password)) {
105
                $this->output->writeln('Empty password is not possible. Stop push ...');
106
                return '';
107
            }
108
            shell_exec('security add-generic-password -a "'.$this->configuration->getJiraUser().'" -s "'.self::KEYCHAIN_SAVE.'" -w "'.$password.'"');
109
        }
110
        return $password;
111
    }
112
}
113