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

Add session checking middleware #21

Merged
merged 2 commits into from
May 18, 2021
Merged

Conversation

paulomarg
Copy link
Collaborator

@paulomarg paulomarg commented May 17, 2021

WHY are these changes introduced?

Most app requests will need to be authenticated when they interact with Shopify. In order to make it easy to ensure that a certain action is fully cleared to access the API, we should provide a middleware to run all of the checks.

WHAT is this pull request doing?

Adding a middleware that can be used as shopify.auth[:online/:offline] (defaulting to online), which will load the session from the current request (auth header / cookies) and check if it:

  • is valid (i.e. not expired, scopes haven't changed)
  • is for the shop of the actual request (mostly to avoid funny business by apps)

If the session is not valid, the middleware will:

  • redirect the user to login for a new OAuth, if the request is NOT a XHR
  • respond with a 401 with special headers (X-Shopify-API-Request-Failure-Reauthorize and X-Shopify-API-Request-Failure-Reauthorize-Url) indicating that the app should go to login.

The default client in the React app was also updated to wrap around the authenticatedFetch call with a function that looks for those headers in the response, and breaks out of the Admin to go to /login.

Checklist

  • I have added/updated tests for this change

@paulomarg paulomarg force-pushed the add_session_check_middleware branch from a8aa846 to 45ef62f Compare May 18, 2021 13:53
@paulomarg paulomarg marked this pull request as ready for review May 18, 2021 13:53
@paulomarg paulomarg requested a review from a team as a code owner May 18, 2021 13:53
@paulomarg paulomarg force-pushed the add_session_check_middleware branch from 45ef62f to 5fe080a Compare May 18, 2021 13:54
Copy link
Contributor

@MuhammadFarag MuhammadFarag left a comment

Choose a reason for hiding this comment

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

Only one thing with setting up the middleware. Awesome work, as usual 👏

@@ -111,7 +111,7 @@ function(Shopify\Auth\OAuthCookie $cookie) {
Route::post('/graphql', function (Request $request) {
$result = Utils::graphqlProxy($request->header(), $request->cookie(), $request->getContent());
return response($result->getDecodedBody())->withHeaders($result->getHeaders());
});
})->middleware('shopify.auth:online');
Copy link
Contributor

Choose a reason for hiding this comment

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

It is weird that we need to explicitly add the middlewear here even though we have added it to kernel. I don't think it should be necessary 🤔 . Maybe we have misconfigured something

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I actually did that on purpose - you can set up a middleware to run for a group of endpoints, but we don't want to authenticate every request, only those that are fired from the client side using authenticatedFetch, so I added it individually to the endpoints that actually need it. Devs can definitely create a separate route group and apply the middleware to it, but I felt like keeping all the endpoints in a single file makes it easier to understand.

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.

2 participants