Issues (186)

includes/Helpers/OAuthProtocolHelper.php (2 issues)

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Helpers;
11
12
use MediaWiki\OAuthClient\Client;
13
use MediaWiki\OAuthClient\ClientConfig;
14
use MediaWiki\OAuthClient\Consumer;
15
use MediaWiki\OAuthClient\Token;
16
use Waca\DataObjects\Domain;
17
use Waca\Exceptions\CurlException;
18
use Waca\PdoDatabase;
19
20
class OAuthProtocolHelper implements Interfaces\IOAuthProtocolHelper
21
{
22
    private $authUrl;
23
    /**
24
     * @var PdoDatabase
25
     */
26
    private $database;
27
    /**
28
     * @var string
29
     */
30
    private $consumerKey;
31
    /**
32
     * @var string
33
     */
34
    private $consumerSecret;
35
    /**
36
     * @var string
37
     */
38
    private $userAgent;
39
40
    /**
41
     * OAuthHelper constructor.
42
     *
43
     * @param string     $consumerKey
44
     * @param string     $consumerSecret
45
     * @param PdoDatabase $database
46
     * @param string      $userAgent
47
     */
48
    public function __construct(
49
        $consumerKey,
50
        $consumerSecret,
51
        PdoDatabase $database,
52
        $userAgent
53
    ) {
54
        $this->consumerKey = $consumerKey;
55
        $this->consumerSecret = $consumerSecret;
56
        $this->userAgent = $userAgent;
57
        $this->database = $database;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getRequestToken()
64
    {
65
        /** @var Token $requestToken */
66
67
        // FIXME: domains!
68
        /** @var Domain $domain */
69
        $domain = Domain::getById(1, $this->database);
70
71
        list($authUrl, $requestToken) = $this->getClient($domain)->initiate();
72
        $this->authUrl = $authUrl;
73
        return $requestToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $requestToken returns the type MediaWiki\OAuthClient\Token which is incompatible with the return type mandated by Waca\Helpers\Interfaces\...lper::getRequestToken() of stdClass.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function getAuthoriseUrl($requestToken)
80
    {
81
        return $this->authUrl;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier)
88
    {
89
        $requestToken = new Token($oauthRequestToken, $oauthRequestSecret);
90
91
        // FIXME: domains!
92
        /** @var Domain $domain */
93
        $domain = Domain::getById(1, $this->database);
94
95
        return $this->getClient($domain)->complete($requestToken, $oauthVerifier);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getClient(...tToken, $oauthVerifier) returns the type MediaWiki\OAuthClient\Token which is incompatible with the return type mandated by Waca\Helpers\Interfaces\...er::callbackCompleted() of stdClass.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret)
102
    {
103
        // FIXME: domains!
104
        /** @var Domain $domain */
105
        $domain = Domain::getById(1, $this->database);
106
107
        return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret));
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET')
114
    {
115
        $userToken = new Token($accessToken, $accessSecret);
116
117
        $apiParams['format'] = 'json';
118
119
        if ($apiParams === null || !is_array($apiParams)) {
120
            throw new CurlException("Invalid API call");
121
        }
122
123
        // FIXME: domains!
124
        /** @var Domain $domain */
125
        $domain = Domain::getById(1, $this->database);
126
127
        $url = $domain->getWikiApiPath();
128
        $isPost = ($method === 'POST');
129
130
        if ($method === 'GET') {
131
            $query = http_build_query($apiParams);
132
            $url .= '?' . $query;
133
            $apiParams = null;
134
        }
135
136
        $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams);
137
138
        return json_decode($data);
139
    }
140
141
    /**
142
     * @param string $oauthEndpoint
143
     *
144
     * @return Client
145
     */
146
    protected function getClient(Domain $domain) : Client
147
    {
148
        $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth");
149
        $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret));
150
        $oauthClientConfig->setUserAgent($this->userAgent);
151
        return new Client($oauthClientConfig);
152
    }
153
}
154