Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SecondaryAuthenticationProvider
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 getAuthenticationRequests
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 beginSecondaryAccountCreation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 beginSecondaryAuthentication
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 continueSecondaryAuthentication
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getProviderForModule
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OATHAuth\Auth;
4
5use MediaWiki\Auth\AbstractSecondaryAuthenticationProvider;
6use MediaWiki\Auth\AuthenticationRequest;
7use MediaWiki\Auth\AuthenticationResponse;
8use MediaWiki\Extension\OATHAuth\IModule;
9use MediaWiki\Extension\OATHAuth\OATHAuth;
10use MediaWiki\MediaWikiServices;
11use MediaWiki\User\User;
12
13class SecondaryAuthenticationProvider extends AbstractSecondaryAuthenticationProvider {
14    /**
15     * @param string $action
16     * @param array $options
17     *
18     * @return array
19     */
20    public function getAuthenticationRequests( $action, array $options ) {
21        return [];
22    }
23
24    /**
25     * @param User $user
26     * @param User $creator
27     * @param array|AuthenticationRequest[] $reqs
28     * @return AuthenticationResponse
29     */
30    public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
31        return AuthenticationResponse::newAbstain();
32    }
33
34    /**
35     * If the user has enabled two-factor authentication, request a second factor.
36     *
37     * @param User $user
38     * @param array $reqs
39     *
40     * @return AuthenticationResponse
41     */
42    public function beginSecondaryAuthentication( $user, array $reqs ) {
43        $authUser = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' )
44            ->findByUser( $user );
45
46        $module = $authUser->getModule();
47        if ( $module === null ) {
48            return AuthenticationResponse::newAbstain();
49        }
50
51        return $this->getProviderForModule( $module )
52            ->beginSecondaryAuthentication( $user, $reqs );
53    }
54
55    /**
56     * Verify the second factor.
57     * @inheritDoc
58     */
59    public function continueSecondaryAuthentication( $user, array $reqs ) {
60        $authUser = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' )
61            ->findByUser( $user );
62
63        $module = $authUser->getModule();
64        $provider = $this->getProviderForModule( $module );
65        $response = $provider->continueSecondaryAuthentication( $user, $reqs );
66        if ( $response->status === AuthenticationResponse::PASS ) {
67            $user->getRequest()->getSession()->set( OATHAuth::AUTHENTICATED_OVER_2FA, true );
68        }
69        return $response;
70    }
71
72    /**
73     * @param IModule $module
74     * @return AbstractSecondaryAuthenticationProvider
75     */
76    private function getProviderForModule( IModule $module ) {
77        $provider = $module->getSecondaryAuthProvider();
78        $services = MediaWikiServices::getInstance();
79        $provider->init(
80            $this->logger,
81            $this->manager,
82            $services->getHookContainer(),
83            $this->config,
84            $services->getUserNameUtils()
85        );
86        return $provider;
87    }
88}