Skip to content

Support Entra ID access token login (FEDAUTH SECURITYTOKEN) - #195

Open
forge-hickory wants to merge 4 commits into
elixir-ecto:masterfrom
hickoryco:fedauth-security-token
Open

forge-hickory wants to merge 4 commits into
elixir-ecto:masterfrom
hickoryco:fedauth-security-token

Conversation

@forge-hickory

@forge-hickory forge-hickory commented Nov 3, 2026 •

Copy link
Copy Markdown

Adds Microsoft Entra ID (Azure AD) access token login, so a connection to Azure SQL Database or Azure SQL Managed Instance can authenticate without a SQL login:

Tds.start_link(
  hostname: host,
  database: "db",
  ssl: true,
  ssl_opts: [verify: :verify_peer, cacerts: :public_key.cacerts_get(), server_name_indication: String.to_charlist(host), customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)]],
  configure: fn opts -> Keyword.put(opts, :access_token, fetch_token()) end
)

The option is read from the connect options on each connect. A pooled caller supplies a fresh token per connection through DBConnection's :configure. Tokens expire, so the README recommends that pattern.

Protocol (MS-TDS)

  • PRELOGIN. The client already sends FEDAUTHREQUIRED (0x06) = 0x01. The server's B_FEDAUTHREQUIRED answer and any NONCE (0x07) are now decoded and kept in connection state, then handed to Login7.new/2.
  • LOGIN7. With :access_token:
    • OptionFlags3.fExtension (0x10) is set.
    • ibExtension/cbExtension (the former ibUnused slot) point at a DWORD after the database field. That DWORD holds the offset of the FeatureExt block that follows it.
    • The FeatureExt block carries FEDAUTH (0x02) with bOptions = bFedAuthLibrary SECURITYTOKEN (0x01) in bits 7..1, fFedAuthEcho in bit 0. Then come FedAuthToken (DWORD byte length + UTF-16LE token), the server's 32-byte nonce when it sent one, and the 0xFF terminator.
    • UserName and Password are sent empty.
    • An empty database is sent with a zero length. The existing 0xAC length would run that field over the extension data.
    • A LOGIN7 larger than one packet is split by the existing encode_packets/3. A typical JWT is ~4 KB as UTF-16.
  • FEATUREEXTACK (0xAE) is decoded into msg_loginack's feature_acks. A token login that gets LOGINACK without a FEDAUTH acknowledgement fails, as MS-TDS requires the server to send one.

Without :access_token, the PRELOGIN and LOGIN7 bytes are unchanged. The tests pin both.

Option checks (in connect/1, before any socket opens)

:access_token must be a non-empty UTF-8 string. It cannot be combined with :password, and it needs ssl: true / :on / :required, so LOGIN7 always travels encrypted. With ssl_opts: [verify: :verify_none], a warning is logged. No error message includes the token.

Credentials in state

  • :access_token is redacted alongside :password when options are cleaned after login.
  • Tds.Protocol gets an Inspect implementation that redacts both, since state holds them during login and is logged by handle_info/2 and DBConnection crash reports.
  • Tds.Protocol.Login7 derives Inspect without :password and :access_token.

Routing redirect fix

When the reconnect to a server named by a routing ENVCHANGE failed, the caller crashed: connect/1's {:error, e} reached a case that expects a 3-tuple. It now returns a connection error. A successful redirect closes the first-hop socket. The reconnect goes through the internal connect, because the options it carries already hold the environment defaults.

Tests

test/fedauth_test.exs, 26 tests, no server needed:

  • golden PRELOGIN bytes with and without a token (identical)
  • golden LOGIN7 bytes for SQL authentication (unchanged) and for access_token: "tkn"
  • the echo bit, nonce echo, the empty-database length, and multi-packet splitting
  • PRELOGIN response decoding
  • every option refusal
  • FEATUREEXTACK before and after LOGINACK
  • a login refusal (18456) that keeps the token out of Tds.Error and inspect/1
  • against a local TCP peer: the PRELOGIN-to-LOGIN7 echo hand-off, redaction once logged in, a LOGINACK without the FEDAUTH ack, and a routing redirect

The existing suite passes on all CI legs (OTP 24/25, Elixir 1.14/1.18, SQL Server 2017/2025, Azure SQL Edge).

Against SQL Server / Azure SQL Edge, a token login with a ~3,000-character token went out as a 3-packet LOGIN7. The server parsed it and refused it with error 18456, which surfaced as a Tds.Error. On-premises SQL Server has no Entra authentication, so this checks framing only. A successful FEDAUTH login against Azure SQL has not been exercised.

Not included: the ADAL/MSAL FEDAUTH workflow (FEDAUTHINFO, 0xEE). Acquiring the token is left to the caller.

Forge and others added 4 commits November 3, 2026 11:48
Add an `:access_token` connect option. When set, LOGIN7 sets fExtension
and carries a FEDAUTH FeatureExt block (MS-TDS 2.2.6.4) with
bFedAuthLibrary = SECURITYTOKEN, the UTF-16LE token behind a DWORD
length, and empty username and password.

- Prelogin: decode the server's FEDAUTHREQUIRED answer and NONCE so
  LOGIN7 can echo them (fFedAuthEcho, Nonce). Refuse to log in with an
  access token over a connection that negotiated no encryption.
- Login7: write ibExtension/cbExtension and the FeatureExt block. The
  bytes produced without `:access_token` are unchanged.
- Tokens: decode FEATUREEXTACK (0xAE) instead of raising on it.
- Redact `:access_token` alongside `:password` in cleaned options, and
  in `inspect/1` of `Tds.Protocol` and `Tds.Protocol.Login7`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Validate `:access_token` once in `Tds.Protocol.connect/1`, before any
  socket opens: it must be a non-empty binary and needs `ssl: true`.
- Carry the PRELOGIN FEDAUTH answer to `Login7.new/2` as its own
  argument instead of through the connect options.
- Place the extension DWORD after the database field so no splice is
  needed; use the `dword()` grammar and a bitstring for bOptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Fail the login when the server sends LOGINACK without acknowledging
  FEDAUTH in FEATUREEXTACK.
- Refuse :access_token together with :password, or when it is not valid
  UTF-8, without echoing it; warn when the server certificate is not
  verified.
- Return a connection error, instead of crashing, when reconnecting to
  the server named by a routing ENVCHANGE fails.
- Test the PRELOGIN-to-LOGIN7 echo, redaction after login, the missing
  acknowledgement and the redirect against a local TCP peer.
- README: a verified TLS configuration, :configure for token refresh,
  and why the Proxy connection policy is needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A routing redirect reconnects with options that already carry the
MSSQL* environment defaults, so it now skips connect/1's validation of
the caller's options, which would refuse an environment password
alongside the token. The first-hop socket is closed once the redirected
connection is up. The verify_none warning is logged once per connect,
outside validation, and the FEDAUTH feature id has one definition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant