How to create SSO URL in paperlesspipeline - laravel

i am trying to create SSO URL for the paperlesspipeline, so it no needs to credential for login
i have to use laravel 5.8 and it's GuzzleHttp package
$timestamp = date(DateTime::ISO8601);
$userID = '*****';
$url = "https://dev.paperlesspipeline.com/sso/signin-v1/";
$hash = hash_hmac('SHA256', $userID.$timestamp,secret key,false);
$client = new \GuzzleHttp\Client(['http_errors' => false]);
$options = [
'form_params' => [
"USERID" => $userID,
"timestamp" => $timestamp,
"hash" => $hash
]
];
$response = $client->post($url, $options);
I am not get sso url, can you halp me what wrong.

You can try this
$response = $client->request('POST', $url, $options);

Related

Laravel 7 cognito used in web and apps

I want to build laravel 7 with aws cognito and use this package aws-sdk-php-laravel.
This project will be made for the web & apps.
so how the laravel handle login from apps then apps get API from web with same token from cognito?
This is my first time use cognito, usually use auth-jwt.
After many tries without package
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CognitoController extends Controller
{
private $key;
private $secret;
private $region;
private $version;
private $client_id;
private $client_secret;
private $user_pool_id;
private $aws;
private $client;
public function __construct()
{
$this->key = 'xxxxxx';
$this->secret = 'xxxxxx';
$this->region = 'xxxxxx';
$this->version = 'latest';
$this->client_id = 'xxxxxx';
$this->client_secret = 'xxxxxx';
$this->user_pool_id = 'xxxxxx';
$config = [
'credentials' => [
'key' => $this->key,
'secret' => $this->secret,
],
'region' => $this->region,
'version' => $this->version,
'app_client_id' => $this->client_id,
'app_client_secret' => $this->client_secret,
'user_pool_id' => $this->user_pool_id,
];
$this->aws = new \Aws\Sdk($config);
$this->client = $this->aws->createCognitoIdentityProvider();
}
public function login()
{
$username = 'guest';
$password = 'password';
$result = $this->client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
'AuthParameters' => [
'USERNAME' => $username,
'PASSWORD' => $password,
'SECRET_HASH' => $this->secretHash($username),
],
'ClientId' => $this->client_id,
'UserPoolId' => $this->user_pool_id,
]);
$access_token = $result->get('AuthenticationResult')['AccessToken'];
echo '<pre>';
print_r( $result );
echo '<br/>';
print_r( $access_token );
echo '</pre>';
}
public function register()
{
$username = 'guest';
$password = 'password';
$email = 'guest#email.com';
$phone_number = '+819000000000';
$result = $this->client->signUp([
'ClientId' => $this->client_id,
'Password' => $password,
'SecretHash' => $this->secretHash($username),
'UserAttributes' => [
[
'Name' => 'phone_number',
'Value' => $phone_number,
],
[
'Name' => 'email',
'Value' => $email,
]
],
'Username' => $username
]);
echo '<pre>';
print_r( $result );
echo '</pre>';
}
public function register_confirm()
{
$username = 'guest';
$confirmationCode = '123456'; // from email verification code
$result = $this->client->confirmSignUp([
'ClientId' => $this->client_id,
'Username' => $username,
'SecretHash' => $this->secretHash($username),
'ConfirmationCode' => $confirmationCode,
]);
echo '<pre>';
print_r( $result );
echo '</pre>';
}
public function list()
{
$search = ['UserPoolId' => $this->user_pool_id];
$users = $this->client->listUsers($search)->toArray();
echo '<pre>';
print_r( $users );
echo '</pre>';
}
private function secretHash($username) {
$clientId = $this->client_id;
$clientSecret = $this->client_secret;
$s = hash_hmac('sha256', $username . $clientId, $clientSecret, true);
return base64_encode($s);
}
}
token from auth cognito is it the same as the token when use JWTAuth?
Laravel Socialite doesn't have a cognito platform yet
You can configure AWS Cognito for OAuth and use one of the Laravel Oauth plugin to connect and authenticate with Cognito. I hope you will be storing user credentials in AWS Cognito User Pool.
Complete documentation (High Level) of AWS Cognito - https://www.slideshare.net/awsugkochi/acdkochi19-enterprise-grade-security-for-web-and-mobile-applications-on-aws
Documentation of AWS Cognito architecture for OAuth configuration - https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-user-pool-oauth-2-0-grants/
Documentation of Laravel Socialite to connect with AWS Cognito over OAuth and get authentication.

Foreach Loop not running in Laravel

