Completed
Push — master ( 50beb4...8d6a23 )
by Michał
02:12
created

Slack::getDefaultRequestOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php namespace nyx\auth\id\protocols\oauth2\providers;
2
3
// Internal dependencies
4
use nyx\auth\id\protocols\oauth2;
5
use nyx\auth;
6
7
/**
8
 * Slack Identity Provider (OAuth 2.0)
9
 *
10
 * @package     Nyx\Auth
11
 * @version     0.1.0
12
 * @author      Michal Chojnacki <[email protected]>
13
 * @copyright   2012-2017 Nyx Dev Team
14
 * @link        https://github.com/unyx/nyx
15
 */
16
class Slack extends oauth2\Provider
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    const SCOPE_SEPARATOR = ' ';
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    const URL_AUTHORIZE = 'https://slack.com/oauth/authorize';
27
    const URL_EXCHANGE  = 'https://slack.com/api/oauth.access';
28
    const URL_IDENTIFY  = 'https://slack.com/api/users.identity';
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    const IDENTITY = auth\id\identities\Slack::class;
34
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * Note: The "identity" scopes cannot be requested alongside other scopes, as Slack otherwise
39
     * returns an 'invalid_scope' error.
40
     */
41
    protected $defaultScopes = ['identity.basic', 'identity.email', 'identity.avatar'];
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    protected function getDefaultRequestOptions(auth\interfaces\Token $token = null) : array
47
    {
48
        if (null === $token) {
49
            return parent::getDefaultRequestOptions($token);
50
        }
51
52
        // Slack expects the token to be present in each authorized request as the 'token' parameter.
53
        // Note: Passing it in the body yielded 'not_authed' (ie. no token) errors from Slack.
54
        return array_merge_recursive(parent::getDefaultRequestOptions(), [
55
            'query' => [
56
                'token' => $token->getId()
57
            ]
58
        ]);
59
    }
60
}
61