Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 45

OAuth 2.0 Grant Flow Explained

Recommended resources

Hello! Welcome to the third lesson in our module on Shopify App Development Foundations.

In our last session, we drew a clear line between the REST and GraphQL Admin APIs. You learned that while REST is a legacy option you might encounter, GraphQL is the modern standard for all new Shopify apps due to its efficiency and type safety. We concluded with a crucial question: how does our app get the permission it needs to make these powerful API calls in the first place?

Today, we answer that question head-on. This lesson will demystify the process of app authentication and authorization. Your goal is to be able to explain the OAuth 2.0 grant flow, the industry-standard protocol that Shopify uses to allow your app to securely access a merchant's store data on their behalf.

Understanding this flow is non-negotiable for a professional Shopify developer. It's the handshake that initiates the entire relationship between an app and a store. Given your preference for understanding the "why" behind the technology, we'll explore this from first principles, ensuring you grasp not just the steps, but the security and design rationale behind them.

1. The Core Problem: Delegated Authorization

Before diving into the mechanics, let's establish the fundamental problem OAuth was designed to solve. How can we let a third-party application (like your app) access data from a service (like Shopify) on behalf of a user (the merchant), without the user having to share their password with your app?

Giving your password to a third-party app is a massive security risk. The app would have unlimited access, and revoking that access would require you to change your main password, affecting every service you use. OAuth was created to solve this "delegated authorization" problem.

To get a quick overview directly from Shopify, let's watch a short video.

What is OAuth? Open Authorization 2.0 Explained

The video 'What is OAuth?' from the official ShopifyDevs channel provides an excellent introduction to the core concepts and the problems OAuth helps solve.

