Enabling CORS in laravel using barryvdh/laravel-cors package? - angularjs

I am working on a simple Angular application and I wish to have my back end data from an API I created myself in Laravel. The API is sending back data just fine. But since I am developing my Angular application separately, I have an error when I use a service in Angular to fetch my resource.
Error
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8001/airports. This can be fixed by moving the resource to the same domain or enabling CORS.
So now I have installed a package in Laravel which is supposed to solve the issue but I am not sure how to use it. The cofig file of the package looks like:
'defaults' => array(
'supportsCredentials' => false,
'allowedOrigins' => array(),
'allowedHeaders' => array(),
'allowedMethods' => array(),
'exposedHeaders' => array(),
'maxAge' => 0,
'hosts' => array(),
),
'paths' => array(
'api/*' => array(
'allowedOrigins' => array('*'),
'allowedHeaders' => array('*'),
'allowedMethods' => array('*'),
'maxAge' => 3600,
),
'*' => array(
'allowedOrigins' => array('*'),
'allowedHeaders' => array('Content-Type'),
'allowedMethods' => array('POST', 'PUT', 'GET', 'DELETE'),
'maxAge' => 3600,
'hosts' => array('api.*'),
),
),
I am still getting the same error. Now what is it that I have to different in order to allow CORS?
Note: I have WAMP installed. I am developing both my Angular application and Laravel API on the same local server.

I ran into something similar with EmberJS instead of Angular. Laravel setup looks correct but I think you need to enable CORS support in Angular.
From a Chrome developer tutorial:
A word on Content Security Policy
Unlike many other JS MVC frameworks,
Angular v1.1.0+ requires no tweaks to work within a strict CSP. It
just works, out of the box!
However, if you're using an older version of Angular between v1.0.1
and v1.1.0, you'll need tell Angular to run in a "content security
mode". This is done by including the ngCsp directive alongside ngApp:
<html data-ng-app data-ng-csp>
You can find the whole article here.

Related

MSGraph token error: "Application (...) was not found in the directory"

I'm trying to implement the Microsoft Graph for OneDrive access in PHP. I'm going by this documentation.
I've registered my app at https://apps.dev.microsoft.com/ and got an App ID and a secret back.
Now, for the token flow, I'm emitting the following link:
$LogonLink =
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?".
http_build_query(array(
'client_id' => $MSGraphAppID,
'response_type' => 'code',
'redirect_uri' => 'http://www.example.com/ongraph.php',
'response_mode' => 'query',
'scope' => 'offline_access Files.ReadWrite'));
The logon and permissions UI works as expected, my app's name and icon comes up. So the App ID is recognized. Now, ongraph.php gets control with a code, and I emit the following POST:
$cu = curl_init('https://login.microsoftonline.com/common/oauth2/v2.0/token');
$Form = array(
'client_id' => $MSGraphAppID,
'scope' => 'offline_access Files.ReadWrite',
'code' => $_GET['Code'],
'redirect_uri' => 'http://www.example.com/ongraph.php',
'grant_type' => 'authorization_code',
'client_secret' => $MSGraphSecret
);
curl_setopt_array($cu, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($Form)));
$r = curl_exec($cu);
I'm getting back error 400 and the following response:
{
"error": "unauthorized_client",
"error_description": "AADSTS70001: Application with identifier '0000000048205436' was not found in the directory 9188040d-6c67-4c5b-b112-36a304b66dad\r\nTrace ID: b0f7cba6-42b3-4f37-8438-5e8566ba1300\r\nCorrelation ID: ca51b601-86cb-4801-a330-354ffd3c715f\r\nTimestamp: 2017-11-08 17:12:33Z",
"error_codes": [70001],
"timestamp": "2017-11-08 17:12:33Z",
"trace_id": "b0f7cba6-42b3-4f37-8438-5e8566ba1300",
"correlation_id": "ca51b601-86cb-4801-a330-354ffd3c715f"
}
What am I missing here, please? I've tried creating a brand new app registration, same result.
EDIT: that's what the app registration site looks like:
The problem here is that you've registered your app as a Live SDK application:
For the v2 Endpoint (and Microsoft Graph), you need to register this as a Converged application:
The https://apps.dev.microsoft.com site handles both application types.

CakePHP 2 JWT Auth Plugin not receiving header in CakeRequest

