DEV Community 👩‍💻👨‍💻

Cover image for Conditional Basic Authorization using the platform, Layer0
Rishi Raj Jain
Rishi Raj Jain

Posted on

Conditional Basic Authorization using the platform, Layer0

After writing Basic Authentication using the platform, Layer0, I thought what if I wanted some users to land to google.com directly if they were from a given IP address or were using an Authorization Header?

What is meant by Conditional Basic Authorization?

I infer it as the "conditional" bypass to basic authorization on an app. The condition herein, can be anything, from matching a certain header to a matching body of a POST request!

For e.g., I want only the user(s) that request a page with cookie name who with value rishi, shall be allowed to view the contents of the page, while rest of the users are thrown to basic authorization. In this case, the "conditional" bypass is of being a user that requests with a cookie: who=rishi.

Creating a Conditional Basic Authentication App with Layer0

The following 8 steps will walk you through implementation of conditional basic authentication with Layer0, which is agnostic to the tech stack of your website.

In the example below, we'll be redirecting authenticated users to https://google.com.

Step 1: Setup

Please complete the steps as in Basic Authentication using the platform, Layer0 to proceed further to the next step 👇🏻

Step 2: Create a new Layer0 environment (say, to-google)

To create a new Layer0 environment, go to your project link as obtained in Step 1 and follow as walked through in Creating an Environment - Layer0 Docs

Step 3: Deploy another app to to-google

In case you find dearth of apps to deploy, go back to the guide Basic Authentication using the platform, Layer0, skip the Step 2 in there and deploy the app via the following command:

npx @layer0/cli@latest deploy -e=to-google # -e or --environment
Enter fullscreen mode Exit fullscreen mode

Step 4: Reach default Layer0 environment

Go to your project in Layer0 console, click on Environments in the navigation bar, and then on default. The following shows the screen you'll land at:
default Environment at basic-auth-example

Click on default and then on Configuration to reach the stage as below:
default's configuration on Layer0 Console

Step 5: Configure Split Testing Rules on the default Layer0 environment

Click on Edit, and then scroll to the bottom of the page under "Split Testing", and then click on Add Rule as in the image below:

Add Rule on Split Testing Layer0 environment

This should open up a modal as below:

Rule's dialog box

Step 6: Add the Cookie Name & Value to create conditional bypass to Basic Authentication

Click on Add Criterion, and then from the dropdown, change Device to Cookie. On the right, one may now the respective Cookie Name and the value it should match. Also, as these users are already authenticated, you want them to be sent directly to google, hence change current environment in the dropdown below to to-google.

This is how this configuration will look like:
who=rishi & to-google

Click on Apply to save the rule.

Step 7: Send all other users to default environment

To establish this, click on Add Rule, under Destination(s) select 100% of the traffic to face whatever is there in the current Layer0 environment, as done in the image below:

default configuration

Click on Apply to save the rule.

Step 8: Review your split test configuration

As in the image below, all the users that fail to match the cookie, go to the environment that attemps to authenticate them, yet again.

Final Conifguration

Activate (Ship!) 🚀

To deploy your changes, click on Activate button at the bottom of the page, and wait for the site to be re-deployed.

Example

The example link still remains the same (let's not make it public that who=rishi cookie users can go to google.com directly 🤫): https://rishi-raj-jain-yg-basic-auth-example-default.layer0-limelight.link

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.