Perl CGI::Session, Multiple Sessions with same IDs, MySQL Driver - database

I have some problem with CGI::Session.
I try to create a new session with an existing session id passed with the cgi object. Normally the session should reuse the existing session in the database, but it doesn't. Instead it creates a new session database entry with the exact same session id.
Here are the relevant parts of my code:
CGI::Session->name("DCGISESSID");
$session = CGI::Session->new('driver:mysql', $cgi,
{
TableName=>'DSESSIONS',
IdColName=>'id',
DataColName=>'a_session',
Handle=>$dbh,
});
$sessioncookie = CGI::Cookie->new(-name=>'DCGISESSID', -value=>$session->id, -expires=>'+1h', -path=>'/');
The code works as long as I do not set the cookie name with the name() method and use the default value CGISESSID as cookiename. But for some reason, after changing it to DCGISESSID with CGI::Session->name("DCGISESSID"); it doesn't work.
Does someone got the same problem or has any advice for me?

Solved the problem. I configured the table false, that's why id wasn't a primary key too.

Related

Laravel 8 Fortify User UUID Login Problem

I am currently setting up a new project using Laravel 8. Out of the box, Laravel is configured to use auto-incrementing ID's for the user's ID. In the past I have overrode this by doing the following.
Updating the ID column in the user table creation migration to
$table->uuid('id');
$table->primary('id');
Adding the following trait
trait UsesUUID
{
protected static function bootUsesUUID()
{
static::creating(function ($model) {
$model->{$model->getKeyName()} = (string) Str::orderedUuid();
});
}
}
Adding the following to the user model file
use UsesUUID;
public $incrementing = false;
protected $primaryKey = 'id';
protected $keyType = 'uuid';
On this new project, I did the same as above. This seems to break the login functionality. When the email and password are entered and submitted, the form clears as though the page has been refreshed. Thing to note is there are no typical validation error messages returned as would be expected if the email and/or password is wrong.
To check that the right account is actually being found and the password is being checked properly, I added the following code to the FortifyServiceProvider boot method. The log file confirms that the user is found and the user object dump is correct too.
Fortify::authenticateUsing(function(Request $request) {
\Log::debug('running login flow...');
$user = User::where('email', $request->email)->first();
if ($user && Hash::check($request->password, $user->password)) {
\Log::debug('user found');
\Log::debug($user);
return $user;
}
\Log::debug('user not found');
return false;
});
Undoing the above changes to the user model fixes the login problem. However, it introduces a new problem that is the login will be successful but it wont be the right account that is logged in. For example, there are 3 accounts, I enter the credentials for the second or third account, but no matter what, the system will always login using the first account.
Anyone have any suggestions or ideas as to what I may be doing wrong, or if anyone has come across the same/similar issue and how you went about resolving it?
Thanks.
After digging around some more, I have found the solution.
Laravel 8 now stores sessions inside the sessions table in the database. The sessions table has got a user_id column that is a foreign key to the id column in the users table.
Looking at the migration file for the sessions table, I found that I had forgot to change the following the problem.
From
$table->foreignId('user_id')->nullable()->index();
To
$table->foreignUuid('user_id')->nullable()->index();
This is because Laravel 8 by default uses auto incrementing ID for user ID. Since I had modified the ID column to the users table to UUID, I had forgotten to update the reference in the sessions table too.

wordpress function with custom database

I am writing a WordPress plugin.
In one program, I capture the WordPress user id and write it to a file in a custom database.
Another program connects to the custom database, retrieves multiple rows having the user id:
$connection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$sql = "SELECT ...";
$prep = $connection->prepare($sql);
and tries to access the wordpress function after retrieving each record:
$user_info = get_userdata($user_id);
As soon as the get_userdata function is executed, the programs dies.
Do I need to connect to the wordpress database?
If so, how?
First of all, Why you have made database connection manually this way as you can able to use global variable "global $wpdb;" and then able to write query based on "$wpdb".
Second thing, you might declare that $user_id variable as global, so can access it globally in file or you should define in functions.php file and check for their availability.
Please let me know if any of this solution does not work for you with details.

Windows Azure The remote server returned an error: (404) Not Found

