Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1. Addd scope, expires, access token, and online access info to session storage #11

Merged
merged 1 commit into from
May 11, 2021

Conversation

MuhammadFarag
Copy link
Contributor

Add missing fields to session storage. These fields are required for the callback flow and online session tokens

@MuhammadFarag MuhammadFarag requested a review from a team as a code owner May 5, 2021 15:08
$dbSession->expires_at = $session->getExpires();
$dbSession->scope = $session->getScope();
if(!empty($session->getOnlineAccessInfo())){
$dbSession->online_access_info = serialize($session->getOnlineAccessInfo());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bitter feeling about this...
Other options:

  • Serialize as JSON and handle potential exception
  • Create a column for each field in the online access info
  • Go the other way around and just serialize the entire Session object against the key

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disadvantage of serializing it as JSON is that we'd have to rebuild the object when coming back (which could be a static method in the info class). I agree that the serialize call is less than ideal, but this is a shallow value object, so it's not necessarily a big problem.

If we want to do things 'the right way', we'd probably go with option 2, which is more work but gives us fully structured data that can be queried for the purposes of an app - like say an app that needs to know which user is doing things in a context that does not use one of the lib's sessions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spread it into separate columns 👍

@MuhammadFarag MuhammadFarag changed the title Addd scope, expires, access token, and online access info to session storage 1. Addd scope, expires, access token, and online access info to session storage May 6, 2021
Copy link
Contributor

@Paulinakhew Paulinakhew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a linter 🤔😂 other than that this PR makes sense! I don't really have an opinion on the serialize thing other than the fact that we should do either option 1 or 3. I'm leaning more towards option 1, for no particular reason

Comment on lines 25 to 37
if($dbSession->expires_at){
$session->setExpires($dbSession->expires_at);
}
if($dbSession->access_token){
$session->setAccessToken($dbSession->access_token);
}
if($dbSession->scope){
$session->setScope($dbSession->scope);
}
if($dbSession->online_access_info){
$session->setOnlineAccessInfo(unserialize($dbSession->online_access_info));
}
return $session;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if($dbSession->expires_at){
$session->setExpires($dbSession->expires_at);
}
if($dbSession->access_token){
$session->setAccessToken($dbSession->access_token);
}
if($dbSession->scope){
$session->setScope($dbSession->scope);
}
if($dbSession->online_access_info){
$session->setOnlineAccessInfo(unserialize($dbSession->online_access_info));
}
return $session;
if ($dbSession->expires_at){
$session->setExpires($dbSession->expires_at);
}
if ($dbSession->access_token){
$session->setAccessToken($dbSession->access_token);
}
if ($dbSession->scope){
$session->setScope($dbSession->scope);
}
if ($dbSession->online_access_info){
$session->setOnlineAccessInfo(unserialize($dbSession->online_access_info));
}
return $session;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tells me, we need to merge a linting PR prior to these PRs

}
return null;
}

public function storeSession(Session $session): bool
{
$dbSession = new \App\Models\Session();
$dbSession =\App\Models\Session::where('session_id', $session->getId())->first();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$dbSession =\App\Models\Session::where('session_id', $session->getId())->first();
$dbSession = \App\Models\Session::where('session_id', $session->getId())->first();

}
return null;
}

public function storeSession(Session $session): bool
{
$dbSession = new \App\Models\Session();
$dbSession =\App\Models\Session::where('session_id', $session->getId())->first();
if(!$dbSession){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!$dbSession){
if (!$dbSession){

$dbSession->access_token = $session->getAccessToken();
$dbSession->expires_at = $session->getExpires();
$dbSession->scope = $session->getScope();
if(!empty($session->getOnlineAccessInfo())){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!empty($session->getOnlineAccessInfo())){
if (!empty($session->getOnlineAccessInfo())){

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Paulinakhew 💯

$dbSession->expires_at = $session->getExpires();
$dbSession->scope = $session->getScope();
if(!empty($session->getOnlineAccessInfo())){
$dbSession->online_access_info = serialize($session->getOnlineAccessInfo());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disadvantage of serializing it as JSON is that we'd have to rebuild the object when coming back (which could be a static method in the info class). I agree that the serialize call is less than ideal, but this is a shallow value object, so it's not necessarily a big problem.

If we want to do things 'the right way', we'd probably go with option 2, which is more work but gives us fully structured data that can be queried for the purposes of an app - like say an app that needs to know which user is doing things in a context that does not use one of the lib's sessions.

@@ -42,12 +60,11 @@ public function testLoadSessionReturnSessionIfItExists()
);
}

public function testStoreSessionReturnFalseIfOperationFails()
public function testStoreShouldUpdateSession()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this comment elsewhere, seems like that was a branch off of this :) - we should probably keep the old test around as it's useful, and trigger a different storage failure (like an invalid type for a column?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just didn't know how to trigger that failure in the tests 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅 won't it fail if we give a column some bogus value? I guess the problem here is that strict typing makes our code too robust to fail 😄 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly 🕺

Copy link
Collaborator

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

$dbSession->expires_at = $session->getExpires();
$dbSession->scope = $session->getScope();
if (!empty($session->getOnlineAccessInfo())) {
$dbSession->user_id = $session->getOnlineAccessInfo()->getId();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit more verbose, but I like this approach better. As a separate PR, we may want to consider adding a method to Shopify\Auth\Session that takes in an object / associative array and pops out a session object so partners don't need to go through all of this (i.e. this could all be done as Session::createFromObject($dbSession).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we can generalize that. Because we don't know the underlying store they are using an how is it being encoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants