TokenBroker

Home / Setup Guide

Setup Guide

How to set up your own TokenBroker from scratch, following the exact path this one took on 2026-11-28. Times are honest: about an hour of provisioning and deployment, most of it waiting on builds and sign-ins, plus ten minutes per provider bootstrap.

TokenBroker is an Azure Function that owns OAuth token renewal for services whose access tokens expire quickly. Callers hold only a static function key; the function holds each provider's refresh token and client secret in Azure Key Vault, mints fresh access tokens, and proxies API calls. If your tools keep dying with 401s an hour after every reconnect, this is the fix.

What you need first

Step 1: Choose your names

Pick a resource group, a region, and globally unique names for the Function App, Key Vault, and storage account. Ours, as the worked example:

Thing Example
Resource group rg-tokenbroker
Region West US 2 (must be a Flex Consumption region)
Function App tokenbroker-example
Key Vault kv-tokenbroker-example
Storage account sttokenbrokerexample

The vault name and Function App name must be globally unique across Azure; add a short random suffix if yours are taken.

Step 2: Provision the Azure resources

Sign in with the MFA-capable flow if your tenant requires it, then:

az group create -n <rg> -l <region>
az storage account create -n <storage> -g <rg> -l <region> --sku Standard_LRS --allow-blob-public-access false
az keyvault create -n <vault> -g <rg> -l <region> --enable-rbac-authorization true
az functionapp create -n <app> -g <rg> --flexconsumption-location <region> --runtime dotnet-isolated --runtime-version 11.0 --functions-version 4 --storage-account <storage> --assign-identity [system]

Two choices here are forced, not taste:

Step 3: Let the function read the vault

The Function App's system-assigned managed identity needs read access to the vault, and you need write access for bootstraps and rotations:

az role assignment create --assignee <app-identity-principal> --role "Key Vault Secrets User" --scope <vault-resource-id>
az role assignment create --assignee <your-user-object-id> --role "Key Vault Secrets Officer" --scope <vault-resource-id>

RBAC changes can take a few minutes to propagate. If a vault read fails right after this step, wait and retry before debugging anything else.

Step 4: The config file

Configuration lives in files under src/TokenBroker, never in code and never in Azure app settings:

{ "KeyVaultUri": "https://<vault>.vault.azure.net/" }

It is copied into the deployment package when present, so the deployed function reads its config from the file. Environment variables (how Azure app settings arrive) still override file values if you ever need an override. Secrets are not configuration: they live in the vault (Step 3), and the only local exception is local.settings.json for vault-less local runs, also gitignored.

Step 5: Build and deploy

From src/TokenBroker, with az, the .NET SDK, and func on your PATH:

dotnet build -c Release
func azure functionapp publish <app> --dotnet-isolated

A minimal local.settings.json (gitignored) containing FUNCTIONS_WORKER_RUNTIME=dotnet-isolated must exist in that folder for runtime detection; its values are not uploaded.

Step 6: Verify the empty broker

Step 7: Add your first provider

The broker ships with Zoho wired up as the reference provider. To broker a provider of your own, follow Adding a Provider: register a dedicated OAuth client, put its id and secret in the vault, write two small classes, deploy, run the one-time grant-code bootstrap, and test one call end to end.

Running it locally

Copy src/TokenBroker/local.settings.json.example to local.settings.json and either point KeyVaultUri at a vault your sign-in can read, or leave it empty and supply each provider's three values as plain settings (<Prefix>ClientId, <Prefix>ClientSecret, <Prefix>RefreshToken). Never commit local.settings.json.

Ground rules that keep this safe