Skip to main content

Implementing OAuth 2.0 into Your Product

Introduction​

One of the biggest issues in the restaurant technology space is the lack of automation for onboarding merchants onto new services. This is especially true for Point of Sale (POS) and Delivery Service Provider (DSP) applications. The process of onboarding a merchant is often manual and time-consuming, sometimes requiring the merchant to provide their credentials to the application provider. This can lead to weeks of delay for the merchant and guarantees fragile integrations.

When integrating with Stream as a POS or DSP, we strive to give our users the most Stream-lined experience in all aspects of our products. If you want state-of-the-art security for 3rd party data access or have aspirations to build your own app store within your product ecosystem, then we recommend building the following solution.

Overview​

OAuth Control Flow

In this document, we'll discuss how to implement OAuth 2.0 into your product to fully automate onboarding of merchants. OAuth 2.0 (RFC 6749) is a protocol that allows a user to grant a third-party web site or application access to their resources, without necessarily revealing their credentials. This document provides a high-level overview of how to implement OAuth 2.0 in your product to fully automate onboarding of merchants. The process includes setting up an Authorization Server, implementing Client Applications, and enabling Resource Owners (in this case, the merchants) to grant access to their resources.

1. Build The Authorization Flow​

The Authorization Server is the software that issues access tokens to Stream after successfully authenticating the resource owner and obtaining authorization. To set this up, you'll need to:

  • Build a web page for merchants to log in to their existing accounts. Stream will redirect them to this page with our client_id as a query parameter.
    • If a merchant reaches this page without an account, you can redirect them to a sales page to sign up recognizing Stream as the lead-gen by the client_id.
    • After successful login, show the merchant a confirmation button that explains which resources they are granting Stream access to.
  • Build a token endpoint /v1/token supporting the following grant types.
    • authorization_code - where Stream can exchange an authorization code for an access_token, expire_time (for the access token), refresh_token and id by authenticating with our client secret created in the next step.
      • id identifies the account the merchant authorized: the company, estate or organization the connection belongs to. See The id field.
      • expire_time should be Unix time in milliseconds. https://currentmillis.com/
        • Suggestion: Set the token expiration period to 30 days. We aim to refresh the token 7 days before its expiration to allow ample time for troubleshooting if any issues arise with the integration.
    • refresh_token - when the access token is near expiration, Stream will send a request to your token endpoint with the refresh_token to obtain a new access_token and expire_time.
      • The refresh_token can remain the same if desired, otherwise we will support refresh token rotation as well.
        • If no rotation supported, you will want to provide refresh_token_expire_time. We will use this to begin to send refresh notifications when closing in on the expiration, in attempt to successfully refresh prior to expiration.

The id field​

The id returned by the authorization_code exchange tells Stream which account the tokens belong to. Stream stores it on the connection and uses it to keep one set of tokens per account.

  • Set id to the top-level entity the merchant authorized, such as their company or estate ID, not a user or a single store.
  • Keep id the same for that account on every exchange, including reconnections. Do not generate a new id per authorization, session or token.
  • Stream compares the id in the token response only. It does not compare provider_id from your locations response.
  • Return id from the first connection. A connection made without one cannot later be reconnected with one.
caution

Returning a different id when a merchant reconnects is rejected. Locations and connections sit under each OAuth connection. If a merchant reconnects the connection for company 1 but logs in to company 2, your token endpoint returns id 2, and Stream rejects the reconnection with a 400 instead of swapping the account under those connections, which would break them. To connect company 2, the merchant adds a new connection instead.

How Stream refreshes tokens​

Stream refreshes on the token's expiry, not on a fixed schedule.

  • Stream calls your token endpoint with grant_type=refresh_token 7 days before the expire_time of the current access token. Give access tokens a lifetime well over 7 days; we recommend 30. With a shorter lifetime, every new access token is already inside the 7-day window, so Stream refreshes again straight away.
  • If you return refresh_token_expire_time, Stream alerts you and the merchant once it is within 30 days. A refresh token that lives less than 30 days is always in that window.
  • A refresh that returns 401 (or 400 for a POS integration), or a 200 without both access_token and expire_time, is treated as permanent. Stream does not retry it: the connection is marked as needing reconnection, and the merchant must reconnect it.
  • A timeout, network error or 5xx is treated as temporary. Stream tries again later and does not mark the connection.
caution

Returning an error body with a 200 ends the connection. Stream reads any 200 without access_token and expire_time as a failed refresh and marks the connection as needing reconnection on the first one. Return a 5xx for a temporary failure so Stream retries, and a 401 only when the refresh token is no longer valid.

2. Register Stream As Client Application​

Client Applications are 3rd party entities (such as Stream) that request access to a merchant's resources.

  • You must provide a unique client ID and a client secret for Stream to securely exchange authorization codes for tokens.
  • We will provide our redirect URI for you to send merchants back to with the authorization code attached as a query parameter after successful authentication.

3. Support Access Tokens For Resource Endpoints​

  • When building the required endpoints for your Stream integration, you must support access token authorization for the endpoints labeled as Stream -> <POS or DSP> (OAuth)

4. Handling Refresh Token Expiration​

  • If you decide not to support refresh token rotation, or use a platform that doesn't allow for it like Cognito, then you must handle the expiration of the token accordingly.
    • Support refresh_token_expire_time on the /v1/token response.
    • Properly return 401 on refresh when expired, so repeated attempts don't occur.
    • Monitor alerting for refresh token expirations, manually reconnecting where needed.
      • When a refresh token is near expiration (within 30 days), we will alert you in preparation of this expiration. During this time, you can go into the platform and manually reconnect the integration through settings to prevent interruptions.

Conclusion​

In conclusion, implementing OAuth 2.0 into your product involves setting up an Authorization Server, implementing client applications, and enabling resource owners to grant access to their resources. By carefully considering the security aspects and complying with the OAuth 2.0 standards, you can ensure a smooth and secure experience for your merchants.

If you follow the steps outlined in this document, you'll be able to implement OAuth 2.0 into your product and fully automate onboarding of merchants. For more information, see the OAuth 2.0 specification. If you have any questions or would like to discuss your implementation, please reach out to us at partners@streamorders.com. We appreciate your interest in building high quality solutions for your customers.