Filevine API AtlasHomeMain APIBulk DataIdentityWebhook events
Start here

Webhook Security

Inbound payloads sent from the Filevine API include a method of verification so that you can ensure that they are being sent from a legitimate source. All subscriptions are generated with a

Inbound payloads sent from the Filevine API include a method of verification so that you can ensure that they are being sent from a legitimate source. All subscriptions are generated with a Filevine Identity Signature for verification.

For either type of signature the following headers will be included in the webhook message.

Header Description
x-fv-timestamp An ISO timestamp like 2019-07-23T22:48:42.511Z
x-fv-messageid The ID of the message
Authorization The Filevine Identity Signature JsonWebToken.
x-fv-hash A constructed hash using the API key, the time stamp, message ID, and the API secret. *This header is only included if you are using API Key signatures.

The following instructions demonstrate how to verify either type of signature.

Javascript: Filevine Identity Signature Validation

// JAVASCRIPT Identity signature validation
import * as oauth4webapi from 'oauth4webapi';
import * as jose from 'jose'

const authorizationHeader = 'Bearer ${JsonWebToken}';
const token = authorizationHeader.split(' ')[1];

const authOptions = {
    Authority: "https://identity.filevine.com",
// For US Tenants. Canada is https://identity.filevine.ca and CJIS is https://identity.filevinegov.com
    Audience: "filevine-v2-webhooks",
    RequiredScope: "filevine-v2-webhooks-access",
};

async function loadJwksUrl() {
    const metadata = await oauth4webapi.discoveryRequest(new URL(authOptions.Authority))
        .then((response) => oauth4webapi.processDiscoveryResponse(new URL(authOptions.Authority), response));;
    if (!metadata || !metadata.jwks_uri) {
        throw new Error("No JWKS Url found");
    }
    // This url can be cached.
    return new URL(metadata.jwks_uri);
}

async function verifyJwt(jwtToken) {
    // This set of keys can be cached.
    const JWKS = jose.createRemoteJWKSet(await loadJwksUrl());

    const { payload, protectedHeader } = await jose
        .jwtVerify(jwtToken, JWKS, {
            issuer: authOptions.Authority,
            audience: authOptions.Audience
        })
        .catch(async (error) => {
            if (error?.code === 'ERR_JWKS_MULTIPLE_MATCHING_KEYS') {
                for await (const publicKey of error) {
                    try {
                        return await jose.jwtVerify(token, publicKey, options)
                    } catch (innerError) {
                        if (innerError?.code === 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED') {
                            continue
                        }
                        throw innerError
                    }
                }
                throw new jose.errors.JWSSignatureVerificationFailed()
            }

            throw error
        });
    var foundScope = payload.scope.find(s => s === authOptions.RequiredScope);
    if (!foundScope) {
        throw Error("Scope not found");
    }


    // If we made it here without an error then we have a valid jwt where we have verified
    // 1. The JWT's signature using a key from the identity server
    // 2. The Audience is the expected value
    // 3. The issuer is the expected value.
    console.log("Protected Header", protectedHeader)
    console.log("Payload", payload)
}

verifyJwt(token);

.Net: Filevine Identity Signature Validation, Using .Net Security features

// Uses package Microsoft.AspNetCore.Authentication.JwtBearer

var authOptions = new
{
    Authority = "https://identity.filevine.com",
// For US Tenants. Canada is https://identity.filevine.ca and CJIS is https://identity.filevinegov.com
    Audience = "filevine-v2-webhooks",
    RequiredScope = "filevine-v2-webhooks-access",
};

builder.Services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.Authority = authOptions.Authority;
        options.Audience = authOptions.Audience;
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidIssuer = options.Authority,
            ValidAudience = options.Audience,
        };
        options.Events = new JwtBearerEvents()
        {
            OnTokenValidated = context =>
            {
                if (!context.Principal.HasClaim(c => c.Type == "scope" && c.Value == authOptions.RequiredScope))
                {
                   context.Fail($"Scope {authOptions.RequiredScope} is not present in the token."); 
                }
                return Task.CompletedTask;
            }
        };
    });

.Net: Filevine Identity Signature Validation, Manually

using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using IdentityModel;
using IdentityModel.Client;
using Microsoft.IdentityModel.Tokens;
using NUnit.Framework;

public class JwtValidationTests
{
    [SetUp]
    public void Setup()
    {
    }

    [Test]
    public async Task TesetIt()
    {
        var authorizationHeader =
        "Bearer ${JsonWebToken}";
        var jwt = authorizationHeader.Split(' ')[1];
        Assert.AreEqual(true, await ValidateJsonWebToken(jwt));
    }

    public async Task<bool> ValidateJsonWebToken(string token)
    {
        var authOptions = new
        {
            Authority = "https://identity.filevine.com",
// For US Tenants. Canada is https://identity.filevine.ca and CJIS is https://identity.filevinegov.com
            Audience = "filevine-v2-webhooks",
            RequiredScope = "filevine-v2-webhooks-access",
        };

        var client = new HttpClient();
        var disco = await client.GetDiscoveryDocumentAsync(authOptions.Authority);

        var keys = new List<SecurityKey>();
        foreach (var webKey in disco.KeySet.Keys)
        {
            var e = Base64Url.Decode(webKey.E);
            var n = Base64Url.Decode(webKey.N);

            var key = new RsaSecurityKey(new RSAParameters { Exponent = e, Modulus = n })
            {
                KeyId = webKey.Kid
            };

            keys.Add(key);
        }

        var parameters = new TokenValidationParameters
        {
            ValidIssuer = authOptions.Authority,
            ValidAudience = authOptions.Audience,
            RequireSignedTokens = true,
            IssuerSigningKeys = keys
        }; 

        var tokenHandler = new JwtSecurityTokenHandler();
        try
        {
            var claims = tokenHandler.ValidateToken(token, parameters, out var _);
            if (!claims.HasClaim(c => c.Type == "scope" && c.Value == authOptions.RequiredScope))
            {
                throw new Exception("Invalid scope");
            }

            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error validating token - message: ${e.Message}" );
            return false;
        } 
    }

}

Source: https://developer.filevine.io/docs/v2-us

More in Start here

Related pages