Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Store state instead of session id in cookie #438

Merged
merged 3 commits into from
Aug 18, 2022

Conversation

mkevinosullivan
Copy link
Contributor

@mkevinosullivan mkevinosullivan commented Jul 29, 2022

WHY are these changes introduced?

Currently, when an app calls beginAuth we create a temporary session and store the session id in the cookie that will be passed throughout the OAuth process. In validateAuthCallback, we use the session id from the cookie to retrieve the state value, which we then compare to the state value we received from Shopify as part of the validation.

Fixes https://github.com/Shopify/first-party-library-planning/issues/388

WHAT is this pull request doing?

Rather than create a temporary session in order to store a session id in a cookie for the OAuth transaction, we can store the state in the cookie instead, that can be compared against the state provided by Shopify in the callback, and then create the session at that point.

Type of change

  • Patch: Bug (non-breaking change which fixes an issue)
  • Minor: New feature (non-breaking change which adds functionality)
  • Major: Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have added a changelog entry, prefixed by the type of change noted above
  • I have added/updated tests for this change
  • I have documented new APIs/updated the documentation for modified APIs (for public APIs)

Copy link
Contributor

@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.

Looking good, had a few comments!

src/auth/oauth/__tests__/oauth.test.ts Show resolved Hide resolved
src/auth/oauth/__tests__/oauth.test.ts Outdated Show resolved Hide resolved
src/auth/oauth/__tests__/oauth.test.ts Show resolved Hide resolved
src/auth/oauth/oauth.ts Outdated Show resolved Hide resolved
src/auth/oauth/oauth.ts Outdated Show resolved Hide resolved
src/auth/oauth/oauth.ts Outdated Show resolved Hide resolved
}

cookies.set(ShopifyOAuth.SESSION_COOKIE_NAME, currentSession.id, {
cookies.set(ShopifyOAuth.SESSION_COOKIE_NAME, session.id, {
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't tell from the diff, but is this only run when not embedded? We shouldn't even need to set the session in the cookie if it's embedded, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's run regardless of embedded/not-embedded ... which is the same as it does today.

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically, we could just NOT run this if Context.IS_EMBEDDED_APP, right? Right now we're just creating a cookie that expires immediately, so it doesn't really add value.

src/auth/oauth/oauth.ts Outdated Show resolved Hide resolved
src/auth/oauth/oauth.ts Outdated Show resolved Hide resolved
src/auth/oauth/oauth.ts Show resolved Hide resolved
Rather than create a temporary session in order to store a session id
in a cookie for the initial OAuth transaction, we can store the state
that can be compared against the state provided by Shopify in the
callback, and then create the session at that point instead.

Fixes Shopify/first-party-library-planning#388
Copy link
Contributor

@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.

LGTM! 🎩ed it and it worked on all combinations of online / offline tokens + embedded / non-embedded.

}

cookies.set(ShopifyOAuth.SESSION_COOKIE_NAME, currentSession.id, {
cookies.set(ShopifyOAuth.SESSION_COOKIE_NAME, session.id, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically, we could just NOT run this if Context.IS_EMBEDDED_APP, right? Right now we're just creating a cookie that expires immediately, so it doesn't really add value.

@mkevinosullivan mkevinosullivan merged commit 170996e into main Aug 18, 2022
@mkevinosullivan mkevinosullivan deleted the kos/save_state_to_cookie branch August 18, 2022 15:55
@shopify-shipit shopify-shipit bot temporarily deployed to production September 19, 2022 19:28 Inactive
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants