Skip to content

Credential access

Mechanism

Actian reads the user's credential with Extension.CurrentCredential() and selects a branch from its optional AuthenticationKind field. For UsernamePassword it builds a separate record, [ UID = Credential[Username], PWD = Credential[Password] ]. For Windows it returns null; the SQL Server-only Trusted_Connection record is commented out. Any other value, including a missing one, reaches the final Error.Record branch.1

The result is passed as CredentialConnectionString to Odbc.DataSource, next to the connection string. That connection string is either a caller-supplied DSN= value or a record of Driver, Server and Database (lines 102–109).2 The source comment says credentials "are not persisted with the query and are set through a separate record field" (lines 112–113). That comment states the intent. This file does not show how the ODBC layer consumes the field.1

Config_DefaultUsernamePasswordHandling = true (line 50) describes automatic UID and PWD handling by the base function. No code in Actian.m reads that name, so the explicit branch runs whatever its value is. Do not treat it as a switch.1

Anaplan selects on a different key. Anaplan.Connect checks Record.HasFields(credential, "Username") and otherwise reads Extension.CurrentCredential()[access_token] (lines 316–332). That fits a connector that offers OAuth and UsernamePassword and tells them apart by the fields present, not by a kind label.3

Choosing this shape

Use AuthenticationKind dispatch when the connector declares a fixed set of kinds and each kind has its own branch, as Actian does. Use field-presence dispatch, as Anaplan does, when the credential records differ in structure. For token-based sources, use the OAuth2 pattern. For driver configuration, see ODBC configuration.

Adaptation and failure paths

Start from the registration, not from a branch. Actian declares Windows and UsernamePassword (lines 260–263). Keep that list and the branches identical. A registered kind with no branch falls through to the final error.1

A missing kind reaches the final error: the optional lookup produces null. M's text-concatenation rule makes the constructed message expression null as well. When adapting, provide a readable fallback for a missing kind while preserving the intended failure.1

Anaplan's fallback at line 328 passes Extension.CurrentCredential(true) where GetApiToken expects a text URL (line 329). Do not copy that expression. This file does not document its argument.3

Keep Credential[Password], the connection string, and the raw credential record out of traces and error messages. The serializer in Actian's diagnostics helper writes each record field's value without masking.4

See diagnostics for trace wrapping and structured errors for error records.

Examples

Actian/Actian.m · lines 111–127 Open original source ↗

        // Handle credentials
        // Credentials are not persisted with the query and are set through a separate 
        // record field - CredentialConnectionString. The base Odbc.DataSource function
        // will handle UsernamePassword authentication automatically, but it is explictly
        // handled here as an example. 
        //
        Credential = Extension.CurrentCredential(),
        CredentialConnectionString =
            if Credential[AuthenticationKind]? = "UsernamePassword" then
                // set connection string parameters used for basic authentication
                [ UID = Credential[Username], PWD = Credential[Password] ]
            else if (Credential[AuthenticationKind]? = "Windows") then
                // set connection string parameters used for windows/kerberos authentication
                //[ Trusted_Connection="Yes" ]    SQLServer only
                null
            else
                error Error.Record("Error", "Unhandled authentication kind: " & Credential[AuthenticationKind]?),

The consumer passes the binding to the ODBC data source function:

Actian/Actian.m · lines 218–239 Open original source ↗

        OdbcDatasource = Odbc.DataSource(ConnectionString, [
            // A logical (true/false) that sets whether to view the tables grouped by their schema names
            HierarchicalNavigation = true, 
            // Prevents execution of native SQL statements. Extensions should set this to true.
            HideNativeQuery = true,
            // Allows upconversion of numeric types
            SoftNumbers = true,
            // Allow upconversion / resizing of numeric and string types
            TolerateConcatOverflow = true,
            // Enables connection pooling via the system ODBC manager
            ClientConnectionPooling = true,
            // Turn off sending SQL Server compatible connection string for Windows auth;
            // namely, "Trusted_Connection=Yes" (not used by our ODBC driver).
            SqlCompatibleWindowsAuth = false,
            // These values should be set by previous steps
            CredentialConnectionString = CredentialConnectionString,
            //AstVisitor = AstVisitor,
            SqlCapabilities = SqlCapabilities,
            SQLColumns = SQLColumns,
            SQLGetInfo = SQLGetInfo,
            SQLGetTypeInfo = SQLGetTypeInfo
        ])

The Anaplan variant selects by field presence:

Anaplan/Anaplan.pq · lines 316–335 Open original source ↗

Anaplan.Connect = (apiUrl as text, authUrl as text) as table =>
    let 
        // Create trace context ONCE for entire session
        traceContext = TraceContext[CreateContext]("PowerBi"),
        trimmedApiUrl = Text.Trim(apiUrl),
        trimmedAuthUrl = Text.Trim(authUrl),
        credential = Extension.CurrentCredential() as record,
        configParams = if Record.HasFields(credential, "Username") then
          let
              apiUrl = if trimmedApiUrl <> null and IsValidUrl(trimmedApiUrl) then (trimmedApiUrl & Extension.LoadString("APIVersionPath")) else prodApiHost & Extension.LoadString("APIVersionPath"),
              authUrl = if trimmedAuthUrl <> null and IsValidUrl(trimmedAuthUrl) then trimmedAuthUrl else prodAuthHost,
              apiTokenTry = GetApiToken(authUrl, false),
              authUrlValidated = if apiTokenTry <> null then authUrl else if GetApiToken(prodAuthHost, false) <> null then prodAuthHost else Extension.CurrentCredential(true),
              apiToken = GetApiToken(authUrlValidated, false)
          in
              List.Buffer({apiToken[tokenValue], apiUrl, authUrl, traceContext})
        else List.Buffer({Extension.CurrentCredential()[access_token], trimmedApiUrl & Extension.LoadString("APIVersionPath"), "", traceContext}),
        source = GetWorkspacesTable(configParams)
    in 
        source as table;

  1. Actian/Actian.m lines 111–127 hold the credential binding; lines 42–50 hold the Config_DefaultUsernamePasswordHandling comment; lines 260–263 register Windows and UsernamePassword. ↩↩↩↩↩

  2. Actian/Actian.m lines 102–109 build the connection string; lines 218–239 pass CredentialConnectionString to Odbc.DataSource, with SqlCompatibleWindowsAuth = false at line 231. ↩

  3. Anaplan/Anaplan.pq lines 316–335 hold Anaplan.Connect, its Username check, and the line 328 fallback; lines 226–233 register OAuth and UsernamePassword. ↩↩

  4. Actian/Diagnostics.pqm lines 101–108 define Serialize.Record, which serializes each field value; no masking branch appears in this file. ↩