Failed Conditions
Pull Request — newinternal (#527)
by Simon
16:02 queued 05:59
created

PageOAuth   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 24 3
A detach() 0 31 4
A main() 0 4 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages\UserAuth;
10
11
use Exception;
12
use Waca\DataObjects\User;
13
use Waca\Exceptions\AccessDeniedException;
14
use Waca\Exceptions\ApplicationLogicException;
15
use Waca\Exceptions\CurlException;
16
use Waca\Exceptions\OAuthException;
17
use Waca\Helpers\OAuthUserHelper;
18
use Waca\Session;
19
use Waca\Tasks\InternalPageBase;
20
use Waca\WebRequest;
21
22
class PageOAuth extends InternalPageBase
23
{
24
    /**
25
     * Attach entry point
26
     *
27
     * must be posted, or will redirect to preferences
28
     */
29
    protected function attach()
30
    {
31
        if (!WebRequest::wasPosted()) {
32
            $this->redirect('preferences');
33
34
            return;
35
        }
36
37
        $database = $this->getDatabase();
38
39
        $this->validateCSRFToken();
40
41
        $oauthProtocolHelper = $this->getOAuthProtocolHelper();
42
        $user = User::getCurrent($database);
43
        $oauth = new OAuthUserHelper($user, $database, $oauthProtocolHelper, $this->getSiteConfiguration());
44
45
        try {
46
            $authoriseUrl = $oauth->getRequestToken();
47
            $this->redirectUrl($authoriseUrl);
48
        }
49
        catch (CurlException $ex) {
50
            throw new ApplicationLogicException($ex->getMessage(), 0, $ex);
51
        }
52
    }
53
54
    /**
55
     * Detach account entry point
56
     */
57
    protected function detach()
58
    {
59
        if ($this->getSiteConfiguration()->getEnforceOAuth()) {
60
            throw new AccessDeniedException($this->getSecurityManager());
61
        }
62
63
        $database = $this->getDatabase();
64
        $user = User::getCurrent($database);
65
        $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration());
66
67
        try {
68
            $oauth->refreshIdentity();
69
        }
70
        catch (CurlException $ex) {
71
            // do nothing. The user's already revoked this access anyway.
72
        }
73
        catch (OAuthException $ex) {
74
            // do nothing. The user's already revoked this access anyway.
75
        }
76
77
        $oauth->detach();
78
79
        // TODO: figure out why we need to force logout after a detach.
80
        $user->setForcelogout(true);
81
        $user->save();
82
83
        // force the user to log out
84
        Session::destroy();
85
86
        $this->redirect('login');
87
    }
88
89
    /**
90
     * Main function for this page, when no specific actions are called.
91
     * @return void
92
     */
93
    protected function main()
94
    {
95
        $this->redirect('preferences');
96
    }
97
}
98