Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.47% |
26 / 34 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
IpHandler | |
76.47% |
26 / 34 |
|
50.00% |
2 / 4 |
10.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
factory | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
run | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
6.06 | |||
getParamSettings | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\SecurityApi\Rest\Feed; |
4 | |
5 | use Config; |
6 | use MediaWiki\Http\HttpRequestFactory; |
7 | use MediaWiki\Permissions\PermissionManager; |
8 | use MediaWiki\Rest\LocalizedHttpException; |
9 | use MediaWiki\Rest\Response; |
10 | use MediaWiki\Rest\SimpleHandler; |
11 | use MediaWiki\User\UserIdentity; |
12 | use RequestContext; |
13 | use Wikimedia\IPUtils; |
14 | use Wikimedia\Message\MessageValue; |
15 | use Wikimedia\ParamValidator\ParamValidator; |
16 | |
17 | class IpHandler extends SimpleHandler { |
18 | /** @var Config */ |
19 | private $config; |
20 | |
21 | /** @var HttpRequestFactory */ |
22 | private $httpRequestFactory; |
23 | |
24 | /** @var PermissionManager */ |
25 | private $permissionManager; |
26 | |
27 | /** @var UserIdentity */ |
28 | private $user; |
29 | |
30 | /** |
31 | * @param Config $config |
32 | * @param HttpRequestFactory $httpRequestFactory |
33 | * @param PermissionManager $permissionManager |
34 | * @param UserIdentity $user |
35 | */ |
36 | public function __construct( |
37 | Config $config, |
38 | HttpRequestFactory $httpRequestFactory, |
39 | PermissionManager $permissionManager, |
40 | UserIdentity $user |
41 | ) { |
42 | $this->config = $config; |
43 | $this->httpRequestFactory = $httpRequestFactory; |
44 | $this->permissionManager = $permissionManager; |
45 | $this->user = $user; |
46 | } |
47 | |
48 | /** |
49 | * @param Config $config |
50 | * @param HttpRequestFactory $httpRequestFactory |
51 | * @param PermissionManager $permissionManager |
52 | * @return self |
53 | */ |
54 | public static function factory( |
55 | Config $config, |
56 | HttpRequestFactory $httpRequestFactory, |
57 | PermissionManager $permissionManager |
58 | ) { |
59 | return new self( |
60 | $config, |
61 | $httpRequestFactory, |
62 | $permissionManager, |
63 | RequestContext::getMain()->getUser() |
64 | ); |
65 | } |
66 | |
67 | /** |
68 | * Get all info on an ip provided by ipoid/feed |
69 | * |
70 | * @param string $ip |
71 | * @return Response |
72 | */ |
73 | public function run( string $ip ): Response { |
74 | if ( !$this->permissionManager->userHasRight( $this->user, 'securityapi-ipoid-feed' ) ) { |
75 | throw new LocalizedHttpException( |
76 | new MessageValue( 'securityapi-rest-access-denied' ), $this->user->isRegistered() ? 403 : 401 ); |
77 | } |
78 | |
79 | if ( !IPUtils::isValid( $ip ) ) { |
80 | throw new LocalizedHttpException( |
81 | new MessageValue( 'securityapi-invalid-ip' ), 400 ); |
82 | } |
83 | |
84 | $baseUrl = $this->config->get( 'SecurityApiIpoidUrl' ); |
85 | if ( !$baseUrl ) { |
86 | throw new LocalizedHttpException( |
87 | new MessageValue( 'securityapi-invalid-url' ), 400 ); |
88 | } |
89 | |
90 | // Get response from ipoid |
91 | $url = $baseUrl . '/feed/v1/ip/' . $ip; |
92 | $req = $this->httpRequestFactory->create( $url, [ 'method' => 'GET' ] ); |
93 | $response = $req->execute(); |
94 | |
95 | if ( !$response->isOK() ) { |
96 | throw new LocalizedHttpException( |
97 | new MessageValue( 'securityapi-rest-error' ), $response->getErrors()[0]['params'][0] ); |
98 | } |
99 | |
100 | return $this->getResponseFactory()->createJson( json_decode( $req->getContent() ) ); |
101 | } |
102 | |
103 | /** |
104 | * @inheritDoc |
105 | */ |
106 | public function getParamSettings() { |
107 | return [ |
108 | 'ip' => [ |
109 | self::PARAM_SOURCE => 'path', |
110 | ParamValidator::PARAM_TYPE => 'string', |
111 | ParamValidator::PARAM_REQUIRED => true, |
112 | ], |
113 | ]; |
114 | } |
115 | } |