Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConsumerAccessControl
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 27
812
0.00% covered (danger)
0.00%
0 / 1
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConsumerKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOAuthVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCallbackUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCallbackIsPrefix
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEmail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEmailAuthenticated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDeveloperAgreement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOwnerOnly
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWiki
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGrants
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRegistration
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSecretKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRsaKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRestrictions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStageTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDeleted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserName
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getWikiName
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getNameAndVersion
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isConfidential
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDAO
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OAuth\Control;
4
5use MediaWiki\Extension\OAuth\Backend\Consumer;
6use MediaWiki\Extension\OAuth\Backend\Utils;
7use MediaWiki\Extension\OAuth\Entity\ClientEntity;
8use MediaWiki\Message\Message;
9use MediaWiki\User\CentralId\CentralIdLookup;
10use MediaWiki\User\User;
11use MediaWiki\Utils\MWRestrictions;
12
13class ConsumerAccessControl extends DAOAccessControl {
14    // accessor fields copied from MWOAuthConsumer, except they can return a Message on access error
15
16    /**
17     * Internal ID (DB primary key).
18     * Returns a Message when the user does not have permission to see this field.
19     * @return int|Message
20     */
21    public function getId() {
22        return $this->get( 'id' );
23    }
24
25    /**
26     * Consumer key (32-character hexadecimal string that's used in the OAuth protocol
27     * and in URLs). This is used as the consumer ID for most external purposes.
28     * Returns a Message when the user does not have permission to see this field.
29     * @return string|Message
30     */
31    public function getConsumerKey() {
32        return $this->get( 'consumerKey' );
33    }
34
35    /**
36     * Name of the consumer.
37     * Returns a Message when the user does not have permission to see this field.
38     * @return string|Message
39     */
40    public function getName() {
41        return $this->get( 'name' );
42    }
43
44    /**
45     * @return int
46     */
47    public function getOAuthVersion() {
48        return (int)$this->get( 'oauthVersion' );
49    }
50
51    /**
52     * Central ID of the owner.
53     * Returns a Message when the user does not have permission to see this field.
54     * @return int|Message
55     */
56    public function getUserId() {
57        return $this->get( 'userId' );
58    }
59
60    /**
61     * Consumer version. This is mostly meant for humans: different versions of the same
62     * application have different keys and are handled as different consumers internally.
63     * Returns a Message when the user does not have permission to see this field.
64     * @return string|Message
65     */
66    public function getVersion() {
67        return $this->get( 'version' );
68    }
69
70    /**
71     * Callback URL (or prefix). The browser will be redirected to this URL at the end of
72     * an OAuth handshake. See getCallbackIsPrefix() for the interpretation of this field.
73     * Returns a Message when the user does not have permission to see this field.
74     * @return string|Message
75     */
76    public function getCallbackUrl() {
77        return $this->get( 'callbackUrl' );
78    }
79
80    /**
81     * When true, getCallbackUrl() returns a prefix; the callback URL can be provided by the caller
82     * as long as the prefix matches. When false, the callback URL will be determined by
83     * getCallbackUrl().
84     * Returns a Message when the user does not have permission to see this field.
85     * @return bool|Message
86     */
87    public function getCallbackIsPrefix() {
88        return $this->get( 'callbackIsPrefix' );
89    }
90
91    /**
92     * Description of the consumer. Currently interpreted as plain text; might change to wikitext
93     * in the future.
94     * Returns a Message when the user does not have permission to see this field.
95     * @return string|Message
96     */
97    public function getDescription() {
98        return $this->get( 'description' );
99    }
100
101    /**
102     * Email address of the owner.
103     * Returns a Message when the user does not have permission to see this field.
104     * @return string|Message
105     */
106    public function getEmail() {
107        return $this->get( 'email' );
108    }
109
110    /**
111     * Date of verifying the email, in TS_MW format. In practice this will be the same as
112     * getRegistration().
113     * Returns null if the timestamp is not set.
114     * Returns a Message when the user does not have permission to see this field.
115     * @return string|null|Message
116     */
117    public function getEmailAuthenticated() {
118        return $this->get( 'emailAuthenticated' );
119    }
120
121    /**
122     * Did the user accept the developer agreement (the terms of use checkbox at the bottom of the
123     * registration form)? Except for very old users, always true.
124     * Returns a Message when the user does not have permission to see this field.
125     * @return bool|Message
126     */
127    public function getDeveloperAgreement() {
128        return $this->get( 'developerAgreement' );
129    }
130
131    /**
132     * Owner-only consumers will use one-legged flow instead of three-legged (see
133     * https://github.com/Mashape/mashape-oauth/blob/master/FLOWS.md#oauth-10a-one-legged ); there
134     * is only one user (who is the same as the owner) and they learn the access token at
135     * consumer registration time.
136     * Returns a Message when the user does not have permission to see this field.
137     * @return bool|Message
138     */
139    public function getOwnerOnly() {
140        return $this->get( 'ownerOnly' );
141    }
142
143    /**
144     * The wiki on which the consumer is allowed to access user accounts. A wiki ID or '*' for all.
145     * Returns a Message when the user does not have permission to see this field.
146     * @return string|Message
147     */
148    public function getWiki() {
149        return $this->get( 'wiki' );
150    }
151
152    /**
153     * The list of grants required by this application.
154     * Returns a Message when the user does not have permission to see this field.
155     * @return string[]|Message
156     */
157    public function getGrants() {
158        return $this->get( 'grants' );
159    }
160
161    /**
162     * Consumer registration date in TS_MW format.
163     * Returns a Message when the user does not have permission to see this field.
164     * @return string|Message
165     */
166    public function getRegistration() {
167        return $this->get( 'registration' );
168    }
169
170    /**
171     * Secret key used to derive the consumer secret for HMAC-SHA1 signed OAuth requests.
172     * The actual consumer secret will be calculated via Utils::hmacDBSecret() to mitigate
173     * DB leaks.
174     * Returns a Message when the user does not have permission to see this field.
175     * @return string|Message
176     */
177    public function getSecretKey() {
178        return $this->get( 'secretKey' );
179    }
180
181    /**
182     * Public RSA key for RSA-SHA1 signed OAuth requests.
183     * Returns a Message when the user does not have permission to see this field.
184     * @return string|Message
185     */
186    public function getRsaKey() {
187        return $this->get( 'rsaKey' );
188    }
189
190    /**
191     * Application restrictions (such as allowed IPs).
192     * Returns a Message when the user does not have permission to see this field.
193     * @return MWRestrictions|Message
194     */
195    public function getRestrictions() {
196        return $this->get( 'restrictions' );
197    }
198
199    /**
200     * Stage at which the consumer is in the review workflow (proposed, approved etc).
201     * Returns a Message when the user does not have permission to see this field.
202     * @return int|Message One of the STAGE_* constants
203     */
204    public function getStage() {
205        return $this->get( 'stage' );
206    }
207
208    /**
209     * Date at which the consumer was moved to the current stage, in TS_MW format.
210     * Returns a Message when the user does not have permission to see this field.
211     * @return string|Message
212     */
213    public function getStageTimestamp() {
214        return $this->get( 'stageTimestamp' );
215    }
216
217    /**
218     * Is the consumer suppressed? (There is no plain deletion; the closest equivalent is the
219     * rejected/disabled stage.)
220     * Returns a Message when the user does not have permission to see this field.
221     * @return bool|Message
222     */
223    public function getDeleted() {
224        return $this->get( 'deleted' );
225    }
226
227    // accessors for common formatting
228
229    /**
230     * Owner username.
231     * Note that this method triggers a DB lookup.
232     * @param User|bool $audience show hidden names based on this user, or false for public
233     * @return string|Message
234     */
235    public function getUserName( $audience = false ) {
236        return $this->get( 'userId', static function ( $id ) use ( $audience ) {
237            return Utils::getCentralIdLookup()->nameFromCentralId(
238                $id,
239                $audience ?: CentralIdLookup::AUDIENCE_PUBLIC
240            );
241        } );
242    }
243
244    /**
245     * Pretty wiki name.
246     * @return string|Message
247     */
248    public function getWikiName() {
249        return $this->get( 'wiki', static function ( $wikiId ) {
250            return Utils::getWikiIdName( $wikiId );
251        } );
252    }
253
254    /**
255     * Consumer name and version in a "Foo [1.0]" format.
256     * @return string|Message
257     */
258    public function getNameAndVersion() {
259        return $this->get( 'name', function ( $s ) {
260            return $s . ' ' . $this->msg( 'brackets', $this->getVersion() )->plain();
261        } );
262    }
263
264    /**
265     * Whether the consumer is confidential or not.
266     * Only meaningful for OAuth 2.0 consumers (see {@link getOAuthVersion()})
267     * and must not be called otherwise.
268     * @return bool
269     */
270    public function isConfidential() {
271        return (bool)$this->get( 'oauth2IsConfidential' );
272    }
273
274    /**
275     * @return Consumer|ClientEntity
276     */
277    public function getDAO() {
278        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
279        return $this->dao;
280    }
281}