Completed
Pull Request — master (#571)
by
unknown
01:29
created

Slack::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 5
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\ClientInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
13
/**
14
 * Slack service.
15
 *
16
 * @author Christophe Garde <[email protected]>
17
 * @link https://api.slack.com/#read_the_docs
18
 */
19
class Slack extends AbstractService
20
{
21
    // Basic
22
    const SCOPE_ID_EMAIL                       = 'identity.email';
23
    const SCOPE_ID_BASIC                       = 'identity.basic';
24
   
25
    public function __construct(
26
        CredentialsInterface $credentials,
27
        ClientInterface $httpClient,
28
        TokenStorageInterface $storage,
29
        $scopes = array(),
30
        UriInterface $baseApiUri = null
31
    ) {
32
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, $stateParameterInAutUrl = true);
33
      
34
35
        if (null === $baseApiUri) {
36
            $this->baseApiUri = new Uri('https://slack.com/api/');
37
        }
38
    }
39
    
40
    
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getAuthorizationUri(array $additionalParameters = array()){
45
        // replace scope by user_scope
46
        // this is a bit ugly, but still looks better than overriding the whole function :)
47
        return str_replace('&scope=','&scope=&user_scope=',parent::getAuthorizationUri($additionalParameters));
48
    }
49
    
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getAuthorizationEndpoint()
54
    {
55
        return new Uri('https://slack.com/oauth/v2/authorize');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getAccessTokenEndpoint()
62
    {
63
        return new Uri('https://slack.com/api/oauth.v2.access');
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getAuthorizationMethod()
70
    {
71
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function parseAccessTokenResponse($responseBody)
78
    {
79
        $data = json_decode($responseBody, true);
80
        if (null === $data || !is_array($data)) {
81
            throw new TokenResponseException('Unable to parse response.');
82
        } elseif (isset($data['error'])) {
83
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
84
        }
85
      
86
        $token = new StdOAuth2Token();
87
        $token->setAccessToken($data['authed_user']['access_token']);
88
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
89
90
        unset($data['authed_user']['access_token']);
91
92
        $token->setExtraParams(array($data['authed_user']));
93
        return $token;
94
    }
95
96
}
97