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

UnixPasswordRetrievalStrategy::__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\GenericPasswordRetrievalStrategy
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 UnixPasswordRetrievalStrategy implements PasswordRetrievalStrategyInterface
34
{
35
36
    /**
37
     * @var OutputInterface
38
     */
39
    protected $output;
40
41
    /**
42
     * @var ConfigurationInterface
43
     */
44
    protected $configuration;
45
46
    /**
47
     * MacOsPasswordRetrievalStrategy constructor.
48
     *
49
     * @param OutputInterface $output
50
     * @param ConfigurationInterface $configuration
51
     */
52
    public function __construct(OutputInterface $output, ConfigurationInterface $configuration)
53
    {
54
        $this->output = $output;
55
        $this->configuration = $configuration;
56
    }
57
58
    /**
59
     * Prompt silent
60
     *
61
     * Interactively prompts for input without echoing to the terminal.
62
     * Requires a bash shell or Windows and won't work with
63
     * safe_mode settings (Uses `shell_exec`)
64
     *
65
     * Source: http://www.sitepoint.com/interactive-cli-password-prompt-in-php/
66
     *
67
     * @param OutputInterface $output Console output interface
68
     * @param string $prompt The message to the user
69
     *
70
     * @return string
71
     */
72
    protected function promptSilent(OutputInterface $output, $prompt = "Enter Password:")
73
    {
74
        $command = "/usr/bin/env bash -c 'echo OK'";
75
        if (rtrim(shell_exec($command)) !== 'OK') {
76
            trigger_error("Can't invoke bash");
77
            return '';
78
        }
79
        $command = "/usr/bin/env bash -c 'read -s -p \""
80
            . addslashes($prompt)
81
            . "\" mypassword && echo \$mypassword'";
82
        $password = rtrim(shell_exec($command));
83
        $output->writeln('');
84
        return $password;
85
    }
86
87
    /**
88
     * Will retrieve a stored password from the OSX keychain
89
     *
90
     * @return string
91
     */
92
    public function getPassword()
93
    {
94
        $password = $this->promptSilent($this->output, 'Please enter password for your push service account "' . $this->configuration->getJiraUser() . '":');
95
        if (empty($password)) {
96
            $this->output->writeln('<error>Empty password is not possible...</error>');
97
98
            return '';
99
        }
100
        return $password;
101
    }
102
}
103