Skip to content
Jennifer Programming Language

sasl - SASL authentication encoders

Import with import "sasl.j" as sasl;. The SASL authentication mechanisms shared by the mail clients (smtp / pop / imap). These format the client tokens; the protocol clients run the mechanism-specific wire dialogue around them. The simple encoders are pure base64; the challenge-response mechanisms (cram, SCRAM) key off the hash / crypto libraries. No networking, and every dependency is TinyGo-clean, so the module runs on either binary.

jennifer
import "sasl.j" as sasl;

def accessToken as string init "ya29.a0AfH6SM...";                 # an OAuth2 access token
def p as string init sasl.plain("me@example.com", "secret");     # SASL PLAIN
def b as string init sasl.bearer("me@gmail.com", $accessToken);  # SASL XOAUTH2

Runnable: examples/modules/sasl_demo.j (and it is exercised end to end by the mail clients' demos, e.g. smtp_demo.j).

Surface

CallReturnsNotes
sasl.plain(user, pass)stringSASL PLAIN: base64 of "\0user\0pass".
sasl.loginUser(user)stringSASL LOGIN step 1: base64 of the username.
sasl.loginPass(pass)stringSASL LOGIN step 2: base64 of the password.
sasl.bearer(user, token)stringSASL XOAUTH2: an OAuth2 bearer-token response.
sasl.cram(user, pass, challenge)stringCRAM-MD5 (RFC 2195): one-step HMAC-MD5 response to a base64 challenge.
sasl.scramStart(algo, user)ScramBegin a SCRAM exchange; algo "sha1", "sha256" or "sha512" (anything else is an Error{kind: "sasl"}). Crypto-grade nonce.
sasl.scramClientFirst(s)stringThe base64 client-first message to send.
sasl.scramClientFinal(s, serverFirst, pass)ScramProcess server-first; derive the proof + expected server signature.
sasl.scramFinalToken(s)stringThe base64 client-final message to send.
sasl.scramVerify(s, serverFinal)boolVerify the server's signature (constant-time); reject on false.
sasl.negotiate(advertised)stringPick the strongest password mechanism from a server's advertised list, as an auth token (see below).

Mechanism negotiation

sasl.negotiate(advertised) takes the mechanism names a server advertises (a list of string, any case) and returns the strongest one this module supports as a mail-client auth token: "scram-sha-256" > "scram-sha-1" > "cram", or "" when the server offers none (the caller then uses its protocol default - PLAIN / LOGIN / USER-PASS). XOAUTH2 is never auto-selected (it needs a bearer token, not a password). The mail clients call this when Options.auth is "auto", feeding it the mechanisms parsed from the SMTP EHLO, POP3 CAPA, or IMAP CAPABILITY advertisement ("" keeps their plain default).

SCRAM (RFC 5802 / RFC 7677)

SCRAM is a salted challenge-response: the password never crosses the wire, and both sides prove knowledge of it. The exchange threads a value-semantic Scram handle through four calls (each returns an updated copy), so a client drives it against the SASL AUTHENTICATE / AUTH loop:

jennifer
import "sasl.j" as sasl;

def s as sasl.Scram init sasl.scramStart("sha256", "user");
# 1. send sasl.scramClientFirst($s); read the base64 server-first reply
def serverFirst as string init "...";
$s = sasl.scramClientFinal($s, $serverFirst, "pencil");
# 2. send sasl.scramFinalToken($s); read the base64 server-final reply
def serverFinal as string init "...";
if (not sasl.scramVerify($s, $serverFinal)) {
    # the server does not know the password - abort, do not trust the session
}

scramClientFinal runs PBKDF2 in the mechanism's hash (crypto.deriveKey), so SCRAM-SHA-1 (MongoDB, XMPP), SCRAM-SHA-256 (PostgreSQL, modern mail) and SCRAM-SHA-512 all work. scramVerify compares the server signature with hash.equal (constant-time). The wire strings are the base64 SASL tokens; the raw SCRAM messages are inside.

scramClientFinal throws kind sasl when the server-first carries a mandatory extension (m=, which RFC 5802 says a client must fail on), the server nonce does not strictly extend the client nonce, the salt is empty, or the iteration count is outside 4096..1000000. The server chooses the count: the floor is RFC 7677's minimum (a lower count is a downgrade that makes a captured exchange cheap to brute-force), and the cap stops a hostile server from stalling the client in PBKDF2. Usernames are escaped (=, ,); SASLprep is not applied (see Out of scope).

sasl.cram(user, pass, challenge) is the simpler one-step CRAM-MD5: the server sends a base64 challenge, the client answers base64("user " + hex(HMAC-MD5)).

XOAUTH2 (the "use a token" half of OAuth2)

sasl.bearer(user, token) builds the SASL XOAUTH2 initial response - base64("user=" user 0x01 "auth=Bearer " token 0x01 0x01) - which is how Google and Microsoft 365 authenticate mail (both have retired password auth). The mail clients accept it via Options.auth = "xoauth2" (with the access token in pass):

jennifer
import "smtp.j" as smtp;
import "transport.j" as transport;

def accessToken as string init "ya29.a0AfH6SM...";   # from your OAuth2 flow
def message as string init "Subject: hi\r\n\r\nHello.\r\n";
def opts as smtp.Options init smtp.Options{host: "smtp.gmail.com", port: 587,
    security: transport.Security.Starttls, clientName: "me.example",
    user: "me@gmail.com",
    pass: $accessToken, auth: "xoauth2"};
smtp.send($opts, "me@gmail.com", ["you@example.com"], $message);

The function is named bearer (after the auth=Bearer token it carries); the wire mechanism name "XOAUTH2" is a string the client sends. Getting the token itself is the other half of OAuth2 - the job of the generic oauth client.

Out of scope

  • Encoders, not transport. sasl formats tokens and drives the SCRAM state machine; the SMTP AUTH, IMAP AUTHENTICATE, and POP3 AUTH line dialogue lives in the respective clients.
  • No SASLprep. SASLprep (RFC 4013, which RFC 5802 asks SCRAM to apply) is not applied: usernames / passwords are used as given (only SCRAM's = / , username escaping is applied), so a non-ASCII credential must already be in the form the server stored; normalize it yourself (NFKC for most servers).

See also

API reference

Every exported function, struct and constant of sasl, with its signature, generated from the module's doc comments: the sasl API reference.