Passed
Push — master ( 53f161...b6d7ca )
by
unknown
43s
created
public_html/tc/services/Authentication.php 1 patch
Switch Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -29,108 +29,108 @@
 block discarded – undo
29 29
     $requestParams = substr($requestURI,strlen($context)+1);
30 30
     
31 31
     switch ($requestMethod) {
32
-    case 'GET':
33
-        if(isset($_SERVER["HTTP_AUTHORIZATION"])){
34
-            $jwt = JWTUtils::getTokenFromRequest($_SERVER["HTTP_AUTHORIZATION"]);
32
+        case 'GET':
33
+            if(isset($_SERVER["HTTP_AUTHORIZATION"])){
34
+                $jwt = JWTUtils::getTokenFromRequest($_SERVER["HTTP_AUTHORIZATION"]);
35 35
 
36
-            $open_id = Utils::getParameterFromRequest($requestParams, $user_id_param_index);
36
+                $open_id = Utils::getParameterFromRequest($requestParams, $user_id_param_index);
37 37
 
38
-            if(strlen($requestParams) > 1){
38
+                if(strlen($requestParams) > 1){
39 39
 
40
-                if(strlen($open_id) > 0){
40
+                    if(strlen($open_id) > 0){
41 41
 
42
-                    $user = new User();
42
+                        $user = new User();
43 43
 
44
-                    $user->setOpen_id($open_id);
44
+                        $user->setOpen_id($open_id);
45 45
 
46
-                    if(JWTUtils::validateJWT($jwt, $user)){
46
+                        if(JWTUtils::validateJWT($jwt, $user)){
47 47
 
48
-                        $result = UserController::getUserByOpenId($user);
48
+                            $result = UserController::getUserByOpenId($user);
49 49
 
50
-                        $json = json_encode($result, JSON_PRETTY_PRINT);
50
+                            $json = json_encode($result, JSON_PRETTY_PRINT);
51 51
 
52
-                        echo($json);
52
+                            echo($json);
53
+
54
+                        }else{
55
+                            header('HTTP/1.0 401 Unauthorized');
56
+                            echo json_encode(array("failed"=>"Invalid token"),JSON_FORCE_OBJECT);
57
+                            exit;
58
+                        }
53 59
 
54 60
                     }else{
55 61
                         header('HTTP/1.0 401 Unauthorized');
56
-                        echo json_encode(array("failed"=>"Invalid token"),JSON_FORCE_OBJECT);
62
+                        echo json_encode(array("failed"=>"No user id provided"),JSON_FORCE_OBJECT);
57 63
                         exit;
58 64
                     }
59
-
60 65
                 }else{
61 66
                     header('HTTP/1.0 401 Unauthorized');
62
-                    echo json_encode(array("failed"=>"No user id provided"),JSON_FORCE_OBJECT);
67
+                    echo json_encode(array("failed"=>'Invalid token, please reauthorize user'),JSON_FORCE_OBJECT);
63 68
                     exit;
64 69
                 }
65 70
             }else{
66 71
                 header('HTTP/1.0 401 Unauthorized');
67
-                echo json_encode(array("failed"=>'Invalid token, please reauthorize user'),JSON_FORCE_OBJECT);
72
+                echo json_encode(array("failed"=>'No authorization token provided'),JSON_FORCE_OBJECT);
68 73
                 exit;
69 74
             }
70
-        }else{
71
-            header('HTTP/1.0 401 Unauthorized');
72
-            echo json_encode(array("failed"=>'No authorization token provided'),JSON_FORCE_OBJECT);
73
-            exit;
74
-        }
75
-        break;
76
-    case 'POST':
77
-        if(isset($_SERVER["HTTP_AUTHORIZATION"])){
78
-            $jwt = JWTUtils::getTokenFromRequest($_SERVER["HTTP_AUTHORIZATION"]);
75
+            break;
76
+        case 'POST':
77
+            if(isset($_SERVER["HTTP_AUTHORIZATION"])){
78
+                $jwt = JWTUtils::getTokenFromRequest($_SERVER["HTTP_AUTHORIZATION"]);
79 79
         
80
-            $jsonBody = file_get_contents('php://input');
81
-            if(strlen($jsonBody) > 1){
80
+                $jsonBody = file_get_contents('php://input');
81
+                if(strlen($jsonBody) > 1){
82 82
                 
83
-                $credentials = json_decode($jsonBody, TRUE); //convert JSON into array
84
-                //var_dump($credentials);
85
-                if($credentials !== null){
83
+                    $credentials = json_decode($jsonBody, TRUE); //convert JSON into array
84
+                    //var_dump($credentials);
85
+                    if($credentials !== null){
86 86
 
87
-                    $user = new User();
87
+                        $user = new User();
88 88
 
89
-                    $user->setOpen_id($credentials["sub"]);
90
-                    $user->setName($credentials["name"]);
91
-                    $user->setEmail($credentials["email"]);
92
-                    $user->setUser_role($credentials["user_role"]);
89
+                        $user->setOpen_id($credentials["sub"]);
90
+                        $user->setName($credentials["name"]);
91
+                        $user->setEmail($credentials["email"]);
92
+                        $user->setUser_role($credentials["user_role"]);
93 93
 
94
-                    if(JWTUtils::validateJWT($jwt, $user)){
94
+                        if(JWTUtils::validateJWT($jwt, $user)){
95 95
                         
96
-                        $result = UserController::getUserByOpenId($user);
96
+                            $result = UserController::getUserByOpenId($user);
97 97
 
98
-                        $json = json_encode($result, JSON_PRETTY_PRINT);
98
+                            $json = json_encode($result, JSON_PRETTY_PRINT);
99 99
 
100
-                        echo($json);
100
+                            echo($json);
101
+                        }else{
102
+                            header('HTTP/1.0 401 Unauthorized');
103
+                            echo json_encode(array("failed"=>"Invalid token"),JSON_FORCE_OBJECT);
104
+                            exit;
105
+                        }
101 106
                     }else{
102
-                        header('HTTP/1.0 401 Unauthorized');
103
-                        echo json_encode(array("failed"=>"Invalid token"),JSON_FORCE_OBJECT);
107
+                        header('HTTP/1.0 400 Bad Request');
108
+                        echo json_encode(array("failed"=>"Bad Reqest"),JSON_FORCE_OBJECT);
104 109
                         exit;
105 110
                     }
111
+                
106 112
                 }else{
107 113
                     header('HTTP/1.0 400 Bad Request');
108 114
                     echo json_encode(array("failed"=>"Bad Reqest"),JSON_FORCE_OBJECT);
109 115
                     exit;
110 116
                 }
111
-                
112 117
             }else{
113
-                header('HTTP/1.0 400 Bad Request');
114
-                echo json_encode(array("failed"=>"Bad Reqest"),JSON_FORCE_OBJECT);
118
+                header('HTTP/1.0 401 Unauthorized');
119
+                echo json_encode(array("failed"=>'No authorization token provided'),JSON_FORCE_OBJECT);
115 120
                 exit;
116 121
             }
117
-        }else{
118
-            header('HTTP/1.0 401 Unauthorized');
119
-            echo json_encode(array("failed"=>'No authorization token provided'),JSON_FORCE_OBJECT);
120
-            exit;
121
-        }
122
-        break;
123
-    case 'DELETE':
124
-        //Here Handle DELETE Request 
125
-        break;
126
-    case 'PUT':
127
-        //Here Handle PUT Request 
128
-        break;
129
-    case 'OPTIONS':
130
-        //Here Handle OPTIONS/Pre-flight requests
131
-        header("Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Origin, x-access-token");
132
-        header("Access-Control-Allow-Methods: GET,POST");
133
-        echo("");
134
-        break;
122
+            break;
123
+        case 'DELETE':
124
+            //Here Handle DELETE Request 
125
+            break;
126
+        case 'PUT':
127
+            //Here Handle PUT Request 
128
+            break;
129
+        case 'OPTIONS':
130
+            //Here Handle OPTIONS/Pre-flight requests
131
+            header("Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Origin, x-access-token");
132
+            header("Access-Control-Allow-Methods: GET,POST");
133
+            echo("");
134
+            break;
135 135
    }
136 136
    ?>
137 137
\ No newline at end of file
Please login to merge, or discard this patch.