Proyectos de Subversion LeadersLinked - Services

Rev

Rev 34 | Rev 44 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 34 Rev 43
Línea 173... Línea 173...
173
        
173
        
174
 
174
 
-
 
175
 
175
 
176
        $this->currentNetworkPlugin = new CurrentNetworkPlugin($adapter);
-
 
177
        if(!$this->currentNetworkPlugin->hasNetwork()) {
176
        $this->currentNetworkPlugin = new CurrentNetworkPlugin($adapter);
178
            
177
        if(!$this->currentNetworkPlugin->hasNetwork()) {
179
            $response = $event->getResponse();
-
 
180
            $code = 401;
178
            $response = $event->getResponse();
181
            $content = json_encode(['success' => false, 'data' => '401 Unauthorized - Private network - not found', 'fatal' => true]);
Línea 179... Línea 182...
179
            $this->sendResponse($response, ['success' => false, 'data' => '401 Unauthorized - Private network - not found', 'fatal' => true]);
182
            $this->sendResponse($response, $code, $content);
-
 
183
 
180
            exit;
184
        }
-
 
185
        
181
        }
186
        if($this->currentNetworkPlugin->getNetwork()->status == Network::STATUS_INACTIVE) {
182
        
187
            
-
 
188
            $response = $event->getResponse();
183
        if($this->currentNetworkPlugin->getNetwork()->status == Network::STATUS_INACTIVE) {
189
            $code = 401;
Línea 184... Línea 190...
184
            $response = $event->getResponse();
190
            $content = json_encode(['success' => false, 'data' => '401 Unauthorized - Private network - inactive', 'fatal' => true]);
185
            $this->sendResponse($response, ['success' => false, 'data' => '401 Unauthorized - Private network - inactive', 'fatal' => true]);
191
            $this->sendResponse($response, $code, $content);
Línea 196... Línea 202...
196
            $tokenAuthAdapter->setData($device_uuid, $password, $timestamp, $rand);
202
            $tokenAuthAdapter->setData($device_uuid, $password, $timestamp, $rand);
Línea 197... Línea 203...
197
            
203
            
198
            $authService = new AuthenticationService();
204
            $authService = new AuthenticationService();
199
            $result = $authService->authenticate($tokenAuthAdapter);
205
            $result = $authService->authenticate($tokenAuthAdapter);
200
            if($result->getCode() != \Laminas\Authentication\Result::SUCCESS) {
-
 
Línea -... Línea 206...
-
 
206
            if($result->getCode() != \Laminas\Authentication\Result::SUCCESS) {
-
 
207
                
201
                $response = $event->getResponse();
208
                $response = $event->getResponse();
-
 
209
                $code = 200;
-
 
210
                $content = json_encode(['success' => false, 'data' => $result->getMessages()[0], 'fatal' => true]);
202
                
211
                $this->sendResponse($response, $code, $content);
Línea 203... Línea 212...
203
                $this->sendResponse($response, ['success' => false, 'data' => $result->getMessages()[0], 'fatal' => true]);
212
 
Línea 204... Línea 213...
204
            }
213
            }
Línea 212... Línea 221...
212
            
221
            
Línea 213... Línea 222...
213
            $token = trim(empty($headers['authorization']) ? $headers['Authorization'] : $headers['authorization']);
222
            $token = trim(empty($headers['authorization']) ? $headers['Authorization'] : $headers['authorization']);
214
            
-
 
-
 
223
            
Línea 215... Línea 224...
215
 
224
 
Línea 216... Línea 225...
216
            if (substr($token, 0, 6 ) == 'Bearer') {
225
            if (substr($token, 0, 6 ) == 'Bearer') {
217
                $response = $event->getResponse();
226
 
Línea 225... Línea 234...
225
                    try { 
234
                    try { 
226
                        $payload = JWT::decode($token, new Key($key, 'HS256'));
235
                        $payload = JWT::decode($token, new Key($key, 'HS256'));
Línea 227... Línea 236...
227
                        
236
                        
-
 
237
                        
228
                        
238
                        if(empty($payload->iss) || $payload->iss != $_SERVER['HTTP_HOST']) {
-
 
239
                            
229
                        if(empty($payload->iss) || $payload->iss != $_SERVER['HTTP_HOST']) {
240
                            $response = $event->getResponse();
-
 
241
                            $code = 401;
-
 
242
                            $content = json_encode(['success' => false, 'data' => 'Unauthorized - JWT - Wrong server', 'fatal' => true]);
-
 
243
                            $this->sendResponse($response, $code, $content);
Línea 230... Línea 244...
230
                            $response = $event->getResponse();
244
                            
Línea 231... Línea 245...
231
                            $this->sendResponse($response, ['success' => false, 'data' => 'Unauthorized - JWT - Wrong server',  'fatal'  => true]);
245
 
232
                            
246
                            
Línea 245... Línea 259...
245
                                    $authByUserId->setData($jwtToken->user_id);
259
                                    $authByUserId->setData($jwtToken->user_id);
Línea 246... Línea 260...
246
                                    
260
                                    
247
                                    $authService = new AuthenticationService();
261
                                    $authService = new AuthenticationService();
248
                                    $result = $authService->authenticate($authByUserId);
262
                                    $result = $authService->authenticate($authByUserId);
-
 
263
                                    if($result->getCode() != \Laminas\Authentication\Result::SUCCESS) {
249
                                    if($result->getCode() != \Laminas\Authentication\Result::SUCCESS) {
264
                                        
-
 
265
                                        $response = $event->getResponse();
-
 
266
                                        $code = 401;
-
 
267
                                        $content = json_encode(['success' => false, 'data' => $result->getMessages()[0], 'fatal' => true]);
Línea 250... Línea -...
250
                                        $response = $event->getResponse();
-
 
251
                                        
268
                                        $this->sendResponse($response, $code, $content);
Línea 252... Línea 269...
252
                                        $this->sendResponse($response, ['success' => false, 'data' => $result->getMessages()[0], 'fatal' => true]);
269
                                        
253
                                    }
270
                                    }
Línea 254... Línea 271...
254
                                    
271
                                    
255
                                    $this->authByJWT = true;
272
                                    $this->authByJWT = true;
-
 
273
                                }
256
                                }
274
                                
-
 
275
                                
-
 
276
                            } else {
257
                                
277
                                $response = $event->getResponse();
Línea 258... Línea 278...
258
                                
278
                                $code = 401;
259
                            } else {
279
                                $content = json_encode(['success' => false, 'data' => 'Unauthorized - JWT - Expired', 'fatal' => true]);
-
 
280
                                $this->sendResponse($response, $code, $content);
260
                                $response = $event->getResponse();
281
 
-
 
282
                            }
261
                                $this->sendResponse($response, ['success' => false, 'data' => 'Unauthorized - JWT - Expired',  'fatal'  => true]);
283
                            
-
 
284
                        }
-
 
285
                    } catch(\Exception $e) {
262
                            }
286
                        
263
                            
287
                        $response = $event->getResponse();
264
                        }
288
                        $code = 401;
265
                    } catch(\Exception $e) {
289
                        $content = json_encode(['success' => false, 'data' => 'Unauthorized - JWT - Wrong key', 'fatal' => true]);
Línea 574... Línea 598...
574
    public function onRenderError(MvcEvent $event)
598
    public function onRenderError(MvcEvent $event)
575
    {
599
    {
576
        $this->processError($event);
600
        $this->processError($event);
577
    }
601
    }
Línea -... Línea 602...
-
 
602
    
-
 
603
    /**
-
 
604
     * 
-
 
605
     * @param \Laminas\Http\Response $response
-
 
606
     * @param int $code
-
 
607
     * @param string $content
578
    
608
     */
579
    public function sendResponse(\Laminas\Http\Response $response, $data)
609
    public function sendResponse($response, $code, $content)
-
 
610
    {
580
    {
611
        
581
        $headers = $response->getHeaders();
612
        $headers = $response->getHeaders();
582
        $headers->clearHeaders();
613
        $headers->clearHeaders();
Línea -... Línea 614...
-
 
614
        $headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
-
 
615
        
583
        $headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
616
        Functions::addCrossSiteToResponse($response);
584
        
617
        
585
        $response->setStatusCode(200);
618
        $response->setStatusCode($code);
586
        $response->setContent(json_encode($data));
619
        $response->setContent($content); //json_encode($data));
587
        $response->send();
620
        $response->send();
Línea 588... Línea 621...
588
        exit;
621
        exit;
Línea 598... Línea 631...
598
        $response = $event->getResponse();
631
        $response = $event->getResponse();
599
        if('error-exception' == $error) {
632
        if('error-exception' == $error) {
600
            $exception = $event->getParam('exception');
633
            $exception = $event->getParam('exception');
601
            error_log($exception->getCode() . ' ' . $exception->getMessage());
634
            error_log($exception->getCode() . ' ' . $exception->getMessage());
602
            error_log($exception->getTraceAsString());
635
            error_log($exception->getTraceAsString());
603
                
636
            
604
            $response = $event->getResponse();
637
            $response = $event->getResponse();
605
            $headers = $response->getHeaders();
-
 
606
            $headers->clearHeaders();
638
            $code = 500;
607
            $headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
-
 
608
            
-
 
609
            $response->setStatusCode(500);
-
 
610
            $response->setContent(json_encode(['success' => false, 'data' => $exception->getCode() . ' ' . $exception->getMessage(), 'fatal' => true]));
639
            $content = json_encode(['success' => false, 'data' => $exception->getCode() . ' ' . $exception->getMessage(), 'fatal' => true]);
611
            $response->send();
640
            $this->sendResponse($response, $code, $content);
Línea 612... Línea 641...
612
                
641
                
-
 
642
        } else if('error-router-no-match' == $error) {
613
        } else if('error-router-no-match' == $error) {
643
            
614
            $response = $event->getResponse();
644
            $response = $event->getResponse();
615
            $headers = $response->getHeaders();
645
            $code = 404;
616
            $headers->clearHeaders();
646
            $content = json_encode(['success' => false, 'data' => 'error-router-no-match', 'fatal' => true]);
617
            $headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
647
            $this->sendResponse($response, $code, $content);
618
            
-
 
619
            $response->setStatusCode(404);
-
 
620
            $response->setContent(json_encode(['success' => false, 'data' => 'error-router-no-match', 'fatal' => true]));
-
 
Línea 621... Línea 648...
621
            $response->send();
648
            
-
 
649
            
622
            
650
 
623
 
-
 
624
        } else if(' error-controller-not-found' == $error) {
651
        } else if(' error-controller-not-found' == $error) {
625
            $response = $event->getResponse();
-
 
626
            $headers = $response->getHeaders();
-
 
627
            $headers->clearHeaders();
-
 
628
            $headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
652
            
629
            
653
            $response = $event->getResponse();
630
            $response->setStatusCode(404);
-
 
Línea -... Línea 654...
-
 
654
            $code = 404;
-
 
655
            $content = json_encode(['success' => false, 'data' => 'error-controller-not-found', 'fatal' => true]);
631
            $response->setContent(json_encode(['success' => false, 'data' => 'error-controller-not-found', 'fatal' => true]));
656
            $this->sendResponse($response, $code, $content);
632
            $response->send();
657
            
633
        } else {
658
 
634
            
659
        } else {
635
            $response = $event->getResponse();
660
            $response = $event->getResponse();
636
            $headers = $response->getHeaders();
-
 
637
            $headers->clearHeaders();
-
 
638
            $headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
-
 
Línea 639... Línea 661...
639
            
661
            $code = 500;
Línea 640... Línea 662...
640
            $response->setStatusCode(500);
662
            $content = json_encode(['success' => false, 'data' => $error, 'fatal' => true]);
Línea 641... Línea -...
641
            $response->setContent(json_encode(['success' => false, 'data' => $error, 'fatal' => true]));
-
 
642
            $response->send();
-
 
643
 
663
            $this->sendResponse($response, $code, $content);
Línea 644... Línea 664...
644
        }
664
 
645
 
665