Skip to content

Commit

Permalink
Added support for the SCRAM-SHA*-PLUS SASL mechanisms
Browse files Browse the repository at this point in the history
Fixes issue #950
  • Loading branch information
jstedfast committed Sep 4, 2021
1 parent a0f8416 commit 79ade35
Show file tree
Hide file tree
Showing 27 changed files with 664 additions and 156 deletions.
12 changes: 8 additions & 4 deletions MailKit/Net/Imap/ImapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -972,12 +972,12 @@ async Task AuthenticateAsync (SaslMechanism mechanism, bool doAsync, Cancellatio

int capabilitiesVersion = engine.CapabilitiesVersion;
var uri = new Uri ("imap://" + engine.Uri.Host);
NetworkCredential cred;
ImapCommand ic = null;
string id;

cancellationToken.ThrowIfCancellationRequested ();

mechanism.TransportContext = (engine.Stream.Stream as SslStream)?.TransportContext;
mechanism.Uri = uri;

var command = string.Format ("AUTHENTICATE {0}", mechanism.MechanismName);
Expand Down Expand Up @@ -1019,8 +1019,7 @@ async Task AuthenticateAsync (SaslMechanism mechanism, bool doAsync, Cancellatio

engine.State = ImapEngineState.Authenticated;

cred = mechanism.Credentials.GetCredential (mechanism.Uri, mechanism.MechanismName);
id = GetSessionIdentifier (cred.UserName);
id = GetSessionIdentifier (mechanism.Credentials.UserName);
if (id != identifier) {
engine.FolderCache.Clear ();
identifier = id;
Expand Down Expand Up @@ -1105,9 +1104,14 @@ async Task AuthenticateAsync (Encoding encoding, ICredentials credentials, bool
if (!engine.AuthenticationMechanisms.Contains (authmech))
continue;

if ((sasl = SaslMechanism.Create (authmech, uri, encoding, credentials)) == null)
cred = credentials.GetCredential (uri, authmech);

if ((sasl = SaslMechanism.Create (authmech, encoding, cred)) == null)
continue;

sasl.TransportContext = (engine.Stream.Stream as SslStream)?.TransportContext;
sasl.Uri = uri;

cancellationToken.ThrowIfCancellationRequested ();

var command = string.Format ("AUTHENTICATE {0}", sasl.MechanismName);
Expand Down
8 changes: 7 additions & 1 deletion MailKit/Net/Pop3/Pop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ async Task AuthenticateAsync (SaslMechanism mechanism, bool doAsync, Cancellatio

cancellationToken.ThrowIfCancellationRequested ();

mechanism.TransportContext = (engine.Stream.Stream as SslStream)?.TransportContext;
mechanism.Uri = new Uri ("pop://" + engine.Uri.Host);

var ctx = new SaslAuthContext (this, mechanism);
Expand Down Expand Up @@ -836,9 +837,14 @@ async Task AuthenticateAsync (Encoding encoding, ICredentials credentials, bool
if (!engine.AuthenticationMechanisms.Contains (authmech))
continue;

if ((sasl = SaslMechanism.Create (authmech, saslUri, encoding, credentials)) == null)
cred = credentials.GetCredential (saslUri, authmech);

if ((sasl = SaslMechanism.Create (authmech, encoding, cred)) == null)
continue;

sasl.TransportContext = (engine.Stream.Stream as SslStream)?.TransportContext;
sasl.Uri = saslUri;

cancellationToken.ThrowIfCancellationRequested ();

var ctx = new SaslAuthContext (this, sasl);
Expand Down
8 changes: 7 additions & 1 deletion MailKit/Net/Smtp/SmtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ async Task AuthenticateAsync (SaslMechanism mechanism, bool doAsync, Cancellatio
string challenge;
string command;

mechanism.TransportContext = (Stream.Stream as SslStream)?.TransportContext;
mechanism.Uri = new Uri ($"smtp://{uri.Host}");

// send an initial challenge if the mechanism supports it
Expand Down Expand Up @@ -901,9 +902,14 @@ async Task AuthenticateAsync (Encoding encoding, ICredentials credentials, bool
if (!AuthenticationMechanisms.Contains (authmech))
continue;

if ((sasl = SaslMechanism.Create (authmech, saslUri, encoding, credentials)) == null)
var cred = credentials.GetCredential (uri, authmech);

if ((sasl = SaslMechanism.Create (authmech, encoding, cred)) == null)
continue;

sasl.TransportContext = (Stream.Stream as SslStream)?.TransportContext;
sasl.Uri = saslUri;

tried = true;

cancellationToken.ThrowIfCancellationRequested ();
Expand Down
Loading

0 comments on commit 79ade35

Please sign in to comment.