Repository navigation
Support Entra ID access token login (FEDAUTH SECURITYTOKEN) - #195
Open
forge-hickory wants to merge 4 commits into
Open
forge-hickory wants to merge 4 commits into
forge-hickory wants to merge 4 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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)
FEDAUTHREQUIRED(0x06) = 0x01. The server'sB_FEDAUTHREQUIREDanswer and anyNONCE(0x07) are now decoded and kept in connection state, then handed toLogin7.new/2.:access_token:OptionFlags3.fExtension(0x10) is set.ibExtension/cbExtension(the formeribUnusedslot) point at a DWORD after the database field. That DWORD holds the offset of the FeatureExt block that follows it.FEDAUTH(0x02) withbOptions = bFedAuthLibrary SECURITYTOKEN (0x01) in bits 7..1, fFedAuthEcho in bit 0. Then comeFedAuthToken(DWORD byte length + UTF-16LE token), the server's 32-byte nonce when it sent one, and the 0xFF terminator.encode_packets/3. A typical JWT is ~4 KB as UTF-16.msg_loginack'sfeature_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_tokenmust be a non-empty UTF-8 string. It cannot be combined with:password, and it needsssl: true/:on/:required, so LOGIN7 always travels encrypted. Withssl_opts: [verify: :verify_none], a warning is logged. No error message includes the token.Credentials in state
:access_tokenis redacted alongside:passwordwhen options are cleaned after login.Tds.Protocolgets anInspectimplementation that redacts both, since state holds them during login and is logged byhandle_info/2and DBConnection crash reports.Tds.Protocol.Login7derivesInspectwithout:passwordand:access_token.Routing redirect fix
When the reconnect to a server named by a routing
ENVCHANGEfailed, the caller crashed:connect/1's{:error, e}reached acasethat 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:access_token: "tkn"Tds.Errorandinspect/1The 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.