Can't authenticate for REST testing - yii2

I have the following virtual host name setup on xampp: reporting.dev
public function userIsAuthenticated(\ApiTester $I)
{
$I->amGoingTo('Check Authentication works');
// i've tried both of these ways to authenticate
$I->amHttpAuthenticated('my_email', 'my_password');
$I->haveHttpHeader('Authorization', 'Basic super_long_token');
$I->sendGET(Url::toRoute('/api/reports', true));
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->deleteHeader('Authorization');
}
The following request works in postman http://reporting.dev/api/reports using a Basic Auth header and the same token in the test above.
This is my api suite config:
class_name: ApiTester
modules:
enabled:
- REST:
depends: PhpBrowser
#url: /api/
part: Json
- Yii2:
part: [orm, fixtures]
configFile: 'config/web.php'
I'm using Yii2, if I remove the behaviors function from my api controller that determines the authentication as Basic Auth, I then do get a 200 response and the expected json.
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
'except' => [],
];
return $behaviors;
}
So I'm unsure what else I can do here or why I'm not authenticated

The problem I had here was the auth token I have in my DB was not base64 encoded.
public function userIsAuthenticated(\ApiTester $I)
{
$I->amGoingTo('Check Authentication works');
// fixed now
$I->haveHttpHeader('Authorization', 'Basic ' . base64_encode('Basic super_long_db_auth_token'));
$I->sendGET(Url::toRoute('/api/reports', true));
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->deleteHeader('Authorization');
}

Related

Yii2 Stripe Webhook testing: "[ERROR] Failed to Post"

