|
| 1 | +--- |
| 2 | +pcx_content_type: how-to |
| 3 | +title: External Evaluation rules |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +--- |
| 7 | + |
| 8 | +import { GlossaryTooltip, Example, WranglerConfig, DashButton } from "~/components"; |
| 9 | + |
| 10 | +With Cloudflare Access, you can create Allow or Block policies which evaluate the user based on custom criteria. This is done by adding an **External Evaluation** rule to your policy. The **External Evaluation** selector requires two values: |
| 11 | + |
| 12 | +- **Evaluate URL** — the API endpoint containing your business logic. |
| 13 | +- **Keys URL** — the key that Access uses to verify that the response came from your API |
| 14 | + |
| 15 | +After the user authenticates with your identity provider, Access sends the user's identity to the external API at **Evaluate URL**. The external API returns a True or False response to Access, which will then allow or deny access to the user. To protect against man-in-the-middle attacks, Access signs all requests with your Access account key and checks that responses are signed by the key at **Keys URL**. |
| 16 | + |
| 17 | +You can set up External Evaluation rules using any API service, but to get started quickly we recommend using [Cloudflare Workers](/workers/). |
| 18 | + |
| 19 | +## Set up external API and key with Cloudflare Workers |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | + |
| 23 | +- [Workers account](/workers/get-started/guide/) |
| 24 | +- Install [npm](https://docs.npmjs.com/getting-started) |
| 25 | +- Install [Node.js](https://nodejs.org/en/) |
| 26 | +- Application protected by Access |
| 27 | + |
| 28 | +### 1. Create a new Worker |
| 29 | + |
| 30 | +1. Open a terminal and clone our example project. |
| 31 | + |
| 32 | + ```sh |
| 33 | + npm create cloudflare@latest my-worker -- --template https://github.com/cloudflare/workers-access-external-auth-example |
| 34 | + ``` |
| 35 | + |
| 36 | +2. Go to the project directory. |
| 37 | + |
| 38 | + ```sh |
| 39 | + cd my-worker |
| 40 | + ``` |
| 41 | + |
| 42 | +3. Create a [Workers KV namespace](/kv/concepts/kv-namespaces/) to store the key. The binding name should be `KV` if you want to run the example as written. |
| 43 | + |
| 44 | + ```sh |
| 45 | + npx wrangler kv namespace create "KV" |
| 46 | + ``` |
| 47 | + |
| 48 | + The command will output the binding name and KV namespace ID, for example |
| 49 | + |
| 50 | + ```txt |
| 51 | + [[kv_namespaces]] |
| 52 | + binding = "KV" |
| 53 | + id = "YOUR_KV_NAMESPACE_ID" |
| 54 | + ``` |
| 55 | + |
| 56 | +4. Open the [Wrangler configuration file](/workers/wrangler/configuration/) in an editor and insert the following: |
| 57 | + |
| 58 | + - `[[kv_namespaces]]`: Add the output generated in the previous step. |
| 59 | + - `<TEAM_NAME>`: your Cloudflare Zero Trust <GlossaryTooltip term="team name">team name</GlossaryTooltip>. |
| 60 | + |
| 61 | + <WranglerConfig> |
| 62 | + |
| 63 | + ```toml |
| 64 | + name = "my-worker" |
| 65 | + workers_dev = true |
| 66 | + compatibility_date = "2024-08-06" |
| 67 | + main = "index.js" |
| 68 | + |
| 69 | + |
| 70 | + [[kv_namespaces]] |
| 71 | + binding = "KV" |
| 72 | + id = "YOUR_KV_NAMESPACE_ID" |
| 73 | + |
| 74 | + [vars] |
| 75 | + TEAM_DOMAIN="<TEAM_NAME>.cloudflareaccess.com" |
| 76 | + DEBUG=false |
| 77 | + ``` |
| 78 | + </WranglerConfig> |
| 79 | + |
| 80 | +### 2. Program your business logic |
| 81 | + |
| 82 | +1. Open `index.js` and modify the `externalEvaluation` function to perform logic on any identity-based data sent by Access. |
| 83 | + |
| 84 | +:::note |
| 85 | + |
| 86 | +- Sample code is available in our [GitHub repository](https://github.com/cloudflare/workers-access-external-auth-example). |
| 87 | +- To view a list of identity-based data fields, log in to your Access application and append `/cdn-cgi/access/get-identity` to the URL. For example, if `www.example.com` is behind Access, visit `https://www.example.com/cdn-cgi/access/get-identity`. |
| 88 | + ::: |
| 89 | + |
| 90 | +2. Deploy the Worker to Cloudflare's global network. |
| 91 | + |
| 92 | + ```sh |
| 93 | + npx wrangler deploy |
| 94 | + ``` |
| 95 | + |
| 96 | +The Worker will be deployed to your `*.workers.dev` subdomain at `my-worker.<YOUR_SUBDOMAIN>.workers.dev`. |
| 97 | + |
| 98 | +### 3. Generate a key |
| 99 | + |
| 100 | +To generate an RSA private/public key pair: |
| 101 | + |
| 102 | +1. Open a browser and go to `https://my-worker.<YOUR_SUBDOMAIN>.workers.dev/keys`. |
| 103 | + |
| 104 | +2. (Optional) Verify that the key has been stored in the `KV` namespace: |
| 105 | + 1. In the Cloudflare dashboard, go to the **Workers KV** page. |
| 106 | + <DashButton url="/?to=/:account/workers/kv/namespaces" /> |
| 107 | + 2. Select **View** next to `my-worker-KV`. |
| 108 | + |
| 109 | +Other key formats (such as DSA) are not supported at this time. |
| 110 | + |
| 111 | +### 4. Create an External Evaluation rule |
| 112 | + |
| 113 | +1. In [Zero Trust](https://one.dash.cloudflare.com/), go to **Access** > **Policies**. |
| 114 | + |
| 115 | +2. Edit an existing policy or select **Add a policy**. |
| 116 | + |
| 117 | +3. Add the following rule to your policy: |
| 118 | + |
| 119 | +| Rule Type | Selector | Evaluate URL | Keys URL | |
| 120 | +| --------- | ------------------- | ------------------------------------------------- | ------------------------------------------------------ | |
| 121 | +| Include | External Evaluation | `https://my-worker.<YOUR_SUBDOMAIN>.workers.dev/` | `https://my-worker.<YOUR_SUBDOMAIN>.workers.dev/keys/` | |
| 122 | + |
| 123 | +4. Save the policy. |
| 124 | + |
| 125 | +5. Go to **Access** > **Applications** and edit the application for which you want to apply the External Evaluation rule. |
| 126 | + |
| 127 | +6. In the **Policies** tab, add the policy that contains the External Evaluation rule. |
| 128 | + |
| 129 | +7. Select **Save application**. |
| 130 | + |
| 131 | +When a user logs in to your application, Access will now check their email, device, location, and other identity-based data against your business logic. |
| 132 | + |
| 133 | +### Troubleshooting the Worker |
| 134 | + |
| 135 | +To debug your External Evaluation rule: |
| 136 | + |
| 137 | +1. Go to your Worker directory. |
| 138 | + |
| 139 | + ```sh |
| 140 | + cd my-worker |
| 141 | + ``` |
| 142 | + |
| 143 | +2. Open the [Wrangler configuration file](/workers/wrangler/configuration/) in an editor and set the `debug` variable to `TRUE`. |
| 144 | + |
| 145 | +3. Deploy your changes. |
| 146 | + |
| 147 | + ```sh |
| 148 | + npx wrangler deploy |
| 149 | + ``` |
| 150 | + |
| 151 | +4. Next, start a session to output realtime logs from your Worker. |
| 152 | + |
| 153 | + ```sh |
| 154 | + wrangler tail -f pretty |
| 155 | + ``` |
| 156 | + |
| 157 | +5. Log in to your Access application. |
| 158 | + |
| 159 | + The session logs should show an incoming and outgoing JWT. The incoming JWT was sent by Access to the Worker API, while the outgoing JWT was sent by the Worker back to Access. |
| 160 | + |
| 161 | +6. To decode the contents of a JWT, you can copy the token into [jwt.io](https://jwt.io/). |
| 162 | + |
| 163 | + The incoming JWT should contain the user's identity data. The outgoing JWT should look similar to: |
| 164 | + |
| 165 | + ```js |
| 166 | + { |
| 167 | + "success": true, |
| 168 | + "iat": 1655409315, |
| 169 | + "exp": 1655409375, |
| 170 | + "nonce": "9J2E9Xg6wYj8tlnA5MV4Zgp6t8rzmS0Q" |
| 171 | + } |
| 172 | + ``` |
| 173 | + |
| 174 | + Access checks the outgoing JWT for all of the following criteria: |
| 175 | + |
| 176 | + - Token was signed by **Keys URL**. |
| 177 | + - Expiration date has not elapsed. |
| 178 | + - API returns `"success": true`. |
| 179 | + - `nonce` is unchanged from the incoming JWT. The `nonce` value is unique per request. |
| 180 | + |
| 181 | + If any condition fails, the External Evaluation rule evaluates to false. |
0 commit comments