I am using the CakePHP plugin (https://github.com/t73biz/cakephp2-jwt-auth). My CakePHP app is version 2.6.2. I have added this to my Auth Component in the AppController.
'JwtAuth.JwtToken' => array(
'fields' => array(
'username' => 'email',
'password' => 'password',
'token' => '_token'
),
'parameter' => '_token',
'contain' => array(
'Organization'
),
'scope' => array(
'User.status' => 'A',
//'User.frozen' => 0,
'User.locked_out' => 0,
'Organization.status' => 'A',
//'User.failed_sign_ins < 4'
),
'header' => 'X_JSON_WEB_TOKEN',
'pepper' => 'pepper' // Says pepper because I do not want to show the pepper key I used for my code
),
I know that the plugin runs because I add a die statement in the getUser function in the plugin and it shows up when I do the API request.
public function getUser(CakeRequest $request) {
$token = $this->_getToken($request);
if ($token) {
return $this->_findUser($token);
}
return false;
}
This is the function that is part of the JwtTokenAuthenticate.php in the Component directory in the Controller directory of the plugin. When I debug $request, I do not get the header as part of the request. I am using Postman for testing the API and Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7ImlkIjoiMTIiLCJzdWIiOiIxMiJ9LCJpYXQiOjE0ODcyNTUxMjYsImV4cCI6MTQ4NzI1ODcyNn0.5hoyXltPmEXIA3eVtJnnn3Dor2lhviej31eZNbaMbow
If I understand you correctly you're trying to pass auth token via Authorization header? But according to the plugin docs you should be doing it either via X_JSON_WEB_TOKEN header or _token query param.
Please add below code inside your .htaccess file, it will work
<IfModule mod_rewrite.c>
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>

cakephp How to change the X-Mailer

Using cakePHP version 2.3.8,
I am trying to change the X-Mailer: CakePHP Email into X-Mailer: PHP mail.
I have tried this in the controller and in the /app/Config/email.php in my 'default' settings.
'X-Mailer' => 'PHP mail',
But not able to get it changed, really frustrating.
You need to use addHeaders() to change it prior to sending the email.
As done here, for example:
https://github.com/dereuromark/tools/blob/master/Lib/EmailLib.php#L638
In this case - extending the core class - one can use Configure to automatically populate the X-Mailer via configs. But you can also do it inline for each email sending functionality.
Basically, on your CakeEmail object:
$CakeEmail = new CakeEmail();
$CakeEmail->addHeaders(array('X-Mailer' => 'My custom X-Mailer'));
If set manually Cake will not add his default value 'CakePHP Email'.
CakePHP 2,
Instead of setting it every time from a controller as suggested by #mark,
you can set in the EmailConfig class, located in Config/email.php:
public $default = array(
'transport' => '...',
'from' => '...',
'emailFormat' => 'both',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'headers'=>array('X-Mailer'=>'Your App Name'),
);
Will send an email with these headers:
To: .....
X-Mailer: Your App Name
Date: ....

Resize the images on the client side in Drupal 7?

Is there any module which integrate with the image/file field module that allows me to resize the images on the client side in Drupal 7? My client has very big images and I prefer that he resizes them on the client side....
Thanks!
Install Plupload module for Drupal
Edit the file plupload.module
Add 'resize' => array('width' => '320', 'height' => '240', 'quality' => '90'), to #plupload_settings.
Example:
$element['filefield_plupload']['pud'] = array(
'#type' => 'plupload',
'#title' => t('Select one or more files to upload'),
// Even though filefield does validation on submit, this is required for
// client side validation as well as proper file munging during upload.
'#upload_validators' => $element['#upload_validators'],
'#plupload_settings' => array(
'cardinality' => $field['cardinality'],
'resize' => array('width' => '320', 'height' => '240', 'quality' => '90'),
Ref: Drupal Issue: plupload client side resize option
Plupload has that functionality:
Allows you to upload files using HTML5 Gears, Silverlight, Flash, BrowserPlus or normal forms, providing some unique features such as upload progress, image resizing and chunked uploads.
Handily enough a Plupload module for Drupal already exists.

CakePHP Test Controller add edit

I am testing controllers add method (CakePHP2.0).
But the issue is that what ever data I send as post gets added to my default databases and not my test database.
Heres the code.
public function testAdd() {
$data = array(
'Post' => array(
'title' => 'Fourth Post Title',
'body' => 'Fourth Post Body',
'created' => '2012-01-22 21:31:52'
)
);
$result = $this->testAction('/posts/add', array('data' => $data, 'method', 'post'));
debug($result);
}
What I was actually expecting that the posted data togo into test DB that i configured in database.php and not in default DB.
There are a few requirements for tests to occur in CakePHP 2.0 that you may want to double check:
Ensure that your Database Config setting in config/database.php is setup as
public $test = array(
//Sample Test Settings Here
);
If you running tests from the web interface, debug must be set to at least 1 or else the tests will not run.
You can also validate that PHPUnit and your test database connection is working properly by running CakePHP core tests by navigating to: http://localhost/your_app/test.php and "AllConfigure" test.
All information from the CakePHP Book: http://book.cakephp.org/2.0/en/development/testing.html

Resources