1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
13
|
|
|
|
14
|
|
|
use CL\Slack\Payload\OauthAccessPayload; |
15
|
|
|
use CL\Slack\Payload\OauthAccessPayloadResponse; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Cas Leentfaar <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class OauthAccessCommand extends AbstractApiCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
parent::configure(); |
30
|
|
|
|
31
|
|
|
$this->setName('oauth:access'); |
32
|
|
|
$this->setDescription('Exchange a temporary OAuth code for an API access token'); |
33
|
|
|
$this->addArgument('client-id', InputArgument::REQUIRED, 'Issued when you created your application'); |
34
|
|
|
$this->addArgument('client-secret', InputArgument::REQUIRED, 'Issued when you created your application'); |
35
|
|
|
$this->addArgument('code', InputArgument::REQUIRED, 'The code param returned via the OAuth callback'); |
36
|
|
|
$this->addOption('redirect-uri', null, InputOption::VALUE_REQUIRED, 'This must match the originally submitted URI (if one was sent)'); |
37
|
|
|
$this->setHelp(<<<EOT |
38
|
|
|
The <info>oauth:access</info> command allows you to exchange a temporary OAuth code for an API access token. |
39
|
|
|
This is used as part of the OAuth authentication flow. |
40
|
|
|
|
41
|
|
|
For more information about the related API method, check out the official documentation: |
42
|
|
|
<comment>https://api.slack.com/methods/oauth.access</comment> |
43
|
|
|
EOT |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return OauthAccessPayload |
49
|
|
|
*/ |
50
|
|
|
protected function createPayload() |
51
|
|
|
{ |
52
|
|
|
$payload = new OauthAccessPayload(); |
53
|
|
|
$payload->setClientId($this->input->getArgument('client-id')); |
54
|
|
|
$payload->setClientSecret($this->input->getArgument('client-secret')); |
55
|
|
|
$payload->setCode($this->input->getArgument('code')); |
56
|
|
|
$payload->setRedirectUri($this->input->getOption('redirect-uri')); |
57
|
|
|
|
58
|
|
|
return $payload; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
* |
64
|
|
|
* @param OauthAccessPayloadResponse $payloadResponse |
65
|
|
|
*/ |
66
|
|
|
protected function handleResponse($payloadResponse) |
67
|
|
|
{ |
68
|
|
|
if ($payloadResponse->isOk()) { |
69
|
|
|
$this->writeOk('Successfully authenticated through oauth!'); |
70
|
|
|
$this->output->writeln('Access token: <comment>%s</comment>', $payloadResponse->getAccessToken()); |
71
|
|
|
$this->output->writeln('Scope: <comment>%s</comment>', $payloadResponse->getScope()); |
72
|
|
|
} else { |
73
|
|
|
$this->writeError(sprintf('Failed to be authenticated through oauth. %s', lcfirst($payloadResponse->getErrorExplanation()))); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|