Repository navigation
feat: admin_origin, for a local stand-in of Shopify (local-storefront) - #34
Merged
Merged
Conversation
`ShopifyBuilder::admin_origin` and `ShopifyApp::with_admin_origin` send the
requests built from the shop's domain (GraphQL, the client credentials grant,
refresh tokens, the code and token exchanges) and the install link of
`authorization_url` to another origin than `https://{shop}`: a local fake of
Shopify such as local-storefront (`http://localhost:9292`), a mock server, a
proxy. Only the scheme, the host and the port change. The shop stays the
identity of the session tokens, the tokens and the errors; the requests sent
to the origin name it in `X-Forwarded-Host`, and the install link in a last
`shop` parameter, so that one origin can serve several shops.
`AdminOrigin` checks it when the client is built: an absolute `https://` URL,
or `http://` to `localhost`, `127.0.0.1` or `[::1]` only, with no path, query
string, fragment or user. Anything else is an `Error::Config` that does not
show what was given. Without the option, nothing changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an admin origin: the requests
shopify_apibuilds from a shop's domain can go to another origin thanhttps://{shop}. The use case is a local fake of Shopify such as local-storefront (http://localhost:9292), so that openshop-rust can run against it. A mock server or a proxy works too.Without the option, nothing changes. The existing tests still assert the exact
https://{shop}/…URLs, and a new test checks that noX-Forwarded-Hostis sent.Public API
ShopifyApp::builderandShopifyApp::clientgive the app's origin to the clients they build, the same way they pass on the transport and the token store. A later.admin_origin(…)on the builder replaces it.What follows the origin
The option replaces only the scheme, host and port. Paths stay Shopify's.
admin_origin("http://localhost:9292")graphql_url)https://{shop}/admin/api/{v}/graphql.jsonhttp://localhost:9292/admin/api/{v}/graphql.jsonhttps://{shop}/admin/oauth/access_tokenhttp://localhost:9292/admin/oauth/access_tokenauthorization_url, browser-facing)https://{shop}/admin/oauth/authorize?…http://localhost:9292/admin/oauth/authorize?…&shop={shop}grep -rn 'https://{' crates/finds these three, plusShopDomain::origin().ShopDomain::origin()stays the shop's identity, and the session-tokendest/isscheck uses it.The shop is still the shop. Session tokens are still verified against
https://{shop}andhttps://{shop}/admin, which is what lsf mints. Errors, logs andTokenStore::savestill name{shop}.myshopify.com.When the host no longer names the shop, the shop is named another way:
X-Forwarded-Host: {shop}. lsf reads this header to pick the shop (seeexplicit_shopin lsf'sadmin/auth/oauth.rs, andorigin_of: "clients send the shop's domain in it"). It matters for the client credentials grant: without it, lsf uses its default shop.GET /admin/oauth/authorize. Without the change, the browser would go to real Shopify for a shop that only exists locally, and the callback would bring a code lsf can't exchange. The link also gets a lastshop={shop}parameter, which lsf reads ("with&shop=for a shop other than the default one"). Without it, lsf would approve for its default shop.Validation (at build time,
Error::Config)http://orhttps://URL: a host and an optional port 1-65535. No path (a trailing/is fine), no query, no fragment, no userinfo.http://is only accepted forlocalhost,127.0.0.1and[::1], so a real access token never leaves the machine in clear text. I took the list literally.127.0.0.2,app.localhost,0.0.0.0and[::ffff:127.0.0.1]are refused overhttp://and accepted overhttps://.[a-z0-9-]), dotted IPv4 or bracketed IPv6. A host whose last label is a number must be a well-formed IPv4 address, sohttps://999.1.1.1andhttps://127.000.000.001are refused./, canonical IP form.no_variant_shows_a_secret.ShopifyBuilder'sDebugdoesn't show the raw input either.Bulk results
Bulk results URLs (lsf answers
http://localhost:9292/__lsf/admin/bulk/<shop>/<n>.jsonl) and staged-upload targets are absolute and used as they come. There was never any check on them to loosen, so nothing changed for real hosts. The default transport already fetchedhttp://127.0.0.1:<port>/…(tests/bulk_stream.rs). A new test downloads from{origin}/__lsf/admin/bulk/…and checks that neither a token norX-Forwarded-Hostis sent.Tests
src/origin.rsunit tests cover accepted and normalized origins, refused ones (35 cases), http only for loopback, errors that don't show the input, and URL building.src/client.rsandsrc/oauth.rsunit tests covergraphql_urlwith and without the option, an invalid origin refused bybuild(), the install link withshop, propagation toShopifyApp::builder, and session tokens still verified for the shop.tests/admin_origin.rsruns against a local HTTP server onlocalhost:<port>through the realReqwestTransport:X-Shopify-Access-Token,X-Forwarded-Host,subject_token);MockTransport: no option means exacthttps://{shop}URLs and no extra header.Checks run locally
I ran these with the rustup 1.99.1 toolchain, the version
mise.tomlpins:cargo fmt --all -- --check: okcargo clippy --workspace --all-targets --all-features --locked -- -D warnings: okcargo test --workspace --all-features --locked: 384 passed, 0 failedcargo test -p shopify_api --locked: 100 passedcheck:features(4 ×cargo check --no-default-features …,cargo test --no-default-features --doc, 29 doctests): okRUSTDOCFLAGS=-D warnings cargo doc --workspace --all-features --no-deps --locked: okcargo +1.88 check --workspace --all-features --locked(MSRV): okcargo package --workspace --locked: okcargo deny check: advisories, bans, licenses and sources okThe version is not bumped and nothing is published. openshop-rust will use this branch through
[patch.crates-io]until a release that contains it is published.🤖 Generated with Claude Code