PHPMailer with Gmail API auth works with unlimited scope, does not work with limited scope - phpmailer

I am trying to set up a application that will use PHPMailer with Gmail API functionality.
I have written my program and tested it and it worked great. The last step for me was to see if I could make the system a bit more secure and follow best practices by only requesting the scope that is needed.
This version of the code works.
$params = [
'clientId' => $clientKeys['clientID'],
'clientSecret' => $clientKeys['secretKey'],
'redirectUri' => $oauth2redirectURI,
'accessType' => 'offline'
];
$provider = new Google($params);
$options = [
'scope' => [
'https://mail.google.com/'
]
];
However, when I reduce the scope to this:
$params = [
'clientId' => $clientKeys['clientID'],
'clientSecret' => $clientKeys['secretKey'],
'redirectUri' => $oauth2redirectURI,
'accessType' => 'offline'
];
$provider = new Google($params);
$options = [
'scope' => [
'https://www.googleapis.com/auth/gmail.send'
]
];
The initial authorization works to get the refresh token and such.
However, PHPMailer no longer is able to send any messages as that user. Instead when attempting to send email messages, PHPMailer throws the following error:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting1

Related

How to check if Google Client_ID and Client_Secret Valid or not

I am making a module in Node.js where the user can make their own Livestream Bot for youtube. The module requires the user to provide their client id and client secret of their application. But how do I check if the client id and client secret they entered is valid and if it isn't throw an appropriate error. Is there an endpoint to do that?
There is no way of validating the client id or the client secret. The only way is to try them and if they dont work then display an error to the user.
You could maybe come up with a regex test for the client_id but i wouldn't recommend it as the format has changed several times over the years which would cause problems in your application.
I would just say try to use it if it fails give them an error message.
Note: I hope you are not prompting people to give you their client id and client secret as its against googles TOS for people to shire their client credentials if you are enc urging people to give you their credentials you are telling them to break the TOS.
Valid Code:
function ProjectValid($PostArray){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://oauth2.googleapis.com/token',
CURLOPT_POST => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
],
CURLOPT_POSTFIELDS => http_build_query([
'code' => md5(time()),
'client_id' => $PostArray['ClientID'],
'client_secret' => $PostArray['ClientSecret'],
'redirect_uri' => $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER["HTTP_HOST"].'/'.$URL,
'grant_type' => 'authorization_code'
]),
));
$Response = curl_exec($curl);
curl_close($curl);
if($Array=json_decode($Response,true) and isset($Array['error']) and $Array['error'] == 'invalid_grant'){
return true;
}else{
return false;
}
}

MS ToDo API is giving error for few users in Taskfolders Endpoint

MS ToDo API is giving error for few users when using following endpoint :
https://outlook.office.com/api/v2.0/me/taskfolders
Here is the piece of code :
$accessToken = "eyJ0eXAiOiJKV1QiLCJub25jZSI6I*******sYVg0X0";
$url = "https://outlook.office.com/api/v2.0/me/taskfolders";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$accessToken)
));
$output=curl_exec($ch);
Here is the error which I am getting :
stdClass Object (
[error] => stdClass Object
(
[code] => ErrorItemNotFound
[message] => The specified object was not found in the store.
)
)
As per the documentation of MS, we should get the list of default folder.
Here is the MS documentation link
That means, I should get default folder list in response and which I am getting in case of other users.
I checked the token validity, it is alive.
Any help is appreciated and thank you in advance.
In case if you're talking about shared tasks then it's not supported at this point. I remember an user voice on this - https://microsoftgraph.uservoice.com/forums/920506-microsoft-graph-feature-requests/suggestions/40295248-get-shared-task-folders-using-graph-api. You may want to vote for it, so that Microsoft can consider for it

How do I retrieve Stripe Session ID for a connected account?

