Passed
Push — master ( bda737...e0ed8e )
by Nils
09:32
created

AuthController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authorizeAction() 0 37 4
A verifyToken() 0 5 1
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 API
11
 *
12
 * @file      AuthControler.php
13
 * ---
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 *
17
 * @copyright 2009-2022 Teampass.net
18
 *
19
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
20
 * ---
21
 *
22
 * @see       https://www.teampass.net
23
 */
24
class AuthController extends BaseController
25
{
26
    /**
27
     * 
28
     */
29
    public function authorizeAction()
30
    {
31
        $strErrorDesc = '';
32
        $requestMethod = $_SERVER["REQUEST_METHOD"];
33
        $arrQueryStringParams = $this->getQueryStringParams();
34
35
        if (strtoupper($requestMethod) === 'POST') {
36
            // Get data
37
            $data = json_decode(file_get_contents("php://input"));
38
            $login = $data->login;
39
            $password = $data->password;
40
            $apikey = $data->apikey;
41
42
            require PROJECT_ROOT_PATH . "/Model/AuthModel.php";
43
            try {
44
                $authModel = new AuthModel();
45
                $arrUser = $authModel->getUserAuth($login, $password, $apikey);
46
                $responseData = json_encode($arrUser);
47
            } catch (Error $e) {
48
                $strErrorDesc = $e->getMessage().' Something went wrong! Please contact support.';
49
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
50
            }
51
            
52
        } else {
53
            $strErrorDesc = 'Method '.$requestMethod.' not supported';
54
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
55
        }
56
57
        // send output
58
        if (!$strErrorDesc) {
59
            $this->sendOutput(
60
                $responseData,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $responseData does not seem to be defined for all execution paths leading up to this point.
Loading history...
61
                array('Content-Type: application/json', 'HTTP/1.1 200 OK')
62
            );
63
        } else {
64
            $this->sendOutput(json_encode(array('error' => $strErrorDesc)), 
65
                array('Content-Type: application/json', $strErrorHeader)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $strErrorHeader does not seem to be defined for all execution paths leading up to this point.
Loading history...
66
            );
67
        }
68
    }
69
70
    public function verifyToken()
71
    {
72
        $strErrorDesc = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $strErrorDesc is dead and can be removed.
Loading history...
73
        $requestMethod = $_SERVER["REQUEST_METHOD"];
0 ignored issues
show
Unused Code introduced by
The assignment to $requestMethod is dead and can be removed.
Loading history...
74
        $arrQueryStringParams = $this->getQueryStringParams();
0 ignored issues
show
Unused Code introduced by
The assignment to $arrQueryStringParams is dead and can be removed.
Loading history...
75
76
    }
77
}