Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoAuthSas
    Handle hoDtExpiry
    Boolean iSuccess
    Handle hoSbResourceUri
    String sSasToken
    Handle hoFac
    String sTemp1
    Boolean bTemp1

    // 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.
    // -------------------------------------------------------------------

    Get Create (RefClass(cComChilkatAuthAzureSAS)) To hoAuthSas
    If (Not(IsComObjectCreated(hoAuthSas))) Begin
        Send CreateComObject of hoAuthSas
    End
    Set ComAccessKey Of hoAuthSas To "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.
    Set ComStringToSign Of hoAuthSas To "resourceURI,expiry"

    // Create an expiry to 30 days in the future.
    Get Create (RefClass(cComCkDateTime)) To hoDtExpiry
    If (Not(IsComObjectCreated(hoDtExpiry))) Begin
        Send CreateComObject of hoDtExpiry
    End
    Get ComSetFromCurrentSystemTime Of hoDtExpiry To iSuccess
    Get ComAddDays Of hoDtExpiry 30 To iSuccess
    Get ComGetAsUnixTimeStr Of hoDtExpiry True To sTemp1
    Get ComSetTokenParam Of hoAuthSas "expiry" "se" sTemp1 To iSuccess

    // 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.
    Get ComSetTokenParam Of hoAuthSas "keyName" "skn" "RootManageSharedAccessKey" To iSuccess

    // Set the URL-encoded-resourceURI
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResourceUri
    If (Not(IsComObjectCreated(hoSbResourceUri))) Begin
        Send CreateComObject of hoSbResourceUri
    End
    Get ComAppend Of hoSbResourceUri "https://<yournamespace>.servicebus.windows.net/" To iSuccess
    Get ComEncode Of hoSbResourceUri "url" "utf-8" To iSuccess
    Get ComGetAsString Of hoSbResourceUri To sTemp1
    Get ComSetTokenParam Of hoAuthSas "resourceURI" "sr" sTemp1 To iSuccess

    // Generate the SAS token.
    Get ComGenerateToken Of hoAuthSas To sSasToken
    Get ComLastMethodSuccess Of hoAuthSas To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoAuthSas To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "SAS token: " sSasToken

    // Save the SAS token to a file.
    // We can then use this pre-generated token for future Service Bus operations.
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get ComWriteEntireTextFile Of hoFac "qa_data/tokens/serviceBusSas.txt" sSasToken "utf-8" False To iSuccess


End_Procedure