I set up a basic Webhook endpoint in a Yii2 controller just to test the connection:
class CreditCardController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'webhook' => ['post'],
],
],
];
}
public function beforeAction($action)
{
if ($action->id == 'webhook')
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
... I just want to dump the payload and return HTTP 200 (roughly based on an example I followed here)
public function actionWebhook()
{
$payload = file_get_contents('php://input');
ob_start();
var_dump($payload);
error_log(ob_get_clean(), 4);
echo json_encode(['status' => 'success']);
}
I installed the Stripe CLI and forwarded the Webhook to my local test server:
stripe listen -f https://testsite.office/credit-card/webhook
When I trigger an event that includes something I am listening for:
stripe trigger invoice.payment_succeeded
I get this message:
[ERROR] Failed to POST: Post "https://testsite.office/credit-card/webhook": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
If I remove the POST rule for the action and code the URL in a browser, it works fine.
Any ideas?
Joe
For anyone slogging along with the same issue (I don't see a lot of Stripe/Yii2 webhook chatter online), here is what worked for me.
I noticed that Yii threw a HeadersAlreadySentException in the Apache error_log. So I had to had to specify the response format as JSON:
return new Response([
'format' => Response::FORMAT_JSON,
'statusCode' => 200,
'statusText' => 'Webhook Handled',
]);
Probably not consequential to this problem, but I also changed accessing the payload to the "Yii way":
$payload = json_decode(Yii::$app->request->getRawBody(), true);

How to read JSON data in laravel controller?

Before this, I was getting 405 methods not allowed error in live server
ajax call to upload files:
$('#form-repeater').on('submit',function(e){
e.preventDefault();
$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
let thiss=$(this);
let form = document.getElementById('form-repeater');
let data =new FormData(form);
$.ajax({
type:thiss.attr('method'),
url:thiss.attr('action'),
data:data,
dataType:'JSON',
// ContentType:'application/json',
cache: false,
processData: false,
success:function(response){
if(response.message=='1'){
Swal.fire(
'Product Added Successfully',
'',
'success'
)
setTimeout(function(){
window.location.href="/banner";
}, 2000);//wait 2 seconds
}
else{
error = response.errors;
if(error.staff){
$('#form-repeater .invalid-staff').html(error.staff);
}else{
$('#form-repeater .invalid-staff').html('');
}
if(error.customerNumber){
$('#form-repeater .invalid-cust_numb').html(error.customerNumber);
}else{
$('#form-repeater .invalid-cust_numb').html('');
}}});
This is my response from ajax call:enter image description here
normally I can't retrieve data in my controller:
I debug using dd($request->all());
When sending JSON requests to your application, you may access the JSON data via the input method as long as the Content-Type header of the request is properly set to application/json. You may even use "dot" syntax to dig into JSON arrays: $name = $request->input('user.name');
Then on your controller you could do something like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
$name = $request->input('user.name');
// Manage your data the way you want
}
}

Laravel setup for basic API and AJAX-calls

Laravel POST return type?
I'm setting up an API alpha version using basic auth single sign on.
Cant debug with postman/js because its not returning JSON but redirecting me instead. From what Ive read AJAX calls shouldnt be redirected? Do I have errors in the configuration?
Routes:
Route::post('/test1', 'tradesCtrl#test1']);
//Route::post('/test1', ['middleware' => 'auth.basic.once', 'uses' => 'tradesCtrl#test1']);
AJAX call:
$.post(
"http://localhost:8000/test1",
{
p1:"100a",
p2:80
},
function(data, status,jqXHR){
console.log(data);
});
Controller (this will echo HTTP (!):
public function test1(Request $request)
{
if($request->ajax()) {
echo "AJAX!";
} else {
echo "HTTP!";
}
}
Allow cross domain for now in App.php
// allow origin
header('Access-Control-Allow-Origin: *');
Kernel.php disable csrf protection by commeting out
app\Http\Middleware\VerifyCsrfToken::class
If I try to insert validation for parameters
public function test1(Request $request)
{
$this->validate($request, [
'p1' => 'Integer',
'p2' => 'Integer'
]);
echo "Serving stuff";
}
This immediately return in a 404 page not found when failing validation, probably a redirect to something else that's not working??

AWS API Gateway - Error when calling API method - 'Could not parse request body into json'

I have a simple POST method connected to a lambda function in my AWS API Gateway.
When I perform a test (via the Gateway API console) everything works fine and the test gets the results I am looking for. Its simple - post a JSON, get back a JSON.
However, after deploying the API and then sending the same JSON used in the test (via http post), I am getting 'Could not parse request body into json'.
Does anyone know what I may be doing wrong?
Note: I am not looking to use models, I just want to pass-through the JSON. I believe that when Amazon writes things like 'input passthrough'
they mean that the input can pass through to the lambda function.
Here are images of my AWS Gateway API setup:
METHOD REQUEST:
INTEGRATION REQUEST:
METHOD RESPONSE:
INTEGRATION RESPONSE:
I found the solution. The problem was that in the POST request you need to send your body as a string and not a JSON object and that string needs to be formatted correct
ie '{"key1": "val1","key2": 22,"key3": 15,"key4": "val4"}'
like so:
function post() {
$.ajax({
url: "https://myapi.us-east-1.amazonaws.com/myStage/myPath",
type: "POST",
data: '{"key1": "val1","key2": 22,"key3": 15,"key4": "val4"}',
mozSystem: true,
dataType: "text", //set to"json" usually
success: function (result) {
switch (result) {
case true:
processResponse(result);
break;
default:
resultDiv.html(result);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error ' + xhr.status + ' ' + thrownError);
alert(thrownError);
}
});
};
Set your parameter in string format ( no json and no array) :
I used http_build_query() function in my curl execution :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://your_url_here",
CURLOPT_POST => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => http_build_query(array(
"key1" => "val1",
"key2" => "val2",
))
));
$resp = curl_exec($curl);
print_r($resp);
if (!curl_exec($curl)) {
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
curl_close($curl);

Laravel 5.1 consuming soap wsdl service using controller and model

Currently I'm using php and nusoap and wanted to convert it to Laravel.
When creating the soap calls I use data out of a mysql database.
So I think I would need a model (to get my data) and a controller (to create request).
EDIT:
<?php
namespace App\Http\Controllers;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
class SoapController extends Controller {
public function demo()
{
// Add a new service to the wrapper
SoapWrapper::add(function ($service) {
$service
->name('currency')
->wsdl('path/to/wsdl')
->trace(true);
->options(['user' => 'username', 'pass' => 'password']);
});
// Using the added service
SoapWrapper::service('currency', function ($service) {
var_dump($service->getFunctions());
var_dump($service->call('Otherfunction'));
});
}
}
from laravel-soap I couldn't find a tutorial on how to send login parameters prior to any other request. In the example 'using the added service' I see the login credentials but it doesn't work.
This is how I got soap to work in Laravel 5.1
clean install laravel 5.1
install artisaninweb/laravel-soap
create a controller SoapController.php
<?php
namespace App\Http\Controllers;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
class SoapController extends Controller {
public function demo()
{
// Add a new service to the wrapper
SoapWrapper::add(function ($service) {
$service
->name('currency')
->wsdl('path/to/wsdl')
->trace(true);
});
$data = [
'user' => 'username',
'pass' => 'password',
];
// Using the added service
SoapWrapper::service('currency', function ($service) use ($data) {
var_dump($service->call('Login', [$data]));
var_dump($service->call('Otherfunction'));
});
}
}
Create a route in your routes.php
Route::get('/demo', ['as' => 'demo', 'uses' => 'SoapController#demo']);
If requered you can also use the model extension as described here