2016-05-10 12 views
6

vs ben bir API İnce v2 ile inşa ettim ve "kimlik doğrulaması" bir katman işlevi geçen bazı yolların güvenliğini: Bir başlıkları Yetkilendirme değeri gönderilmişse,SLIM Çerçeve Rota Kimlik v2 v3

/** 
    * List marca novos 
    * method GET 
    * url /novos/marca/:idmarca 
    */ 
    $app->get('/novos/marca/:idmarca', 'authenticate', function($idmarca) { 
     $response = array(); 
     $db = new DbHandler('dbnovos'); 


     // fetching marca 
     $marca = $db->getMarcaNovos($idmarca); 

     $response["error"] = false; 
     $response["marca"] = array(); 

     array_walk_recursive($marca, function(&$val) { 
      $val = utf8_encode((string)$val); 
     }); 

     array_push($response["marca"], $marca); 

     echoRespnse(200, $response, "marcaoutput"); 
    })->via('GET', 'POST'); 

kimlik doğrulaması işlevi çekleri (user_api_key) ve veritabanına karşı kontrol eder.

Ben folowwing güzergahı ile İnce v3 API aynı işlevselliği almaya çalışıyorum:

/** 
    * List marca novos 
    * method GET 
    * url /novos/marca/:idmarca 
    */ 
    $app->get('/novos/marca/{idmarca}', function ($request, $response, $args) { 

    $output = array(); 
    $db = new DbHandler('mysql-localhost'); 
    $marca = $db->getMarcaNovos($args['idmarca']); 

    if ($marca != NULL) { 
     $i = 0; 
     foreach($marca as $m) { 
      $output[$i]["id"] = $m['id']; 
      $output[$i]["nome"] = utf8_encode($m['nome']); 
      $i++; 
     } 

    } else { 
     // unknown error occurred 
     $output['error'] = true; 
     $output['message'] = "An error occurred. Please try again"; 
    } 

    // Render marca view 
    echoRespnse(200, $response, $output, "marca"); 
})->add($auth); 

Bu benim katman

/** 
* Adding Middle Layer to authenticate every request 
* Checking if the request has valid api key in the 'Authorization' header 
*/ 
$auth = function ($request, $response, $next) { 

$headers = $request->getHeaders(); 
$outcome = array(); 

// Verifying Authorization Header 
if (isset($headers['Authorization'])) { 
    $db = new DbHandler('mysql-localhost'); 

    // get the api key 
    $api_key = $headers['Authorization']; 
    // validating api key 
    if (!$db->isValidApiKey($api_key)) { 
     // api key is not present in users table 
     $outcome["error"] = true; 
     $outcome["message"] = "Access Denied. Invalid Api key"; 
     echoRespnse(401, $outcome, $output); 
    } else { 
     global $user_id; 
     // get user primary key id 
     $user_id = $db->getUserId($api_key); 
     $response = $next($request, $response); 
     return $response; 
    } 
} else { 
    // api key is missing in header 
    $outcome["error"] = true; 
    $outcome["message"] = "Api key is missing"; 
    //echoRespnse(400, $response, $outcome); 
    return $response->withStatus(401)->write("Not allowed here - ".$outcome["message"]); 
} 

}; 

olduğu Ama her zaman hata alıyorum: "Değil Burada izin verilir - Api anahtarı eksik " Temelde, eğer $ başlık ['Yetkilendirme'] ayarlanmışsa test başarısız oluyor. $ Headers dizi yapısı nedir veya başlık üzerinden Yetkilendirme değerini nasıl alabilirim?

cevap

2

Geçerli HTTP Temel Yetkilendirme başlığından başka bir şey gönderiyorsanız, PHP buna erişemez. Aşağıdaki yeniden yazma kuralını .htaccess dosyanıza ekleyerek bu konuda çalışabilirsiniz.

RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 
+1

Bitiş noktası çağrısını postacı krom uygulamasıyla test ediyorum. Yetkilendirme anahtarını yalnızca değer olarak belirteçle gönderiyorum. V2'de de aynı şekilde çalışıyor, önerilen kural ve çıktıyı eklemek denedim aynı. – mjpramos

İlgili konular