Protect integration secrets in AL

Completed

An API solution can handle credentials in two directions. External callers authenticate to Business Central with OAuth 2.0. The extension might also need a credential when it calls another service. Don't place either credential in AL source, a request file committed to source control, or a regular table field.

Choose an isolated-storage scope

Isolated storage keeps key-value data private to an extension. A key in one extension isn't available to another extension. DataScope adds boundaries inside the extension.

Scope Use when the value belongs to
Module The extension across users and companies.
Company One company, shared by authorized users of the extension.
User One user across companies.
CompanyAndUser One user in one company.

Select the narrowest scope that matches ownership. A shared fulfillment credential that differs by company belongs in Company scope. A user-delegated token can require CompanyAndUser scope. Retrieval, replacement, and deletion must use the same key and scope.

Isolated storage doesn't replace access control. Limit who can run setup code that stores or deletes a value, and grant only the permissions needed to consume it.

Keep the value as SecretText

SecretText prevents assignment back to ordinary Text and hides the value from normal debugger inspection. The protection should cover retrieval, transit, and consumption.

codeunit 71303 "Integration Secret Store"
{
    [NonDebuggable]
    procedure StoreCredential(CredentialText: Text)
    var
        Credential: SecretText;
    begin
        if CredentialText = '' then
            Error('Credential must contain a value.');

        Credential := CredentialText;

        if IsolatedStorage.Contains(CredentialKeyLbl, DataScope::Company) then
            IsolatedStorage.Delete(CredentialKeyLbl, DataScope::Company);

        if not IsolatedStorage.Set(
            CredentialKeyLbl, Credential, DataScope::Company)
        then
            Error('The credential could not be stored.');
    end;

    procedure GetCredential() Credential: SecretText
    begin
        if not IsolatedStorage.Get(
            CredentialKeyLbl, DataScope::Company, Credential)
        then
            Error('No integration credential is configured for this company.');
    end;

    var
        CredentialKeyLbl: Label 'IntegrationCredential', Locked = true;
}

The setter accepts Text only at the input boundary and immediately converts it inside a [NonDebuggable] procedure. The getter returns SecretText; callers can't assign that result to plain text.

Consume, replace, and delete the secret

HTTP types provide overloads that accept SecretText. Build an authorization header without unwrapping the credential.

procedure AddAuthorizationHeader(var Headers: HttpHeaders)
var
    Credential: SecretText;
    AuthorizationValue: SecretText;
begin
    Credential := GetCredential();
    AuthorizationValue := SecretStrSubstNo('Bearer %1', Credential);
    Headers.Add('Authorization', AuthorizationValue);
end;

Don't include a secret in an error, telemetry dimension, Message, or ordinary text variable. A masked page control reduces display exposure during setup, but the receiving variable is still plain text until the nondebuggable procedure converts it.

Plan the credential lifecycle:

  1. Store the value under a stable key and chosen scope.
  2. Retrieve it as SecretText only when an operation needs it.
  3. Replace it by writing the new protected value under the same key and scope.
  4. Delete it with IsolatedStorage.Delete when the integration is disconnected.
  5. Report only whether a value exists, not the value itself.

Secret storage for an extension's outbound integration doesn't manage OAuth tokens used by external clients to call Business Central. Client applications obtain those tokens from Microsoft Entra ID and send them in the request's Authorization header.

For current APIs and constraints, see Isolated storage and Protect sensitive values with SecretText.