Passed
Pull Request — master (#2)
by Tim
02:03
created

Twitter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 50
c 8
b 0
f 0
dl 0
loc 138
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A authenticate() 0 3 1
A finalStep() 0 41 4
A temporaryCredentials() 0 24 1
1
<?php
2
3
namespace SimpleSAML\Module\authtwitter\Auth\Source;
4
5
use League\OAuth1\Client\Server\Twitter as TwitterServer;
6
use SimpleSAML\Assert\Assert;
7
use SimpleSAML\Auth;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Module;
12
use SimpleSAML\Utils;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Authenticate using Twitter.
17
 *
18
 * @package simplesamlphp/simplesamlphp-module-authtwitter
19
 */
20
21
class Twitter extends Auth\Source
22
{
23
    /**
24
     * The string used to identify our states.
25
     */
26
    public const STAGE_TEMP = 'twitter:temp';
27
28
    /**
29
     * The key of the AuthId field in the state.
30
     */
31
    public const AUTHID = 'twitter:AuthId';
32
33
    /** @var string */
34
    private string $key;
35
36
    /** @var string */
37
    private string $secret;
38
39
    /** @var string */
40
    private string $scope;
41
42
    /** @var bool */
43
//    private bool $force_login;
44
45
    /** @var bool */
46
//    private bool $include_email;
47
48
    /**
49
     * Constructor for this authentication source.
50
     *
51
     * @param array $info  Information about this authentication source.
52
     * @param array $config  Configuration.
53
     */
54
    public function __construct(array $info, array $config)
55
    {
56
        // Call the parent constructor first, as required by the interface
57
        parent::__construct($info, $config);
58
59
        $configObject = Configuration::loadFromArray(
60
            $config,
61
            'authsources[' . var_export($this->authId, true) . ']'
62
        );
63
64
        $this->key = $configObject->getString('key');
65
        $this->secret = $configObject->getString('secret');
66
        $this->scope = $configObject->getString('scope');
67
//        $this->force_login = $configObject->getBoolean('force_login', false);
68
//        $this->include_email = $configObject->getBoolean('include_email', false);
69
    }
70
71
72
    /**
73
     * Log-in using Twitter platform
74
     *
75
     * @param array &$state  Information about the current authentication.
76
     */
77
    public function authenticate(array &$state): void
78
    {
79
        $this->temporaryCredentials($state);
80
    }
81
82
83
    /**
84
     * Retrieve temporary credentials
85
     *
86
     * @param array &$state  Information about the current authentication.
87
     */
88
    private function temporaryCredentials(array &$state): void
89
    {
90
        // We are going to need the authId in order to retrieve this authentication source later
91
        $state[self::AUTHID] = $this->authId;
92
93
        $stateId = base64_encode(Auth\State::saveState($state, self::STAGE_TEMP));
94
95
        $server = new TwitterServer(
96
            [
97
                'identifier' => $this->key,
98
                'secret' => $this->secret,
99
                'callback_uri' => Module::getModuleURL('authtwitter/linkback.php') . '?AuthState=' . $stateId,
100
            ]
101
        );
102
103
        // First part of OAuth 1.0 authentication is retrieving temporary credentials.
104
        // These identify you as a client to the server.
105
        $temporaryCredentials = $server->getTemporaryCredentials();
106
107
        $state['authtwitter:authdata:requestToken'] = serialize($temporaryCredentials);
108
        Auth\State::saveState($state, self::STAGE_TEMP);
109
110
        $server->authorize($temporaryCredentials);
111
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
112
    }
113
114
115
    /**
116
     * @param array &$state
117
     */
118
    public function finalStep(array &$state, Request $request): void
119
    {
120
        $requestToken = unserialize($state['authtwitter:authdata:requestToken']);
121
122
        $oauth_token = $request->get('oauth_token');
123
        if ($oauth_token === null) {
124
            throw new Error\BadRequest("Missing oauth_token parameter.");
125
        }
126
127
        if ($requestToken->getIdentifier() !== $oauth_token) {
128
            throw new Error\BadRequest("Invalid oauth_token parameter.");
129
        }
130
131
        $oauth_verifier = $request->get('oauth_verifier');
132
        if ($oauth_verifier === null) {
133
            throw new Error\BadRequest("Missing oauth_verifier parameter.");
134
        }
135
136
        $server = new TwitterServer(
137
            [
138
                'identifier' => $this->key,
139
                'secret' => $this->secret,
140
            ]
141
        );
142
143
        $tokenCredentials = $server->getTokenCredentials(
144
            $requestToken,
145
            $request->get('oauth_token'),
146
            $request->get('oauth_verifier')
147
        );
148
149
        $state['token_credentials'] = serialize($tokenCredentials);
150
        $userdata = $server->getUserDetails($tokenCredentials);
151
var_dump($userdata);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($userdata) looks like debug code. Are you sure you do not want to remove it?
Loading history...
152
exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
153
        $attributes = [];
0 ignored issues
show
Unused Code introduced by
$attributes = array() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
154
        $attributes['twitter_at_screen_name'] = ['@' . $userdata->uid];
155
        $attributes['twitter_screen_n_realm'] = [$userdata->uid . '@twitter.com'];
156
        $attributes['twitter_targetedID'] = ['http://twitter.com!' . $userdata->uid];
157
158
        $state['Attributes'] = $attributes;
159
    }
160
}
161