Passed
Pull Request — master (#4916)
by
unknown
06:21
created

AuthController::authorizeAction()   B

Complexity

Conditions 8
Paths 28

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 34
c 1
b 0
f 0
nc 28
nop 0
dl 0
loc 51
rs 8.1315

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version    API
12
 *
13
 * @file      AuthControler.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2025 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
26
use Symfony\Component\HttpFoundation\Request AS symfonyRequest;
27
28
class AuthController extends BaseController
29
{
30
    /**
31
     * 
32
     */
33
    public function authorizeAction()
34
    {
35
        $request = symfonyRequest::createFromGlobals();
36
        $requestMethod = $request->getMethod();
37
        $strErrorDesc = $responseData = $strErrorHeader = '';
38
39
        if (strtoupper($requestMethod) === 'POST') {
40
            require API_ROOT_PATH . "/Model/AuthModel.php";
41
            try {
42
                $authModel = new AuthModel();
43
44
                // Leer el JSON del body del POST
45
                $body = $request->getContent();
46
                $data = json_decode($body, true);
47
48
                $login = $data['login'] ?? null;
49
                $password = $data['password'] ?? null;
50
                $apikey = $data['apikey'] ?? null;
51
52
                // Validación mínima para evitar null
53
                if (!$login || !$password || !$apikey) {
54
                    throw new Exception('Missing parameter');
55
                }
56
57
                $arrUser = $authModel->getUserAuth($login, $password, $apikey);
58
59
                if (array_key_exists("token", $arrUser)) {
60
                    $responseData = json_encode($arrUser);
61
                } else {
62
                    $strErrorDesc = $arrUser['error'] . " (" . $arrUser['info'] . ")";
63
                    $strErrorHeader = 'HTTP/1.1 401 Unauthorized';
64
                }
65
            } catch (Error|Exception $e) {
66
                $strErrorDesc = $e->getMessage().' Something went wrong! Please contact support.2';
67
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
68
            }
69
        } else {
70
            $strErrorDesc = 'Method '.$requestMethod.' not supported';
71
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
72
        }
73
74
        // send output
75
        if (empty($strErrorDesc) === true) {
76
            $this->sendOutput(
77
                $responseData,
78
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
79
            );
80
        } else {
81
            $this->sendOutput(
82
                json_encode(['error' => $strErrorDesc]), 
83
                ['Content-Type: application/json', $strErrorHeader]
84
            );
85
        }
86
    }
87
}
88