I want to send a request to external API using guzzle, but not foreach is not running.
public function recursub() {
$usersCheck = User::where('trialExpires', '<=', Carbon::now());
//Get all Check User
foreach ($usersCheck as $user) {
dd('Hello');
$url = 'https://api.##############';
$client = new Client();
$response = $client->request('GET', $url, [
'headers' => [
'Authorization' => 'Bearer '.'#########################',
'Content-Type' => 'application/json'
],
'form_params' => [
'authorization_code' => $user->authorization_code,
'customer' => $user->email,
//'plan' => '#######################',
]
]);
It worked if I hardcode the value into the form paramas.
public function recursub() {
$usersCheck = User::where('trialExpires', '<=', Carbon::now())->get();
//Get all Check User
foreach ($usersCheck as $user) {
dd('Hello');
$url = 'https://api.##############';
$client = new Client();
$response = $client->request('GET', $url, [
'headers' => [
'Authorization' => 'Bearer '.'#########################',
'Content-Type' => 'application/json'
],
'form_params' => [
'authorization_code' => $user->authorization_code,
'customer' => $user->email,
//'plan' => '#######################',
]
]);
get() method is used to get data in array, so now you have to add get() method in the last of the query. Here is the code below:
$usersCheck = User::where('trialExpires', '<=', Carbon::now())->get();
get() method is used for getting multiple records and first is used for single record.

How to generate access_token without username and password

I am trying to make an API with Passport. If a user tries to login or signs up with Socialite, find user than generate access_token then redirect to the frontend with access_token in URL parameters.
I tried to register and login than generate access_token with user email and the default password, which is not suitable for security.
try {
$serviceUser = Socialite::driver($service)->stateless()->user();
} catch (\Exception $e) {
return redirect(config('app.client_url').'/auth/social-callback?error=Unable to login using '.$service.'. Please try again'.'&origin=login');
}
$email = $serviceUser->getEmail();
$name = $serviceUser->getName();
$user = $this->getExistingUser($serviceUser, $email, $service);
$newUser = false;
if (!$user) {
$newUser = true;
$user = new User;
$user->name = $name;
$user->email = $email;
$user->username = Str::random(10);
if ($service === 'facebook') {
$user->image = $serviceUser->avatar;
}
$user->verify = true;
$user->save();
}
if ($this->needsToCreateSocial($user, $service)) {
Social::create([
'user_id' => $user->id,
'social_id' => $serviceUser->getId(),
'service' => $service
]);
}
$http = new Client;
$response = $http->post(config('app.url').'/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => '2',
'client_secret' => 'oBKWxgF2fDvrxwA05ciapwy4JYKaHxzhGzr6D24X',
'username' => $email,
'password' => 'gebdandi',
'scope' => '',
],
]);
$body = json_decode((string) $response->getBody(), true);
$accessToken = $body['access_token'];
return redirect(config('app.client_url').'/social-callback?token='.$accessToken.'&origin='.($newUser ? 'register' : 'login'));
I can't find any solution in the documentation.
after reading all documentation i found solution in this documentation
i did like this
edit authserviceprovider like this
public function boot()
{
$this->registerPolicies();
Passport::routes();
Passport::personalAccessClientId(1);
Passport::tokensExpireIn(now()->addDays(15));
Passport::refreshTokensExpireIn(now()->addDays(30));
}
added code in controller like this
$accessToken = $user->createToken('access_token')->accessToken;
thanks for laravel Team for provide good documentation

Guzzle HTTP POST request in laravel

how to pass path parameter in guzzle HTTP Post request. I am having url like this - http://base_url/v1/rack/{id}/books
in my url {id} is the path parameter.
$addLibraryUrl = $base_url."v1/rack/{id}/book";
$headers["id"] = $id;
$requestContent['json'] = $data;
$client = new Client();
$response = $client->post($addLibraryUrl, [
"headers" => $headers,
"json" => json_encode($data)
]);
I dont think the latest guzzle uses URI templates, but the functionality to do the parameter replacing is still there:
$addLibraryUrl = \GuzzleHttp\uri_template($base_url. "v1/rack/{id}/book" , [
'id' => $id,
]);
Also you can just put the id into the URI yourself very easily.
$addLibraryUrl = $base_url."v1/rack/{$id}/book";
example:
blah.com/v1/rack/5/book
I solved my issue in this way:
$id = your_rack_id_value;
$client = new Client([ 'base_uri' => $base_url, ]);
$uri = 'v1/rack/.$id.'/book';
$requestEncodedData = json_encode($data);
$response = $client->post($uri, [
'body' => $requestEncodedData,
'headers' => [
'Content-Type' => 'application/json',
]
]);
If you sent form params then you need to sent them as post params like :
// Initialize Guzzle client
$client = new GuzzleHttp\Client(['headers'=> 'Some headers']);
// Create a POST request
$response = $client->request(
'POST',
'http://yoururl.com',
[
'form_params' => [
'key1' => 'value1',
'key2' => 'value2'
]
]
);
Or like in your case change 'json' to form_params:
$addLibraryUrl = $base_url."v1/rack/{id}/book";
$headers["id"] = $id;
$requestContent['json'] = $data;
$client = new Client();
$response = $client->post($addLibraryUrl, [
"headers" => $headers,
"form_params" => json_encode($data)
]);

laravel guzzle httpsocket parameters for ssl verify peer & host

I have the following code in CakePHP. I need the same code in laravel using guzzle
$url = "https://xyz?";
$query ='first_name='. $data->FirstName .'&gender=""'. '&home_phone='. $data->HomePhone.'&ip_address='. $data->IPAddress.'&last_name='. $data->LastName.'&user_defined_url='. $result;
$HttpSocket = new HttpSocket(array('ssl_verify_peer' => false, 'ssl_verify_host' => false));
$post_response = $HttpSocket->get($url,$query);
$response = explode('&',$post_response->body);
I have converted it in laravel using guzzle but doesnt work. Following my code that Ive converted in laravel:
$client = new Client(['verify' => false ]);
$post_response = $client->get($url, $query);
$response = explode('&',$post_response->body);
Note: use GuzzleHttp\Client; is written at the top of file.
Thanks in advance for the help!
Can you try like this,
$client = new Client();
$post_response = $client->request('GET', $url, [
'verify' => false,
'form_params' => [
'first_name' => $data->FirstName,
'gender' => "",
'home_phone' => $data->HomePhone,
'ip_address' => $data->IPAddress,
'last_name' => $data->LastName,
'user_defined_url' => $result
]
]);
$response = explode('&',$post_response->body);
Instead of form_params you can use query for sending the parameters as query string.

Resources