Sample code for 30+ languages & platforms
Zig

How to Generate an Azure Service Bus Shared Access Signature (SAS)

See more Azure Service Bus Examples

Demonstrates generating and using an Azure Service Bus Shared Access Signature (SAS).

Note: This example requires Chilkat v9.5.0.65 or greater.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Note: Requires Chilkat v9.5.0.65 or greater.

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // -------------------------------------------------------------------
    // Create a Shared Access Signature (SAS) token for Azure Service Bus.
    // -------------------------------------------------------------------

    const auth_sas = try chilkat.AuthAzureSAS.init();
    defer auth_sas.deinit();
    auth_sas.setAccessKey("AzureServiceBus_PrimaryKey");

    // The SAS token for Service Bus will look like this:
    // (The order of params will be different.  The order does not matter.)
    // sig=<signature-string>&se=<expiry>&skn=<keyName>&sr=<URL-encoded-resourceURI>

    // Specify the format of the string to sign.
    auth_sas.setStringToSign("resourceURI,expiry");

    // Create an expiry to 30 days in the future.
    const dt_expiry = try chilkat.DateTime.init();
    defer dt_expiry.deinit();
    dt_expiry.setFromCurrentSystemTime() catch {};
    dt_expiry.addDays(30) catch {};
    auth_sas.setTokenParam("expiry", "se", try dt_expiry.getAsUnixTimeStr(alloc, true)) catch {};

    // Set the skn (keyname)
    // This example uses the key "RootManageSharedAccessKey".  This give full access.
    // In a typical scenario, you would create a new Azure key (for the service bus)
    // in the Azure portal, such that the key has limited permissions.  This would
    // allow you to give the SAS token to others for specific access for some period of time.
    auth_sas.setTokenParam("keyName", "skn", "RootManageSharedAccessKey") catch {};

    // Set the URL-encoded-resourceURI
    const sb_resource_uri = try chilkat.StringBuilder.init();
    defer sb_resource_uri.deinit();
    sb_resource_uri.append("https://<yournamespace>.servicebus.windows.net/") catch {};
    sb_resource_uri.encode("url", "utf-8") catch {};
    auth_sas.setTokenParam("resourceURI", "sr", try sb_resource_uri.getAsString(alloc)) catch {};

    // Generate the SAS token.
    const sas_token = auth_sas.generateToken(alloc) catch {
        std.debug.print("{s}\n", .{try auth_sas.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("SAS token: {s}\n", .{sas_token});

    // Save the SAS token to a file.
    // We can then use this pre-generated token for future Service Bus operations.
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    fac.writeEntireTextFile("qa_data/tokens/serviceBusSas.txt", sas_token, "utf-8", false) catch {};
}