aboutsummaryrefslogtreecommitdiff
path: root/MinetestPasswordPrimaryAuthenticationProvider.php
blob: 7d2fb19f2c1494e3b7e02e3b099d8962ec0f6e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
 * This program is a free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Auth
 */

namespace MediaWiki\Auth;

use User;

/**
 * A primary authentication provider that authenticates the user against a remote Minetest site.
 *
 * @ingroup Auth
 * @since 1.27
 */
class MinetestPasswordPrimaryAuthenticationProvider extends AbstractPrimaryAuthenticationProvider {

	/** @var string The URL of the Minetest site we authenticate against. */
	protected $minetestUrl;

	/** @var array */
    /*	protected $tokens = [];*/
    
    /**
     * @param array $params Settings
     *  - minetestUrl: The URL of the Minetest site we authenticate against.
     */
    public function __construct( $params = [] ) {

		if ( empty( $params['minetestUrl'] ) ) {
			throw new \InvalidArgumentException( 'The minetestUrl parameter missing in the auth configuration' );
		}

		$this->minetestUrl = $params['minetestUrl'];
    }

	public function beginPrimaryAuthentication( array $reqs ) {
		$req = AuthenticationRequest::getRequestByClass( $reqs, PasswordAuthenticationRequest::class );
		if ( !$req ) {
			return AuthenticationResponse::newAbstain();
		}
        
		if ( $req->username === null || $req->password === null ) {
			return AuthenticationResponse::newAbstain();
		}

		$username = User::getCanonicalName( $req->username, 'usable' );
		if ( $username === false ) {
			return AuthenticationResponse::newAbstain();
		}
        
		$token = $this->getMinetestUserToken( $req->username,  $req->password );
        
		if ( $token === false ) {
			return AuthenticationResponse::newAbstain();
            
		} else {
			return AuthenticationResponse::newPass( $username );
		}
	}
    
	/**
	 * Prepares a curl handler to use for querying the Minetest web services.
	 *
	 * @param string $url
	 * @return resource
	 */
	protected function getMinetestCurlClient( $url ) {
        
		$curl = curl_init( $url );

		curl_setopt_array( $curl, [
			CURLOPT_USERAGENT => 'MWAuthMinetestBot/1.0',
			CURLOPT_NOBODY => false,
			CURLOPT_HEADER => false,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_SSL_VERIFYPEER => 1,
			CURLOPT_SSL_VERIFYHOST => 2,
		]);

		return $curl;
	}

	/**
	 * Attempts to authenticate the user against Minetest. Checks if user is authenticated.
	 *
	 * @param string $username
	 * @param string $password
	 * @return bool False on error, true otherwise
	 */
	protected function getMinetestUserToken( $username,  $password ) {

		$curl = $this->getMinetestCurlClient( $this->minetestUrl.'/query' );

		$params = http_build_query( [
			'name' => $username,
			'password' => $password,
		] );

		curl_setopt_array( $curl, [
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $params,
		]);

		$ret = curl_exec( $curl );
		$info = curl_getinfo( $curl );
		$error = curl_error( $curl );
		curl_close( $curl );

        sleep(2);
                
        $query2 = $this->getMinetestCurlClient( $this->minetestUrl.'/status/'.$username );
        $ret = curl_exec ( $query2 );

            
		if ( !empty( $error ) ) {
			$this->logger->error( 'AuthMinetest: cURL error: '.$error );
			return false;

		} else if ( $info['http_code'] != 200 ) {
			$this->logger->error( 'AuthMinetest: cURL error: unexpected HTTP response code '.$info['http_code'] );
			return false;

		} 
        if ( $ret == "True" ) {
            return true;
            
        } else {
            return false;
        }

	}

    /**
     * @param null|\User $user
     * @param AuthenticationResponse $response
     */
    public function postAuthentication( $user, AuthenticationResponse $response ) {
		if ( $response->status !== AuthenticationResponse::PASS ) {
			return;
		}
        return;
	}


	public function testUserCanAuthenticate( $username ) {
		return $this->testUserExists( $username );
	}

	public function testUserExists( $username, $flags = User::READ_NORMAL ) {
		// TODO - there is no easy way to do this without additional web services on the Minetest side.
		return false;
	}

	public function providerAllowsPropertyChange( $property ) {
		return false;
	}

	public function providerAllowsAuthenticationDataChange( AuthenticationRequest $req, $checkData = true) {
		return \StatusValue::newGood( 'ignored' );
	}

	public function providerChangeAuthenticationData( AuthenticationRequest $req ) {
		return;
	}

	public function accountCreationType() {
		return self::TYPE_CREATE;
	}

    public function beginPrimaryAccountCreation( $user, $creator, array $reqs ) {
		throw new \BadMethodCallException( 'This should not get called' );
	}

    public function getAuthenticationRequests( $action, array $options ) {
        switch ( $action ) {
            case AuthManager::ACTION_LOGIN:
                return [ new PasswordAuthenticationRequest() ];
            default:
                return [];
        }
    }
}