Watch the first 3 minutes and 12 seconds of this video. Pay close attention to: The critical distinction between authentication (verifying who you are) and authorization (what you're allowed to do). The three major problems that arise from sharing credentials directly.

As the video explained, OAuth is a framework that allows a user to grant limited, revocable permissions to an application without ever sharing their credentials. This is the foundation of the entire Shopify App Store ecosystem.


2. The Cast of Characters: OAuth Roles and Terminology

To understand the OAuth flow, we first need to define the actors involved. The terminology can be a bit abstract, so we'll map each term to our Shopify context.

A more in-depth video can help us here. While this video is quite long, we'll only watch specific, highly relevant segments.

OAuth 2.0 and OpenID Connect (in plain English)

The video 'OAuth 2.0 and OpenID Connect (in plain English)' by OktaDev is one of the best deep dives available. We'll use it to define the core terminology.

Watch the segment from 15:54 to 18:55. This will introduce the key terms we'll be using throughout the lesson.

Let's summarize those roles in our Shopify context:

  • Resource Owner: The Shopify Merchant. They own the data (products, orders, etc.) and have the authority to grant access to it.
  • Client: Your Shopify App. It's the application that wants to access the merchant's data.
  • Authorization Server: Shopify's OAuth service. This is the system that presents the consent screen to the merchant and issues access credentials.
  • Resource Server: The Shopify Admin API (GraphQL or REST). This is the server that hosts the protected data and responds to API requests.

The entire goal of the OAuth flow is for the Client (your app) to get a special key, called an Access Token, which it can present to the Resource Server (the Admin API) to prove it has permission from the Resource Owner (the merchant).


3. The Flow: Authorization Code Grant

Shopify apps use the most common and secure OAuth 2.0 flow, known as the Authorization Code Grant. It's a multi-step process involving redirects between the merchant's browser, your app, and Shopify's authorization server.

Let's visualize the entire sequence first, then break it down.

Shopify App OAuth 2.0 Grant Flow Sequence Diagram
This sequence diagram shows the full journey of a merchant installing a Shopify app. It illustrates the redirects and server-to-server communication required to obtain an access token and establish a session.

Now, let's walk through this flow, step-by-step, using the OktaDev video again for a detailed explanation.

OAuth 2.0 and OpenID Connect (in plain English)

This segment of the OktaDev video details the Authorization Code flow. It clearly explains the purpose of each step, from the initial redirect to the final API call.

Watch from 18:55 to 22:35. Focus on the sequence: The app redirects the user to the authorization server. The server redirects the user back with an authorization code. The app's backend exchanges that code for an access token. The app uses the access token to call the resource server.

Here's a breakdown of the steps you just saw, applied to Shopify:

  1. Initiate Authorization: A merchant clicks "Install App". Your app redirects them to a special Shopify URL (/admin/oauth/authorize). This URL includes your app's unique client_id, the scopes (permissions) you're requesting, and a redirect_uri (where Shopify should send the merchant back).

  2. Merchant Grants Consent: Shopify presents a screen to the merchant, listing the scopes your app requested (e.g., "This app wants to read products and write orders"). The merchant logs into their store (if they aren't already) and clicks "Install app" to approve.

  3. Receive Authorization Code: Shopify redirects the merchant's browser back to the redirect_uri you specified. Appended to this URL is a temporary, one-time-use authorization_code.

  4. Exchange Code for Access Token: Your app's backend server receives this request. It then makes a direct, secure, server-to-server POST request to Shopify's token endpoint (/admin/oauth/access_token). This request includes the authorization_code along with your app's secret credentials (client_id and client_secret).

  5. Receive Access Token: If the code and credentials are valid, Shopify's server responds directly to your server with the holy grail: the access_token.

  6. Store the Token and Make API Calls: Your app server securely stores this access_token (usually in a database, associated with the shop's domain) and uses it in the X-Shopify-Access-Token header for all future Admin API calls on behalf of that store.

The "Why": Front-Channel vs. Back-Channel Security

You might be wondering: why the two-step process? Why not just send the access_token directly in the redirect URL in Step 3? This is the most brilliant part of the Authorization Code flow's design, and it's critical for security.

OAuth 2.0 and OpenID Connect (in plain English)

This is the most important segment for understanding the security rationale. It explains the concept of front-channel (less secure, browser) and back-channel (highly secure, server-to-server) communication.

Please watch carefully from 26:35 to 33:15. This addresses why the exchange for the access token must happen on the backend.

The key takeaway is that anything happening in the user's browser is considered the front-channel. It's less secure because the URL and its parameters can be seen in browser history, logs, or by malicious browser extensions. The authorization_code is passed through the front-channel. It's temporary and single-use, so the risk is contained.

The exchange for the powerful, long-lived access_token happens on the back-channel—a direct and secure server-to-server connection. This ensures the access_token is never exposed to the browser, where it could be stolen.

Test your understanding!

A developer suggests "simplifying" the OAuth flow by having Shopify send the permanent access_token directly to the app's callback URL in the merchant's browser. Based on the concept of front-channel vs. back-channel, why is this a terrible idea?

Show answer

This is a terrible idea because it would expose the permanent access_token on the front-channel (the browser). An attacker could potentially steal this token from the URL in several ways: from the browser's history, through a malicious browser extension, or via network sniffing on an insecure Wi-Fi network. Once stolen, the attacker could use the token to make malicious API calls to the merchant's store. The Authorization Code flow is specifically designed to ensure the powerful access_token is only ever transmitted on the secure back-channel, from Shopify's server directly to the app's server.


4. The Shopify Implementation in Detail

Now let's connect this theory to the concrete details you'll see in Shopify's documentation and code. The abstract concepts map to specific parameters and verification steps.

Implement authorization code grant manually

Shopify's official documentation, 'Implement authorization code grant manually', walks through the practical steps. Reading this will solidify your understanding of the parameters and security checks involved.

Read the following sections from the documentation: Step 2: Request authorization code: Look at the table of query parameters for the authorization URL. Pay attention to client_id, scope, redirect_uri, and state (also called a nonce). Step 3: Validate authorization code: Review the 'Security checks' subsection. This highlights the importance of validating the nonce and the hmac. Step 4: Get an access token: Look at the parameters for the POST request. Notice that this is where the client_secret is used.

Let's focus on two crucial security parameters mentioned in that document:

  • state (or nonce): This is a random, unpredictable string your app generates and includes in the initial redirect to Shopify (Step 1). Shopify passes it back to you in the callback (Step 3). Your app must verify that the state value it receives is the exact same one it sent. This prevents Cross-Site Request Forgery (CSRF) attacks, where an attacker tries to trick a merchant into authorizing a malicious request.
  • hmac: This is a signature generated by Shopify using your app's client_secret. Your app must verify this signature on all incoming requests from Shopify to ensure they are authentic and haven't been tampered with.

Your full-stack experience with Node.js and Express will help you visualize how this works in code.

Authenticating App Requests with OAuth 2.0 in Shopify

This blog post, 'Authenticating App Requests with OAuth 2.0 in Shopify', provides a concise summary and, most importantly, a simplified Node.js/Express code example that brings the whole flow to life.

Read section 3, 'The OAuth Authorization Code Flow', for a quick recap. Then, carefully study the code in section 4, 'Implementing OAuth 2.0 in Node.js/Express'. Notice how the /auth route handles the initial redirect and the /auth/callback route handles the code exchange.

The code snippet you just reviewed demonstrates the two critical server routes:

  1. One route to kick off the process by redirecting the merchant to Shopify.
  2. A callback route to handle the return from Shopify, verify security parameters, and make the back-channel call to exchange the code for the access token.

Conclusion

You've now dissected the entire authentication and authorization process for Shopify apps. This is a complex but essential topic, and understanding it from first principles will make you a much more capable developer.

Key Takeaways:

  • OAuth 2.0 is for delegated authorization, allowing an app to act on behalf of a user without needing their password.
  • Shopify apps use the Authorization Code Grant Flow, which is the most secure and common OAuth 2.0 flow for web applications.
  • The flow involves a front-channel (browser-based redirects) to get a temporary authorization_code and a secure back-channel (server-to-server request) to exchange that code for a permanent access_token.
  • This two-step process is a deliberate security design to protect the access_token from ever being exposed to the browser.
  • Key parameters like client_id, scope, state, and hmac are used to identify the app, define permissions, and secure the flow against attacks.

In our next lesson, we will finally get our hands dirty. We'll set up a new Shopify app project using the Shopify CLI and a Node.js backend. The CLI brilliantly automates this entire OAuth flow for you. But now, when you run that command and see your app install successfully, you'll know exactly what's happening in the intricate dance of redirects and API calls under the hood.