Conditional Basic Authorization using the platform, Layer0
Tags: edge, tutorial, webdev, layer0
Published: 2022-06-05 22:06:24

After writing [Basic Authentication using the platform, Layer0](https://dev.to/digitalplayer1125/basic-authentication-using-the-platform-layer0-41co), 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](https://dev.to/digitalplayer1125/basic-authentication-using-the-platform-layer0-41co) 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](#setup) and follow as walked through in [Creating an Environment - Layer0 Docs](https://docs.layer0.co/guides/environments#creating-an-environment)

### 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](https://dev.to/digitalplayer1125/basic-authentication-using-the-platform-layer0-41co), **skip** the Step 2 in there and deploy the app via the following command:

```bash
npx @layer0/cli@latest deploy -e=to-google # -e or --environment
```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6focls0decpbemgv43xt.png)

Click on default and then on Configuration to reach the stage as below:
![default's configuration on Layer0 Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m53gxpp0svvlgn27j44t.png)
 
### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vo6qa0sgoxaniwlg9fn.png)

This should open up a modal as below:

![Rule's dialog box](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4nj8mc98z8qegx359out.png) 

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpz92oq65j6854841zkb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnv1qux28nymuhjvxdrt.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grqd6ioaf157fe2jhhfc.png)

### 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