ProfilesAPI::logout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
class ProfilesAPI extends Flipside\Http\Rest\RestAPI
3
{
4
    public function setup($app)
5
    {
6
        $app->post('/login[/]', array($this, 'login'));
7
        $app->post('/logout[/]', array($this, 'logout'));
8
        $app->post('/zip[/]', array($this, 'validateZip'));
9
    }
10
11
    public function login($request, $response)
12
    {
13
        $params = $request->getParams();
14
        if(!isset($params['username']) || !isset($params['password']))
15
        {
16
            $response->getBody()->write('Missing Required Parameters!');
17
            return $response->withStatus(400);
18
        }
19
        $auth = Flipside\AuthProvider::getInstance();
20
        $res = $auth->login($params['username'], $params['password']);
21
        if($res === false)
22
        {
23
            return $response->withStatus(403);
24
        }
25
        else
26
        {
27
            $user = \Flipside\FlipSession::getUser();
28
            $privateKey = file_get_contents('/var/www/secure_settings/jwtRS256.key');
29
            $groups = $user->getGroups();
30
            if($groups === false)
31
            {
32
                $groups = array();
33
            }
34
            $count = count($groups);
35 View Code Duplication
            for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            {
37
                $groups[$i] = $groups[$i]->getGroupName();
38
            }
39
            $token = array(
40
                'iss' => $request->getUri()->getHost(),
41
                'sub' => $user->uid,
42
                'private' => array('Flipside'=>array(
43
                    'email'=>$user->mail,
44
                    'groups'=>$groups,
45
                    'sessionIDs'=> array(
46
                        'php'=>session_id()
47
                        )
48
                    )      
49
                )
50
            );
51
            $cookieParams = session_get_cookie_params();
52
            $jwt = \Firebase\JWT\JWT::encode($token, $privateKey, 'RS256');
53
            $response = $response->withHeader('Set-Cookie', 'Flipside_JWT='.$jwt.'; path=/; domain='.$cookieParams['domain'].'; secure');
54
            return $response->withJson($res);
55
        }
56
    }
57
58
    public function logout($request, $response)
59
    {
60
        \Flipside\FlipSession::end();
61
        $cookieParams = session_get_cookie_params();
62
        $response = $response->withHeader('Set-Cookie', 'Flipside_JWT=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain='.$cookieParams['domain'].'; secure');
63
        return $response->withJson(true);
64
    }
65
66
    public function validateZip($request, $response, $args)
67
    {
68
        $obj = $request->getQueryParams();
69
        if(empty($obj))
70
        {
71
            $obj = (array)$request->getParsedBody();
72
        }
73
        $ret = false;
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
        if($obj['c'] == 'US')
75
        {
76
            if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $obj['postalCode']))
77
            {
78
                $contents = file_get_contents('http://ziptasticapi.com/'.$obj['postalCode']);
79
                $resp = json_decode($contents);
80
                if(isset($resp->error))
81
                {
82
                    $ret = $resp->error;
83
                }
84
                else
85
                {
86
                    $ret = true;
87
                }
88
            }
89
            else
90
            {
91
                $ret = 'Invalid Zip Code!';
92
            }
93
        }
94
        else
95
        {
96
            $ret = true;
97
        }
98
        return $response->withJson($ret);
99
    }
100
}
101
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
102