MediaWiki REL1_34
ApiOATHValidate.php
Go to the documentation of this file.
1<?php
20
24use ApiBase;
25use User;
26use ApiResult;
27use FormatJson;
28
35class ApiOATHValidate extends ApiBase {
36 public function execute() {
37 // Be extra paranoid about the data that is sent
38 $this->requireAtLeastOneParameter( $this->extractRequestParams(), 'totp', 'data' );
39 $this->requirePostedParameters( [ 'token', 'data', 'totp' ] );
40
41 $params = $this->extractRequestParams();
42 if ( $params['user'] === null ) {
43 $params['user'] = $this->getUser()->getName();
44 }
45
46 $this->checkUserRightsAny( 'oathauth-api-all' );
47
48 $user = User::newFromName( $params['user'] );
49 if ( $user === false ) {
50 $this->dieWithError( 'noname' );
51 }
52
53 // Don't increase pingLimiter, just check for limit exceeded.
54 if ( $user->pingLimiter( 'badoath', 0 ) ) {
55 $this->dieWithError( 'apierror-ratelimited' );
56 }
57
58 $result = [
59 ApiResult::META_BC_BOOLS => [ 'enabled', 'valid' ],
60 'enabled' => false,
61 'valid' => false,
62 'module' => ''
63 ];
64
65 if ( !$user->isAnon() ) {
66 $userRepo = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' );
67 $authUser = $userRepo->findByUser( $user );
68 if ( $authUser ) {
69 $module = $authUser->getModule();
70 if ( $module instanceof IModule ) {
71 $data = [];
72 if ( isset( $params['totp'] ) ) {
73 // Legacy
74 if ( $module instanceof TOTP ) {
75 $data = [
76 'token' => $params['totp']
77 ];
78 }
79 } else {
80 $decoded = FormatJson::decode( $params['data'], true );
81 if ( is_array( $decoded ) ) {
82 $data = $decoded;
83 }
84 }
85 $result['enabled'] = $module->isEnabled( $authUser );
86 $result['valid'] = $module->verify( $authUser, $data ) !== false;
87 $result['module'] = $module->getName();
88 }
89 }
90 }
91
92 $this->getResult()->addValue( null, $this->getModuleName(), $result );
93 }
94
95 public function getCacheMode( $params ) {
96 return 'private';
97 }
98
99 public function isInternal() {
100 return true;
101 }
102
103 public function needsToken() {
104 return 'csrf';
105 }
106
107 public function getAllowedParams() {
108 return [
109 'user' => [
110 ApiBase::PARAM_TYPE => 'user',
111 ],
112 'totp' => [
113 ApiBase::PARAM_TYPE => 'string',
115 ],
116 'data' => [
117 ApiBase::PARAM_TYPE => 'string'
118 ]
119 ];
120 }
121
122 protected function getExamplesMessages() {
123 return [
124 'action=oathvalidate&totp=123456&token=123ABC'
125 => 'apihelp-oathvalidate-example-1',
126 'action=oathvalidate&user=Example&totp=123456&token=123ABC'
127 => 'apihelp-oathvalidate-example-2',
128 'action=oathvalidate&user=Example&data={"totp":"123456"}&token=123ABC'
129 => 'apihelp-oathvalidate-example-3',
130 ];
131 }
132}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition ApiBase.php:112
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:2130
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
requirePostedParameters( $params, $prefix='prefix')
Die if any of the specified parameters were found in the query part of the URL rather than the post b...
Definition ApiBase.php:989
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
requireAtLeastOneParameter( $params, $required)
Die if none of a certain set of parameters is set and not false.
Definition ApiBase.php:959
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
This class represents the result of the API operations.
Definition ApiResult.php:35
JSON formatter wrapper class.
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
needsToken()
Returns the token type this module requires in order to execute.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:518
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
return true
Definition router.php:94