Completed
Pull Request — develop (#67)
by
unknown
01:40
created

ProfilesAPI::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
class ProfilesAPI extends Http\Rest\RestAPI
3
{
4
    public function setup($app)
5
    {
6
        $app->options('/login[/]', array($this, 'optionsResponse'));
7
        $app->post('/login[/]', array($this, 'login'));
8
        $app->post('/logout[/]', array($this, 'logout'));
9
        $app->post('/zip[/]', array($this, 'validateZip'));
10
11
    }
12
13
    public function options($request, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    {
15
      return $response->withJson(true);
16
    }
17
18
    public function login($request, $response)
19
    {
20
        $params = $request->getParams();
21
        if(!isset($params['username']) || !isset($params['password']))
22
        {
23
            return $response->withStatus(400);
24
        }
25
        $auth = AuthProvider::getInstance();
26
        $res = $auth->login($params['username'], $params['password']);
27
        if($res === false)
28
        {
29
            return $response->withStatus(403);
30
        }
31
        else
32
        {
33
            $user = \FlipSession::getUser();
34
            $privateKey = file_get_contents('/var/www/secure_settings/jwtRS256.key');
35
            $groups = $user->getGroups();
36
            if($groups === false)
37
            {
38
                $groups = array();
39
            }
40
            $count = count($groups);
41 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...
42
            {
43
                $groups[$i] = $groups[$i]->getGroupName();
44
            }
45
            $token = array(
46
                'iss' => $request->getUri()->getHost(),
47
                'sub' => $user->uid,
48
                'private' => array('Flipside'=>array(
49
                    'email'=>$user->mail,
50
                    'groups'=>$groups,
51
                    'sessionIDs'=> array(
52
                        'php'=>session_id()
53
                        )
54
                    )      
55
                )
56
            );
57
            $cookieParams = session_get_cookie_params();
58
            $jwt = \Firebase\JWT\JWT::encode($token, $privateKey, 'RS256');
59
            $response = $response->withHeader('Set-Cookie', 'Flipside_JWT='.$jwt.'; path=/; domain='.$cookieParams['domain'].'; secure');
60
            return $response->withJson($res);
61
        }
62
    }
63
64
    public function logout($request, $response)
65
    {
66
        FlipSession::end();
67
        $cookieParams = session_get_cookie_params();
68
        $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');
69
        return $response->withJson(true);
70
    }
71
72
    public function validateZip($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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