diff --git a/packages/koa-shopify-auth/CHANGELOG.md b/packages/koa-shopify-auth/CHANGELOG.md index 8de88e3abb..dba8f11f67 100644 --- a/packages/koa-shopify-auth/CHANGELOG.md +++ b/packages/koa-shopify-auth/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [Unreleased] + +- Updated redirect script to use App Bridge [1242](https://github.com/Shopify/quilt/pull/1242) + ## 3.1.37 - 2019-09-23 ### Fixed diff --git a/packages/koa-shopify-auth/src/auth/create-enable-cookies-redirect.ts b/packages/koa-shopify-auth/src/auth/create-enable-cookies-redirect.ts index 5cb2a5dc9a..694b9bf0ee 100644 --- a/packages/koa-shopify-auth/src/auth/create-enable-cookies-redirect.ts +++ b/packages/koa-shopify-auth/src/auth/create-enable-cookies-redirect.ts @@ -4,8 +4,11 @@ import createTopLevelRedirect from './create-top-level-redirect'; import {TEST_COOKIE_NAME} from './index'; -export default function createEnableCookiesRedirect(path: string) { - const redirect = createTopLevelRedirect(path); +export default function createEnableCookiesRedirect( + apiKey: string, + path: string, +) { + const redirect = createTopLevelRedirect(apiKey, path); return function topLevelOAuthRedirect(ctx: Context) { // This is to avoid a redirect loop if the app doesn't use verifyRequest or set the test cookie elsewhere. diff --git a/packages/koa-shopify-auth/src/auth/create-top-level-oauth-redirect.ts b/packages/koa-shopify-auth/src/auth/create-top-level-oauth-redirect.ts index 4405fd546c..1fb75ed157 100644 --- a/packages/koa-shopify-auth/src/auth/create-top-level-oauth-redirect.ts +++ b/packages/koa-shopify-auth/src/auth/create-top-level-oauth-redirect.ts @@ -4,8 +4,11 @@ import createTopLevelRedirect from './create-top-level-redirect'; import {TOP_LEVEL_OAUTH_COOKIE_NAME} from './index'; -export default function createTopLevelOAuthRedirect(path: string) { - const redirect = createTopLevelRedirect(path); +export default function createTopLevelOAuthRedirect( + apiKey: string, + path: string, +) { + const redirect = createTopLevelRedirect(apiKey, path); return function topLevelOAuthRedirect(ctx: Context) { ctx.cookies.set(TOP_LEVEL_OAUTH_COOKIE_NAME, '1'); diff --git a/packages/koa-shopify-auth/src/auth/create-top-level-redirect.ts b/packages/koa-shopify-auth/src/auth/create-top-level-redirect.ts index a0353fe5a6..e48c58a105 100644 --- a/packages/koa-shopify-auth/src/auth/create-top-level-redirect.ts +++ b/packages/koa-shopify-auth/src/auth/create-top-level-redirect.ts @@ -4,7 +4,7 @@ import {Context} from 'koa'; import redirectionPage from './redirection-page'; -export default function createTopLevelRedirect(path: string) { +export default function createTopLevelRedirect(apiKey: string, path: string) { return function topLevelRedirect(ctx: Context) { const {host, query} = ctx; const {shop} = query; @@ -13,8 +13,9 @@ export default function createTopLevelRedirect(path: string) { const queryString = querystring.stringify(params); ctx.body = redirectionPage({ - origin: `https://${shop}`, + origin: shop, redirectTo: `https://${host}${path}?${queryString}`, + apiKey, }); }; } diff --git a/packages/koa-shopify-auth/src/auth/index.ts b/packages/koa-shopify-auth/src/auth/index.ts index 894ec59031..9573b0adb1 100644 --- a/packages/koa-shopify-auth/src/auth/index.ts +++ b/packages/koa-shopify-auth/src/auth/index.ts @@ -40,11 +40,17 @@ export default function createShopifyAuth(options: OAuthStartOptions) { const oAuthCallback = createOAuthCallback(config); const inlineOAuthPath = `${prefix}/auth/inline`; - const topLevelOAuthRedirect = createTopLevelOAuthRedirect(inlineOAuthPath); + const topLevelOAuthRedirect = createTopLevelOAuthRedirect( + config.apiKey, + inlineOAuthPath, + ); const enableCookiesPath = `${oAuthStartPath}/enable_cookies`; const enableCookies = createEnableCookies(config); - const enableCookiesRedirect = createEnableCookiesRedirect(enableCookiesPath); + const enableCookiesRedirect = createEnableCookiesRedirect( + config.apiKey, + enableCookiesPath, + ); return async function shopifyAuth(ctx: Context, next: NextFunction) { if (ctx.path === oAuthStartPath && !hasCookieAccess(ctx)) { diff --git a/packages/koa-shopify-auth/src/auth/redirection-page.ts b/packages/koa-shopify-auth/src/auth/redirection-page.ts index 6c11be7275..70908b89b5 100644 --- a/packages/koa-shopify-auth/src/auth/redirection-page.ts +++ b/packages/koa-shopify-auth/src/auth/redirection-page.ts @@ -1,18 +1,21 @@ -export default function redirectionScript({origin, redirectTo}) { +export default function redirectionScript({origin, redirectTo, apiKey}) { return ` - diff --git a/packages/koa-shopify-auth/src/auth/test/enable-cookies-redirect.test.ts b/packages/koa-shopify-auth/src/auth/test/enable-cookies-redirect.test.ts index 1b9422a059..bf59e2d183 100644 --- a/packages/koa-shopify-auth/src/auth/test/enable-cookies-redirect.test.ts +++ b/packages/koa-shopify-auth/src/auth/test/enable-cookies-redirect.test.ts @@ -14,10 +14,11 @@ const query = querystring.stringify.bind(querystring); const baseUrl = 'myapp.com/auth'; const shop = 'shop1.myshopify.io'; const path = '/auth/enable_cookies'; +const apiKey = 'somekey'; describe('CreateEnableCookiesRedirect', () => { it('sets the test cookie', () => { - const enableCookiesRedirect = createEnableCookiesRedirect(path); + const enableCookiesRedirect = createEnableCookiesRedirect(apiKey, path); const ctx = createMockContext({ url: `https://${baseUrl}?${query({shop})}`, }); @@ -28,14 +29,14 @@ describe('CreateEnableCookiesRedirect', () => { }); it('sets up and calls the top level redirect', () => { - const enableCookiesRedirect = createEnableCookiesRedirect(path); + const enableCookiesRedirect = createEnableCookiesRedirect(apiKey, path); const ctx = createMockContext({ url: `https://${baseUrl}?${query({shop})}`, }); enableCookiesRedirect(ctx); - expect(createTopLevelRedirect).toHaveBeenCalledWith(path); + expect(createTopLevelRedirect).toHaveBeenCalledWith(apiKey, path); expect(mockTopLevelRedirect).toHaveBeenCalledWith(ctx); }); }); diff --git a/packages/koa-shopify-auth/src/auth/test/index.test.ts b/packages/koa-shopify-auth/src/auth/test/index.test.ts index e90ecc795d..68fd5c79e4 100644 --- a/packages/koa-shopify-auth/src/auth/test/index.test.ts +++ b/packages/koa-shopify-auth/src/auth/test/index.test.ts @@ -47,6 +47,7 @@ describe('Index', () => { await shopifyAuth(ctx, nextFunction); expect(createEnableCookiesRedirect).toHaveBeenCalledWith( + 'myapikey', '/auth/enable_cookies', ); expect(mockEnableCookiesRedirect).toHaveBeenCalledWith(ctx); @@ -64,6 +65,7 @@ describe('Index', () => { await shopifyAuth(ctx, nextFunction); expect(createTopLevelOAuthRedirect).toHaveBeenCalledWith( + 'myapikey', '/auth/inline', ); expect(mockTopLevelOAuthRedirect).toHaveBeenCalledWith(ctx); diff --git a/packages/koa-shopify-auth/src/auth/test/top-level-oauth-redirect.test.ts b/packages/koa-shopify-auth/src/auth/test/top-level-oauth-redirect.test.ts index 0454c9ca5a..e4d510c2bd 100644 --- a/packages/koa-shopify-auth/src/auth/test/top-level-oauth-redirect.test.ts +++ b/packages/koa-shopify-auth/src/auth/test/top-level-oauth-redirect.test.ts @@ -14,10 +14,11 @@ const query = querystring.stringify.bind(querystring); const baseUrl = 'myapp.com/auth'; const shop = 'shop1.myshopify.io'; const path = '/auth/inline'; +const apiKey = 'somekey'; describe('CreateTopLevelOAuthRedirect', () => { it('sets the test cookie', () => { - const topLevelOAuthRedirect = createTopLevelOAuthRedirect(path); + const topLevelOAuthRedirect = createTopLevelOAuthRedirect(apiKey, path); const ctx = createMockContext({ url: `https://${baseUrl}?${query({shop})}`, }); @@ -28,14 +29,14 @@ describe('CreateTopLevelOAuthRedirect', () => { }); it('sets up and calls the top level redirect', () => { - const topLevelOAuthRedirect = createTopLevelOAuthRedirect(path); + const topLevelOAuthRedirect = createTopLevelOAuthRedirect(apiKey, path); const ctx = createMockContext({ url: `https://${baseUrl}?${query({shop})}`, }); topLevelOAuthRedirect(ctx); - expect(createTopLevelRedirect).toHaveBeenCalledWith(path); + expect(createTopLevelRedirect).toHaveBeenCalledWith(apiKey, path); expect(mockTopLevelRedirect).toHaveBeenCalledWith(ctx); }); }); diff --git a/packages/koa-shopify-auth/src/auth/test/top-level-redirect.test.ts b/packages/koa-shopify-auth/src/auth/test/top-level-redirect.test.ts index 195808548b..75564d3c50 100644 --- a/packages/koa-shopify-auth/src/auth/test/top-level-redirect.test.ts +++ b/packages/koa-shopify-auth/src/auth/test/top-level-redirect.test.ts @@ -9,11 +9,12 @@ const query = querystring.stringify.bind(querystring); const baseUrl = 'myapp.com/auth'; const path = '/path'; const shop = 'shop1.myshopify.io'; -const shopOrigin = 'https://shop1.myshopify.io'; +const shopOrigin = 'shop1.myshopify.io'; +const apiKey = 'fakekey'; describe('TopLevelRedirect', () => { it('redirects to the provided path with shop parameter', () => { - const topLevelRedirect = createTopLevelRedirect(path); + const topLevelRedirect = createTopLevelRedirect(apiKey, path); const ctx = createMockContext({ url: `https://${baseUrl}?${query({shop})}`, }); @@ -24,6 +25,7 @@ describe('TopLevelRedirect', () => { redirectionPage({ redirectTo: `https://myapp.com/path?${query({shop})}`, origin: shopOrigin, + apiKey, }), ); });