AuthController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A authorizeAction() 0 42 5
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
        $arrQueryStringParams = $this->getQueryStringParams();
39
40
        if (strtoupper($requestMethod) === 'POST') {
41
            require API_ROOT_PATH . "/Model/AuthModel.php";
42
            try {
43
                $authModel = new AuthModel();
44
                $arrUser = $authModel->getUserAuth(
45
                    $arrQueryStringParams['login'],
46
                    $arrQueryStringParams['password'],
47
                    $arrQueryStringParams['apikey']
48
                );
49
                if (array_key_exists("token", $arrUser)) {
50
                    $responseData = json_encode($arrUser);
51
                } else {
52
                    $strErrorDesc = $arrUser['error'] . " (" . $arrUser['info'] . ")";
53
                    $strErrorHeader = 'HTTP/1.1 401 Unauthorized';
54
                }
55
            } catch (Error $e) {
56
                $strErrorDesc = $e->getMessage().' Something went wrong! Please contact support.2';
57
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
58
            }
59
            
60
        } else {
61
            $strErrorDesc = 'Method '.$requestMethod.' not supported';
62
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
63
        }
64
65
        // send output
66
        if (empty($strErrorDesc) === true) {
67
            $this->sendOutput(
68
                $responseData,
69
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
70
            );
71
        } else {
72
            $this->sendOutput(
73
                json_encode(['error' => $strErrorDesc]), 
74
                ['Content-Type: application/json', $strErrorHeader]
75
            );
76
        }
77
    }
78
}