Add SSO to Your Ruby Sinatra App Using WorkOS

November 20, 2021

If your SaaS product’s backend is built with Ruby’s Sinatra web framework and you want to add SSO functionality for your customers, you can do a dry-run of the SSO integration using our example Ruby Sinatra app. It makes use of the WorkOS Ruby SDK to authenticate users via SSO.

If you get stuck while following the steps below and aren't able to resolve the issue by reading our API reference or SSO Setup Guide, please reach out to us at [email protected] so we can help!

Prerequisites

Clone the Ruby on Rails app

1. In your CLI, navigate to the directory into which you want to clone the Ruby example app git repo:

2. Clone the main Ruby example app repo:

3. Navigate to the Ruby Sinatra SSO example app, located within the cloned repo:

4.  Install the dependencies:

Securely store the environment variables

5. Obtain and make note of your WorkOS API key and SSO-specific, WorkOS Client ID from the WorkOS Dashboard. The locations of these values are shown in the screenshots below.

6. Create a .env file to store the environment variables:

7. Open the new .env file with your preferred text editor and replace the placeholder values for WORKOS_API_KEY and WORKOS_CLIENT_ID:

The .env file is listed in this repo's .gitignore file, so your sensitive information will not be checked into version control. This is an important consideration for keeping sensitive information such as API keys private.

The workos gem will read your API key and Client ID from the .env file.

8. To ensure the example app is served as expected at this stage, start the server:


9. Navigate to http://localhost:4567, where you will see a “Sign in” button. If you click the button, you will see an error page if you haven’t properly configured SSO in the WorkOS dashboard yet. We’ll do the configuration in the next section. For now, stop the server by typing CTRL + c in the CLI.

Set up SSO with WorkOS

10. Create an Organization and an SSO Connection within the Organization in your WorkOS Dashboard.

11. Locate the Connection ID for the new SSO connection by first navigating to the new Organization’s detail page, and then clicking on the SSO connection’s detail page:


12. Add the Connection ID to the app.rb file, near where the API key and redirect URI are defined:

13. Add http://localhost:4567/callback as a Redirect URI in the Configuration section of the Dashboard. Ensure it is set as the default.


Test the integration

14. Start the server again by running this command in the CLI:


15. Navigate to http://localhost:4567. You will see the “Sign in” button again. If you click the button, you will be prompted to sign in using the identity provider (IdP) you configured during the SSO setup.

16. Once you’re signed in, the webpage will display a JSON object containing the user information for the logged-in user.

Nice work! You just set up SSO for your Ruby Sinatra app!

Need help?

If you get stuck while following the steps below and aren't able to resolve the issue by reading our API reference or SSO Setup Guide, please reach out to us at [email protected] so we can help!

Start Integrating Today
Create an account to begin adding enterprise-ready features to your application today.
Get Started

This site uses cookies to improve your experience. Please accept the use of cookies on this site. You can review our cookie policy here and our privacy policy here. If you choose to refuse, functionality of this site will be limited.