Skip to content

Commit 058cd00

Browse files
get host from auth header, and return in redirect url
1 parent a9c5101 commit 058cd00

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-koa-shopify-auth",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "A better, simplified version of the (no longer supported) @Shopify/koa-shopify-auth middleware library. It removes the use of cookies for sessions (which greatly smooths the auth process), replaces a deprecated API call, and supports v2 of the official @shopify/shopify-api package.",
55
"author": "TheSecurityDev",
66
"license": "MIT",

src/verify-request.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ export default function verifyRequest(options?: VerifyRequestOptions) {
7878
ctx.response.status = 401;
7979
ctx.response.set(REAUTH_HEADER, "1"); // Tell the client to re-authorize by setting the reauth header
8080
// Get the shop from the session, or the auth header (we can't get it from the query if we're making a post request)
81-
let shop: string;
82-
if (session) {
83-
shop = session.shop; // Get shop from the session token
84-
} else if (Shopify.Context.IS_EMBEDDED_APP) {
85-
shop = getShopFromAuthHeader(ctx); // Get shop from auth header
81+
let reauthUrl = authRoute;
82+
if (Shopify.Context.IS_EMBEDDED_APP) {
83+
reauthUrl += `?${getShopAndHostQueryStringFromAuthHeader(ctx)}`;
84+
} else if (session?.shop) {
85+
reauthUrl += `?shop=${session.shop}`; // This won't have the host param, which probably won't work (but this library is meant for embedded apps, so it's not a big deal; do non-embedded apps even need the host param?)
8686
}
87-
const reauthUrl = `${authRoute}?shop=${shop}`;
8887
ctx.response.set(REAUTH_URL_HEADER, reauthUrl); // Set the reauth url header
8988
} else {
9089
// Otherwise redirect to the auth page
@@ -105,13 +104,14 @@ async function clearSession(ctx: Context, accessMode = defaultOptions.accessMode
105104
}
106105
}
107106

108-
function getShopFromAuthHeader(ctx: Context) {
107+
function getShopAndHostQueryStringFromAuthHeader(ctx: Context): string {
109108
const authHeader: string = ctx.req.headers.authorization;
110109
const matches = authHeader?.match(/Bearer (.*)/);
111110
if (matches) {
112111
const payload = Shopify.Utils.decodeSessionToken(matches[1]);
113112
const shop = payload.dest.replace("https://", "");
114-
return shop;
113+
const host = Buffer.from(payload.iss.replace("https://", "")).toString("base64");
114+
return new URLSearchParams({ shop, host }).toString();
115115
}
116116
return null;
117117
}

0 commit comments

Comments
 (0)