Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
42 / 45 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ResetClientSecret | |
93.33% |
42 / 45 |
|
75.00% |
3 / 4 |
7.01 | |
0.00% |
0 / 1 |
| getFixedParams | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getUnifiedParams | |
86.36% |
19 / 22 |
|
0.00% |
0 / 1 |
4.04 | |||
| getParamSettings | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getBodyParamSettings | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\OAuth\Rest\Handler; |
| 4 | |
| 5 | use MediaWiki\Context\RequestContext; |
| 6 | use MediaWiki\Extension\OAuth\Control\ConsumerAccessControl; |
| 7 | use MediaWiki\Extension\OAuth\Entity\UserEntity; |
| 8 | use MediaWiki\Extension\OAuth\OAuthServices; |
| 9 | use MediaWiki\MediaWikiServices; |
| 10 | use MediaWiki\Rest\LocalizedHttpException; |
| 11 | use MediaWiki\Utils\MWRestrictions; |
| 12 | use Wikimedia\Message\MessageValue; |
| 13 | use Wikimedia\ParamValidator\ParamValidator; |
| 14 | |
| 15 | class ResetClientSecret extends AbstractClientHandler { |
| 16 | |
| 17 | /** |
| 18 | * @inheritDoc |
| 19 | */ |
| 20 | protected function getFixedParams(): array { |
| 21 | return [ |
| 22 | 'action' => 'update', |
| 23 | 'rsaKey' => '', |
| 24 | 'resetSecret' => true, |
| 25 | 'restrictions' => MWRestrictions::newDefault(), |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @inheritDoc |
| 31 | */ |
| 32 | protected function getUnifiedParams(): array { |
| 33 | $params = parent::getUnifiedParams(); |
| 34 | $params['client_key'] = $this->getRequest()->getPathParam( 'client_key' ); |
| 35 | |
| 36 | $requestContext = RequestContext::getMain(); |
| 37 | $consumerRepository = OAuthServices::wrap( MediaWikiServices::getInstance() )->getConsumerRepository(); |
| 38 | $clientAccess = ConsumerAccessControl::wrap( |
| 39 | $consumerRepository->getByKey( $params['consumerKey'] ), $requestContext |
| 40 | ); |
| 41 | if ( !$clientAccess ) { |
| 42 | throw new LocalizedHttpException( |
| 43 | MessageValue::new( 'mwoauth-invalid-consumer-key' ), 400 |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | $dataAccessObj = $clientAccess->getDAO(); |
| 48 | |
| 49 | if ( $dataAccessObj->getDeleted() ) { |
| 50 | throw new LocalizedHttpException( |
| 51 | MessageValue::new( 'mwoauth-consumer-deleted-error' ), 401 |
| 52 | ); |
| 53 | } elseif ( !$dataAccessObj->isOwnedBy( UserEntity::newFromMWUser( $requestContext->getUser() ) ) ) { |
| 54 | throw new LocalizedHttpException( |
| 55 | MessageValue::new( 'mwoauth-consumer-user-mismatch' ), 400 |
| 56 | ); |
| 57 | } |
| 58 | $params['changeToken'] = $dataAccessObj->getChangeToken( $requestContext ); |
| 59 | |
| 60 | return $params; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @inheritDoc |
| 65 | */ |
| 66 | public function getParamSettings(): array { |
| 67 | return [ |
| 68 | 'client_key' => [ |
| 69 | self::PARAM_SOURCE => 'path', |
| 70 | ParamValidator::PARAM_TYPE => 'string', |
| 71 | ParamValidator::PARAM_REQUIRED => true, |
| 72 | self::PARAM_DESCRIPTION => new MessageValue( 'mwoauth-rest-param-desc-client_key' ), |
| 73 | ], |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @inheritDoc |
| 79 | */ |
| 80 | public function getBodyParamSettings(): array { |
| 81 | return [ |
| 82 | 'reason' => [ |
| 83 | self::PARAM_SOURCE => 'body', |
| 84 | ParamValidator::PARAM_TYPE => 'string', |
| 85 | ParamValidator::PARAM_REQUIRED => false, |
| 86 | ParamValidator::PARAM_DEFAULT => '', |
| 87 | self::PARAM_DESCRIPTION => new MessageValue( 'mwoauth-rest-param-desc-reason' ), |
| 88 | ], |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | } |