How to login with Panther on an external website? - symfony-panther

I use symfony/panther for web crawling. To access the page I want, I need to be authenticated and I don't know how to do it.
I have tried :
$client = Client::createChromeClient();
$client->start();
$crawler = $client->request('GET', 'http://website.com/login.php');
$form = $crawler->selectButton('Submit!')->form([
'email' => myEmail,
'pwd' => myPassword
]);
$client->submit($form);
But it doesn't work, my user isn't authenticated.
I think I need to retrieve a cookie and add it to my client but I don't know how to do it.
Can someone please give me the steps to do this?

Related

Lavarel Auth session lost after request

We try to authenticate a user manually via the builtin Auth functionality.
We don't retain a database as that's not needed for our purpose.
We followed this part of the docs: https://laravel.com/docs/9.x/authentication#authenticate-a-user-instance
We make a user on a certain request, we checked the Auth::check() method and I get a true, so it's working:
$user = User::make([
'name' => $userGoogle->name,
'email' => $userGoogle->email,
'token' => $userGoogle->token,
'refresh_token' => $userGoogle->refreshToken,
'employeeId' => $employeeId,
]);
Auth::login($user);
//request()->session()->invalidate(); //Needed?
//request()->session()->regenerateToken(); //Needed?
dd(Auth::check());
But, when loading another test page:
Route::get('test', function() {
dd(Auth::check());
});
This always returns false. I also tried with regenerating the session token as I suspect something is going wrong there.
But no avail. It just doesn't work. Or is a database mandatory? It's not stated in the docs, and the ::check() after Auth::login works perfectly.
Thanks

Guzzle 7 + Guzzle-cache-middleware

I'm using Guzzle 7 to grab content from an external API with basic authentication. It works fine. Now I'd like to integrate cache management. So i've tried to use this plugin: [Guzzle-cache-middleware][1] and I can't make it working correctly. I can get API response and my desired content but the cache directory is not populated.
I've searched all around the web but I can't figure this out. Could you please tell me how to solve this? Here is my code:
$userName = "xxxxxxx";
$password = "yyyyyyyyyyy";
require_once './vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
$stack = HandlerStack::create();
$cache = new CacheMiddleware();
$stack->push($cache, '/home/xxxx/xxxxx/guzzle2/cache');
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://api.xxxxx.com/xxx/',
"timeout" => 30.0,
]);
$json = $client->get('zzzzzz.json', [
'auth' => [
$userName,
$password
]
]);
var_dump($json->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
Output:
string(4) "MISS"
So API result is different from cache. But headers params (ETag and Last-Modified) are still unchanged and my cache folder is still blank.
Thank you for your help!

Laravel Sentinel Login with Webview App

I have a web application built with Laravel 5.1.
I have opened a POST route on frontend for webview apps.
This is my app login function:
$json = array();
$credentials = [
'email' => $request->email,
'password' => $request->password,
];
$error = $this->auth->login($credentials, false);
if (!$error)
$json['success'] = trans('user::messages.successfully logged in');
else
$json['error'] = $error;
echo json_encode($json);
However, when I redirect the mobile user to homepage after a successfull request, user is not logged in.
Interestingly, when I close and re-open the app, user is logged in.
Any ideas?
UPDATE: I have discovered that when I put something in Session with login, it doesn't show up at Session. But when I close the app and re-open, Session restored.
Have you tried comparing the cookies both for after logging in and restarting the app after login from the debugbar? You may not be sending necessary cookies.

Laravel - Oauth password grant

I find it difficult to wrap my head around the password grant authentication process.
What I'm trying to achieve: A mobile device sends the username and the password of a registered user to the API and gets the access token as a response in JSON form.
What I've achieved so far: The API receives the user credentials (username and password) and authenticates the user using Laravel's built-in Auth authentication system.
I know that at this point I should proceed to Oauth authentication. Once the credentials pass the Auth step, I have the user's username, password (specified in the users table) and I can get the client_id and client_secret from the oauth_clients table, but I'm not sure how to accomplish the rest. Do I need to make another POST request? If yes, then how do I do it from the controller?
I am actually implementing this right now. Let me show you some code.
This is my part of my login function:
// this does the process for getting the access token
$oauth = AuthorizationServer::performAccessTokenFlow();
// some hacks
$oauth = (array) $oauth;
// more hacks
$oauth = json_decode($oauth["\0*\0data"], true);
// checks if a token was actually generated
if(!in_array('bearer', $oauth))
{
// returns what was generated if the token is missing
return Responser::error(400, $oauth);
}
You would have to post additional data on your login other than the username and password of the user.
So your post request will contain:
username=the_username
password=the_password
grant_type=password
client_id=the_client_id
client_secret=the_client_secret
note that grant_type=password is constant.
Now you have to check the configurations of the package too found at app/config/packages/lucadegasperi/oauth2-server-laravel/oauth2.php:
You should have the following code:
'password' => array(
'class' => 'League\OAuth2\Server\Grant\Password',
'access_token_ttl' => 604800,
'callback' => function($username, $password){
$credentials = array(
// change this to username if username is the field on your database
'email' => $username,
'password' => $password,
);
$valid = Auth::validate($credentials);
if (!$valid) {
return false;
}
return Auth::getProvider()->retrieveByCredentials($credentials)->id;
}
),
And you are done.
update
the code above that generates the token is inside this function:
// allow user to login with username or email and password
$user_pass = array('username' => $username, 'password' => $password);
$email_pass = array('email' => $username, 'password' => $password);
// check if input is email and use $email_pass combination or else use $user_pass combination
$login = ($isEmail->passes() ? $email_pass : $user_pass);
// try to authenticate username & password
if (Auth::attempt($login))
{
// now you are authenticated here
// get the client id and secret from the database
// maybe use curl or some hacks
// login stuff and token generation
}
This is a bit of an old post, but if you are looking to do this without having multiple post requests, you could always just do something like:
//Get Oauth creds
$apiCreds = OauthClient::where('email', Input::get('email'))->first();
//Provide additional post data for password grant
Input::merge([
'grant_type' => 'password',
'client_id' => $apiCreds->id,
'client_secret' => $apiCreds->secret,
'username' => Input::get('email'),
'password' => Input::get('password'),
'scope' => 'all'
]);
//Login
return AuthorizationServer::performAccessTokenFlow();

how to get and use codeigniter cookies?

I am trying to use codeigniter cookies so that i can bookmark some topic , i don't know how to get cookies vale and use it to display bookmarked item in the user browser , it doesn't work when i try to display cokies value using print_r(); please help how to proceed. Thanks
function index(){
$this->load->helper('cookie');
$data['title'] = 'some bookmark';
$cookie = array(
'name' =>'mycookie',
'value' => 'some val',
'expire' => '86500',
'domain' => '.localhost',
'path' => '/',
'prefix' => 'something_'
);
set_cookie($cookie);
$this->load->view('bookmark', $data);
}
Browsers have trouble dealing with localhost (or .localhost) as the domain for cookie storage - they expect a domain to have at least two dots. Try setting the domain to FALSE (or don't set it) while testing.
If that still doesn't work, can you show us how you're retrieving the cookie data?

Resources