this is my configuration for API’s with JWT Token And Simple database users table Token //application.php
$service->loadAuthenticator(‘Authentication.Jwt’, [
‘secretKey’ => file_get_contents(CONFIG . ‘jwt.pem’),
‘algorithm’ => ‘RS256’,
‘returnPayload’ => false
]);
$service->loadAuthenticator(‘Authentication.Form’, [
‘fields’ => $fields,
‘loginUrl’ => Router::url(‘/api/v1/usuaria/login.json’),
]);
//$service->loadAuthenticator(‘Authentication.Session’);$service->loadAuthenticator('Authentication.Token', [ 'queryParam' => 'token', 'header' => 'Authorization', 'tokenPrefix' => 'Token', ]); $service->loadIdentifier('Authentication.JwtSubject', ['fields' => $fields, 'resolver' => ['className' => 'Authentication.Orm'], 'tokenField' => 'userkeyid']); $service->loadIdentifier('Authentication.Password', [ 'fields' => $fields, 'resolver' => [ 'className' => 'Authentication.Orm', 'finder' => 'authenticatedUser' // <<< there it goes ] ]); return $service;
the above code working fine for Jwt token, please check below code
$token = ‘eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJteWFwcCIsInN1YiI6ODA2NTQ5MDksImV4cCI6MTY4OTc3NjcyMX0.cW2vqOm-2BDOFLAgOJ9pdWlNKcIBNSHJR1DWakPdDA0JDp-_604JCobNjsiQy5XHaVo3R9ba6Vb5hXpJtAvvR-V-iMjrewRXEmUcvDs4tWMAnaHslGjQTLAvPJGhc6GFHPSDK2cnTe0S2-ufA6iyNIwaTA_rj6xzuCeWNlQ7SSQ’;
$params = http_build_query([‘profile[first_name]’=>‘bheem’]);
$options = stream_context_create([‘http’=>[
‘method’=>‘GET’,
‘header’=>“Content-Type: application/x-www-form-urlencoded\r\n”.'Authorization: Bearer '.$token,
‘content’=>$params
]]);$auth = ‘http://localhost/skyweb/api/v1/usuaria/logout.json’;
$user = ‘http://localhost/skyweb/api/v1/users.json’;$api = file_get_contents($user,false,$options);
print_r($api);
the above code run in different system not in cakephp… cakephp just service provider application when client machine call Cakephp Apis then Cakephp return data… but need to token for accessing information so Jwt token works fine but I want to fetch data with both tokens like Simple Token and Jwt Token. i am using this plugin for simple token:-
- queryParam: Name of the query parameter. Configure it if you want to get the token from the query parameters.
- header: Name of the header. Configure it if you want to get the token from the header.
- tokenPrefix: The optional token prefix.
I have been following these steps but does not work.
‘header’=>“Content-Type: application/x-www-form-urlencoded\r\n”.'Authorization: Bearer '.$token,
it return 401 error so it means token does get cakephp side. then I try with like this https://example.com/api/v1/users.json?token=ldjlajsdljlajsdalkjdla even this is not work for me.
Just tell me how can i send token into Authorization Header. I do not want to use query parameter, better way is send in header Authorization.