I'm following this tutorial which is about Table Storage Service. I'm using the emulator version of the tutorial which you can find at point 5 of paragraph "Configuring your connection string when using Cloud Services".
This is the code I pasted in the 'About' ActionResult:
public ActionResult About()
{
// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Create the CloudTable object that represents the "people" table.
CloudTable table = tableClient.GetTableReference("people");
// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter#contoso.com";
customer1.PhoneNumber = "425-555-0101";
// Create the TableOperation that inserts the customer entity.
TableOperation insertOperation = TableOperation.Insert(customer1);
// Execute the insert operation.
table.Execute(insertOperation);
return View();
}
At this line table.Execute(insertOperation); I get the following error message:
StorageException was unhandled by user code The remote server returned
an error: (404) Not Found.
The project template I used was "Windows Azure Cloud Service". The next window that popped up, I only added "ASP.NET MVC 4 Web Role".
Anyone any idea what is causing this error?
Does the people table exists in Storage area? (you can check from Azure management portal)
I came here after getting the same error(Not found when .replace), but my case was different, I actually had people table but the problem with my code is that I was updating the Row key value.
I think you can only update the other fields but not the partition keys or the row keys. Just thought of adding this to the answers in case someone else gets the same issue as me.

Get UserId from the Session Id and Server URL

I have a requirement, where I will be getting the Session Id and Server URL(parameters of a webservice method).
Say like this:
Session ID : 00D900000xxxxxx!ARUAQOb4VVoQR1UXlY_Hvuy1DdKdN6nSfnNJKYwPTF9R3tYuA2jzBsWXHIGDQUFL13iebnYSDKKC45H98TzVxxxxxxxxxx
Server URL :
https://ap1.salesforce.com/services/Soap/u/12.0/00D900000xxxxxx
Now i need to get the User Id(or any other user details) from these two.
Thanks in Advance!!
Nitin
You could use the SOAP API, which has a GetUserInfo method. Calling this method will return a GetUserInfoResult object, which will contain the user ID associated to the session.
Adam, GetUserInfo method requires existing connection object which we can create by calling method, so I applied the following code and now everything works fine!
ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint("https://login.salesforce.com/services/Soap/c/24.0/");
config.setServiceEndpoint("https://na14.salesforce.com/services/Soap/c/24.0/00DXXXXXXXXXXXX");
config.setSessionId("00DXXXXXXXXXXXX!1AQ4AQO980Fmu25SOFQxxOlQN8zAaHOlnfdk._rZU2Vkf_CV0HJREqKavMLaPg9jtA9N517MNHLdLeF.aVkoZtnk2eu7u.XNn");
connection = new EnterpriseConnection(config); GetUserInfoResult
userInfo = connection.getUserInfo();
I used the same server URL and sessionId that I have received from query parameter string.

Generating a key from ancestor path in Google App Engine

I have a model called Request. A request is created with a parent User like so:
request = Request(parentUserKey)
Now, the key_name for a User is that user's email, so when I create a new user, I do:
user = User(key_name = 'abc#gmail.com')
So what I want to do now is to create a key using Key.from_path for a Request, so I try:
requestKey = Key.from_path('User', 'abc#gmail.com', 'Request', 1)
I put 1 because I will use this key to fetch all Requests with ID higher than 1 (or any other arbitrary int) via the following:
requestQuery.filter("__key__ >", requestKey)
Then for testing purposes, I try to convert the key to a string via keyString = str(requestKey), but I get the following error:
Cannot string encode an incomplete key
What am I doing wrong?
To elaborate on what Guido wrote, doing all of this work to manually create a key probably isn't the best approach to solve your problem. Rather, if you store all of User's Request entities in User's entity group, you can simply and straight-forwardly retrieve them with an ancestor query.
First, to make all of the Request entities children of User, we're going to slightly change the way that you instantiate a Request object:
request = Request(parent=parentUser) # Where parentuser is a User already in the datastore
# Do whatever else you need to do to initialize this entity
request.put()
Now, to get all of the Request objects that belong to User, you just:
requests = Request.all().ancestor(parentUser).fetch(1000)
# Obviously, you want to intelligently manage the condition of having
# more that 1000 Requests using cursors if need be.
Having the ability to manually create Keys using all of the path info is great, but it is also often more work than is necessary.
Does this solve your use-case?
A key with a 0 id is not valid. Instead of that filter, use an ancestor query.

Resources