I'm using Stripe checkout. I create the session and process the charge. On success, I have the session id. I want to retrieve the session object for the connected account. (This works fine for a charge to my standard account, but fails when for retrieving the session for the connected account).
For reference, here's the PHP code for creating the session before the charge:
\Stripe\Stripe::setApiKey($Skey);
$session = \Stripe\Checkout\Session::create([
'customer_email' => $Email,
'payment_method_types' => ['card'],
'line_items' => $itms,
'payment_intent_data' => [
'description' => $Web_Short_Name . '*' .$Transaction_ID,
'application_fee_amount' => $Fee,
'metadata' => ['Transaction_ID' => $Transaction_ID],
],
'success_url' => 'https://[myweb]/success.php?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'https://[myweb]/cart.php',
],
['stripe_account' => $Stripe_Account] );
}
FIRST ATTEMPT:
$s = $_GET['session_id'];
$stripe = new \Stripe\StripeClient(
['api_key' => '[my secret key'],
['stripe_account' => '[connected account]']
);
$s2=$stripe->checkout->sessions->retrieve($s,[]);
SECOND ATTEMPT:
$s = $_GET['session_id'];
\Stripe\Stripe::setApiKey('[my secret key]');
$stripe = new \Stripe\StripeClient(
'[my secret key]'
);
$s2=$stripe->checkout->sessions->retrieve($s,[]);
Thanks in advance!
Bob
(I've used StackOverflow as a resource for years...but this is my first post).
For connected accounts, you can fill the second parameter of the retrieve function, just like you did when creating the session:
\Stripe\Stripe::setApiKey('<your API key>');
$session = \Stripe\Checkout\Session::retrieve('<the session id>', [
'stripe_account' => '<the id of the connected Stripe account>'
]);
Got what I needed to work. Essentially I was looking for the PaymentIntent object when I got the call to my webhook from Stripe.
Snippet from my checkout webhook:
<?
require __DIR__ . '/vendor/autoload.php';
$payload = #file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
// Handle the event
\Stripe\Stripe::setApiKey({YOUR_SECRET_KEY});
$session_id = $event->data->object->id;
switch ($event->type) {
case 'checkout.session.completed':
$checkout = $event->data->object; // contains a \Stripe\PaymentIntent
// Then define and call a method to handle the successful payment intent.
//handleCheckoutSucceeded($checkout);
handleCheckout($db,$Transaction_ID,$session_id,'SUCCEEDED');
?>
\Stripe\Stripe::setApiKey('<your API key>');
$session = \Stripe\Checkout\Session::retrieve('<the session id>',[], [
'stripe_account' => '<the id of the connected Stripe account>'
]);
The second param array should be empty if no other parameters to pass and stripe_account is to be passed in the third array which is for the options array.

How to connect my LDAP with an existing ldap system in Laravel 5.4

I have to connect my ldap with an existing ldap system with the following conditions:
Domain used is 12.14.4.38
Username 0000001 and password 123456.
I've opened this link , but I still don't understand how to use it. This is my adldap.php code
<?php
return [
'connections' => [
'default' => [
'auto_connect' => true,
'connection' => Adldap\Connections\Ldap::class,
'schema' => Adldap\Schemas\ActiveDirectory::class,
'connection_settings' => [
'account_prefix' => env('ADLDAP_ACCOUNT_PREFIX', ''),
'account_suffix' => env('ADLDAP_ACCOUNT_SUFFIX', ''),
'domain_controllers' => explode(' ', env('ADLDAP_CONTROLLERS', '12.14.4.38')),
'port' => env('ADLDAP_PORT', 389),
'timeout' => env('ADLDAP_TIMEOUT', 5),
'base_dn' => env('ADLDAP_BASEDN', 'dc=12.14.4.38'),
'admin_account_suffix' => env('ADLDAP_ADMIN_ACCOUNT_SUFFIX', ''),
'admin_username' => env('ADLDAP_ADMIN_USERNAME', '0000001'),
'admin_password' => env('ADLDAP_ADMIN_PASSWORD', '123456'),
'follow_referrals' => false,
'use_ssl' => false,
'use_tls' => false,
],
],
],
];
// Create a new Adldap Provider instance.
$provider = new \Adldap\Connections\Provider(connections);
$ad = new \Adldap\Adldap(connections);
try {
// Connect to the provider you specified in your configuration.
$provider = $ad->connect('default');
// Connection was successful.
// We can now perform operations on the connection.
$user = $provider->search()->users()->find('0000001');
} catch (\Adldap\Auth\BindException $e) {
die("Can't connect / bind to the LDAP server! Error: $e");
}
You didn't specify a dn/path nor did you enter a path for the admin
This is how it normally looks
Search host: 12.14.4.38
basedn: "ou=Users,dc=DRiski,dc=com" <- I use your username as an example
admin:"cn=admin,ou=admins,dc=DRiski,dc=com"
password: just the regular password
What is with the weird cn, dc, ou stuff... that is like a path/folder in wich it needs to look to find the user (or users/groups in the case of base dn)...
base dn: specifies where to look for the users, in this case: in the folder users, on the server Driski.com
That is also how you specify your admin (tell the server where to find the thing).
Solved?
If not, try connecting to your ldap using ldapadmin (or another administrative tool) such that you can see how it works, and what path to enter...

Need to send a mail (bash or php script) using "sendmail" of a different linux server

Any means of specifying the IP address we want to connect to send that mail.?
Use PHP`s PEAR library class Mail. It's really straightforward.
Example:
(it's using remote SMTP server with SMTP AUTH, you don't need to use it)
<?php
require_once "Mail.php";
// mail data
$from = "You <sender#example.com>";
$to = "Her <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
// SMTP server info
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
// create mail headers
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject);
// create PEAR Mail object passing SMTP server info
$smtp = Mail::factory('smtp',
array (
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
// send the email
$mail = $smtp->send($to, $headers, $body);
// check the result
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

Resources