Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 107
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateOAuthConsumer
0.00% covered (danger)
0.00%
0 / 107
0.00% covered (danger)
0.00%
0 / 2
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 1
210
1<?php
2/**
3 * Example:
4 *
5 * createOAuthConsumer.php
6 *   --callbackIsPrefix
7 *   --callbackUrl="https://foourl"
8 *   --description="Application description"
9 *   --grants="editprotected"
10 *   --grants="createaccount"
11 *   --name="Application name"
12 *   --user="Admin"
13 *   --version="0.2"
14 *   --wiki=default
15 *   --approve
16 *
17 * You can optionally output successful results as json using --jsonOnSuccess
18 */
19
20namespace MediaWiki\Extension\OAuth;
21
22use MediaWiki\Context\RequestContext;
23use MediaWiki\Extension\OAuth\Backend\Consumer;
24use MediaWiki\Extension\OAuth\Backend\Utils;
25use MediaWiki\Extension\OAuth\Control\ConsumerSubmitControl;
26use MediaWiki\Maintenance\Maintenance;
27use MediaWiki\User\User;
28use MWRestrictions;
29
30/**
31 * @ingroup Maintenance
32 */
33
34// @codeCoverageIgnoreStart
35if ( getenv( 'MW_INSTALL_PATH' ) ) {
36    $IP = getenv( 'MW_INSTALL_PATH' );
37} else {
38    $IP = __DIR__ . '/../../..';
39}
40
41require_once "$IP/maintenance/Maintenance.php";
42// @codeCoverageIgnoreEnd
43
44class CreateOAuthConsumer extends Maintenance {
45    public function __construct() {
46        parent::__construct();
47        $this->addDescription( "Create an OAuth consumer" );
48        $this->addOption(
49            'oauthVersion',
50            'OAuth version (' . Consumer::OAUTH_VERSION_1 . ' or ' . Consumer::OAUTH_VERSION_2 .
51                ', default ' . Consumer::OAUTH_VERSION_1 . ')',
52            false,
53            true
54        );
55        $this->addOption( 'user', 'User to run the script as', true, true );
56        $this->addOption( 'name', 'Application name', true, true );
57        $this->addOption( 'description', 'Application description', true, true );
58        $this->addOption( 'version', 'Application version', true, true );
59        $this->addOption( 'callbackUrl', 'Callback URL', true, true );
60        $this->addOption(
61            'callbackIsPrefix',
62            'Allow a consumer to specify a callback in requests (OAuth 1 only)'
63        );
64        $this->addOption( 'grants', 'Grants', true, true, false, true );
65        $this->addOption( 'jsonOnSuccess', 'Output successful results as JSON' );
66        $this->addOption( 'approve', 'Accept the consumer' );
67        $this->addOption(
68            'ownerOnly',
69            'Make the consumer only usable by the given user; see ' .
70                'https://www.mediawiki.org/wiki/OAuth/Owner-only_consumers.'
71        );
72        $this->addOption(
73            'oauth2IsNotConfidential',
74            'Mark the client as *not* confidential (OAuth 2 only). By default, clients are confidential.'
75        );
76        $this->addOption(
77            'oauth2GrantTypes',
78            'The OAuth 2 grant types: authorization_code, refresh_token, and/or client_credentials.',
79            false,
80            true,
81            false,
82            true
83        );
84        $this->requireExtension( "OAuth" );
85    }
86
87    public function execute() {
88        $user = User::newFromName( $this->getOption( 'user' ) );
89        if ( !$user->isNamed() ) {
90            $this->fatalError( 'User must be registered' );
91        }
92        if ( $user->getEmail() === '' ) {
93            $this->fatalError( 'User must have an email' );
94        }
95        $oauthVersion = (int)$this->getOption( 'oauthVersion', Consumer::OAUTH_VERSION_1 );
96        if ( !in_array( $oauthVersion, [ Consumer::OAUTH_VERSION_1, Consumer::OAUTH_VERSION_2 ], true ) ) {
97            $this->fatalError(
98                'Invalid oauthVersion, must be ' . Consumer::OAUTH_VERSION_1 .
99                    ' or ' . Consumer::OAUTH_VERSION_2 . '!'
100            );
101        }
102        if ( $oauthVersion === Consumer::OAUTH_VERSION_2 ) {
103            if ( $this->hasOption( 'callbackIsPrefix' ) ) {
104                $this->fatalError( 'callbackIsPrefix is only available in oauthVersion 1' );
105            }
106        } else {
107            if ( $this->hasOption( 'oauth2IsNotConfidential' ) ) {
108                $this->fatalError( 'oauth2IsNotConfidential is only available in oauthVersion 2' );
109            }
110            if ( $this->hasOption( 'oauth2GrantTypes' ) ) {
111                $this->fatalError( 'oauth2GrantTypes is only available in oauthVersion 2' );
112            }
113        }
114
115        $data = [
116            'action' => 'propose',
117            'name'         => $this->getOption( 'name' ),
118            'version'      => $this->getOption( 'version' ),
119            'description'  => $this->getOption( 'description' ),
120            'callbackUrl'  => $this->getOption( 'callbackUrl' ),
121            'oauthVersion' => $oauthVersion,
122            'callbackIsPrefix' => $this->hasOption( 'callbackIsPrefix' ),
123            'grants' => '["' . implode( '","', $this->getOption( 'grants' ) ) . '"]',
124            'granttype' => 'normal',
125            'ownerOnly' => $this->hasOption( 'ownerOnly' ),
126            'oauth2IsConfidential' => !$this->hasOption( 'oauth2IsNotConfidential' ),
127            'oauth2GrantTypes' => $this->getOption( 'oauth2GrantTypes', [ 'authorization_code', 'refresh_token' ] ),
128            'email' => $user->getEmail(),
129            // All wikis
130            'wiki' => '*',
131            // Generate a key
132            'rsaKey' => '',
133            'agreement' => true,
134            'restrictions' => MWRestrictions::newDefault(),
135        ];
136
137        $context = RequestContext::getMain();
138        $context->setUser( $user );
139
140        $dbw = Utils::getOAuthDB( DB_PRIMARY );
141        $control = new ConsumerSubmitControl( $context, $data, $dbw );
142        $status = $control->submit();
143
144        if ( !$status->isGood() ) {
145            $this->fatalError( $status->getMessage()->text() );
146        }
147
148        /** @var Consumer $cmr */
149        $cmr = $status->value['result']['consumer'];
150
151        if ( $this->hasOption( 'approve' ) ) {
152            $data = [
153                'action' => 'approve',
154                'consumerKey'  => $cmr->getConsumerKey(),
155                'reason'       => 'Approved by maintenance script',
156                'changeToken'  => $cmr->getChangeToken( $context ),
157            ];
158            $control = new ConsumerSubmitControl( $context, $data, $dbw );
159            $approveStatus = $control->submit();
160        }
161
162        $outputData = [
163            'created' => true,
164            'id' => $cmr->getId(),
165            'name' => $cmr->getName(),
166            'key' => $cmr->getConsumerKey(),
167            'secret' => Utils::hmacDBSecret( $cmr->getSecretKey() ),
168        ];
169
170        if ( isset( $approveStatus ) ) {
171            $outputData['approved'] = $approveStatus->isGood() ?
172                1 : $approveStatus->getWikiText( false, false, 'en' );
173        }
174
175        if ( $this->hasOption( 'jsonOnSuccess' ) ) {
176            $this->output( json_encode( $outputData ) );
177        } else {
178            foreach ( $outputData as $key => $value ) {
179                $this->output( $key . ': ' . $value . PHP_EOL );
180            }
181        }
182    }
183}
184
185// @codeCoverageIgnoreStart
186$maintClass = CreateOAuthConsumer::class;
187require_once RUN_MAINTENANCE_IF_MAIN;
188// @codeCoverageIgnoreEnd