# Varnish

If you have a Varnish server to manage your site's HTTP cache, you can integrate the call to the First-id gate.

This documentation was written with Varnish 6.2 in VCL 4.1

vmod-querystring

To manipulate query strings more easily, you need to install vmod-querystring. To do this, visit the Github (opens new window) page of the module for installation. If you want to learn more, check out the module documentation (opens new window).

The VCL instructions below will intervene on different Varnish subroutines :

  • vcl_init => Initialization of parsers to detect the firstId and redirectUri query parameters
  • vcl_recv => When receiving user requests:
    • If there is no firstid cookie and a consent cookie YOUR_CONSENT_COOKIE is present, redirect to the gate.
    • If the firstId and redirectUri query parameters are present, start processing.
  • vcl_synth => Allows to handle the response that Varnish will make (without consulting the cache/backend)

Refer to the lifecycle (opens new window) of a Varnish request for more information.

These VCL instructions were designed in a local HTTP environment. Feel free to replace the scheme http:// with https:// according to your infrastructure.

Regarding the attributes of the firstid cookie, in the example below, the path is set to /, with a lifespan of 180 days.

Setting a cookie is done via a string separated by ;. Check the MDN documentation (opens new window) for the list of existing attributes for setting your cookie.

HTTPOnly

To retrieve the firstid cookie in client-side JavaScript, we do not recommend setting the httpOnly attribute.

Cookie configuration

You have to create a Secure cookie on your main domain (see Cookie deposit).

vcl 4.1;
import std;
import querystring;
import blob;

backend default {
    .host = "nginx";
    .port = "80";
}

sub vcl_init {
    new firstIdParser = querystring.filter(match = name);
    firstIdParser.add_string("firstId");

    new redirectUriParser = querystring.filter(match = name);
    redirectUriParser.add_string("redirectUri");
}

sub vcl_recv {
    # you have been just redirect from First-id Gate :
    if (std.strstr(firstIdParser.extract(req.url, mode = keep), "=")
        && std.strstr(redirectUriParser.extract(req.url, mode = keep), "=")
        && req.method == "GET"
    )
    {
        return (synth(999, ""));
    }

    # This rule must be after the previous one, to avoid infinite loop on First-id gate redirection.
    # User hasn't a First-id cookie, go to First-id gate
    # Add here your condition on User Agent to filter robot / crawler
    # Adjust the name of your consent cookie. In this example we consider if the cookie exist, user has consented
    if (req.http.Cookie !~ "firstid" && req.http.Cookie ~ "YOUR_CONSENT_COOKIE" && req.url !~ ".(jpe?g|gif|png|json|js|woff2?|map|css|svg|ico|bmp|xml|ttf|txt)$") {
       return (synth(998, ""));
    }

    return (hash);
}

sub vcl_synth {
  if (resp.status == 998) {
    set resp.status = 302;

    set resp.http.Location = "https://gate.first-id.fr?redirectHost=" + blob.transcode(encoded="http://"+req.http.host, encoding=URL) + "&redirectUri=" + blob.transcode(encoded=req.url, encoding=URL);

    return(deliver);
  }

  if (resp.status == 999) {
    set resp.status = 302;
    # adjust your scheme if needed, in local we have only access on HTTP
    set resp.http.Location = "https://" + req.http.host + blob.transcode(encoded=regsuball(std.strstr(redirectUriParser.extract(req.url, mode = keep), "="),"=",""), decoding=URL);

    # Cookie set section :
    # the separator char = ";"
    # maybe add "Secure;" to ensure HTTPS usage only
    # Optional : add your domain with your ";domain= <YOUR_DOMAIN>" or use req.http.host
    # MaxAge : 1day = 86400 seconds
    # in this exemple : 15 552 000 seconds = 180 days
    set resp.http.Set-Cookie = "firstid"+ std.strstr(firstIdParser.extract(req.url, mode = keep), "=") +"; path=/; Max-Age=15552000";


    return(deliver);
  }
}

URL encoding

The redirectHost and redirectUri parameters must be encoded in urlEncode to be correctly interpreted.

Excluding Redirections

Warning, you should not perform redirections for the following file types:

jpe?g|gif|png|json|js|woff2?|map|css|svg|ico|bmp|xml|ttf|txt

Please add a filter on these file types in your configuration.

# Add call to First-id Healthcheck

To check that the First-id service is online, we recommend you add a call to our Healthcheck. This will prevent your users from being redirected to an unavailable service.

To do this, simply declare an additional backend in your Varnish configuration file, which will contain the URL of our Healthcheck.

Recommended endpoint: /check-v2 – reads a status updated every minute; more resilient to network latency.

Backward compatibility: /check remains available if you need to keep the previous behavior (synchronous test on each call).

backend hcfirstid {
    .host = "healthcheck.prod.first-id.fr";
    .port = "443";
    .probe = {
        .url = "/check-v2";
        .timeout = 60s;
        .interval = 1s;
        .window = 5;
        .threshold = 3;
    }
}

Here's what the configuration looks like :

  • Varnish will attempt 1 check every 60 seconds => .interval = 60s;
  • Varnish will allow 1s for the server to respond => .timeout = 1s;
  • Varnish will look at the last 5 attempts => .window = 5;
  • If there are 3 errors, it considers the backend as KO => .threshold = 3;

Then all you need to do is add a condition to your vcl_recv to prevent the user from being redirected if the First-id backend is down.

sub vcl_recv {
  
  # If the request is a GET and contains the parameters firstId and redirectUri
  if (req.method == "GET"
  && std.strstr(firstIdParser.extract(req.url, mode = keep), "=")
  && std.strstr(redirectUriParser.extract(req.url, mode = keep), "=")) {
    # Deposit firstid cookie
    return (synth(999, ""));
  }
  # Check that the 'YOUR_CONSENT_COOKIE' cookie exists AND that the 'firstid' cookie doesn't exist
  if (req.http.Cookie !~ "firstid" && req.http.Cookie ~ "YOUR_CONSENT_COOKIE" && std.healthy(hc)) {
    # Redirect to First-ID
    return (synth(998, ""));
  }
  return (hash);
}

You'll notice that we've added the condition && std.healthy(hc) to check that the First-id backend is online.

Preserve User Referrer

To preserve the referrer of my user, I must take several actions described here.