public static PopSession CreateSession(IPopSessionProfile profile, SaslClientMechanism authMechanismSpecified, UpgradeConnectionStreamCallback createSslStreamCallback) { PopSession session; var result = CreateSession(profile, authMechanismSpecified, createSslStreamCallback, out session); if (result.Succeeded) return session; else throw new PopAuthenticationException(result); }
public static PopCommandResult CreateSession(IPopSessionProfile profile, SaslClientMechanism authMechanismSpecified, UpgradeConnectionStreamCallback createSslStreamCallback, out PopSession session) { if (profile == null) throw new ArgumentNullException("profile"); var authority = profile.Authority; var securePort = string.Equals(authority.Scheme, PopUri.UriSchemePops, StringComparison.OrdinalIgnoreCase); if (securePort && createSslStreamCallback == null) throw new ArgumentNullException("createSslStreamCallback"); PopCommandResult result; session = null; session = new PopSession(authority.Host, authority.Port, profile.Timeout, securePort ? createSslStreamCallback : null); session.HandlesIncapableAsException = false; session.TransactionTimeout = profile.Timeout; session.SendTimeout = profile.SendTimeout; session.ReceiveTimeout = profile.ReceiveTimeout; // try querying server capability (ignore error; POP3 Extension Mechanism might not supported) session.Capa(); if (profile.UseTlsIfAvailable && session.ServerCapabilities.IsCapable(PopCapability.Stls) && !session.IsSecureConnection) { var r = session.Stls(createSslStreamCallback, false); if (r.Failed) throw new PopUpgradeConnectionException(r.ResultText); // try re-querying server capability (ignore error; POP3 Extension Mechanism might not supported) session.Capa(); } if (authMechanismSpecified == null) result = Authenticate(session, profile); else result = session.Auth(authMechanismSpecified); if (result == null) { throw new PopAuthenticationException("appropriate authentication mechanism not found"); } else if (result.Failed) { try { try { session.Disconnect(false); } catch (PopConnectionException) { // ignore } } finally { session = null; } } return result; }
private static PopCommandResult Authenticate(PopSession session, IPopSessionProfile profile) { var authority = profile.Authority; var username = PopStyleUriParser.GetUser(authority); var authMechanism = PopStyleUriParser.GetAuthType(authority); /* * http://tools.ietf.org/html/rfc2384 * 4. POP User Name and Authentication Mechanism * * The string ";AUTH=*" indicates that the client SHOULD select an * appropriate authentication mechanism. It MAY use any mechanism * supported by the POP server. * * If a user name is included with no authentication mechanism, then * ";AUTH=*" is assumed. */ var canFallback = false; if (authMechanism == null) { if (string.IsNullOrEmpty(username)) { authMechanism = PopAuthenticationMechanism.Anonymous; canFallback = true; } else { authMechanism = PopAuthenticationMechanism.SelectAppropriate; } } if (authMechanism == PopAuthenticationMechanism.SelectAppropriate) { var allowInsecureMechanism = session.IsSecureConnection || profile.AllowInsecureLogin; return AuthenticateWithAppropriateMechanism(session, allowInsecureMechanism, profile.Credentials, username, GetUsingSaslMechanisms(profile.UsingSaslMechanisms ?? new string[0])); } else if (authMechanism == PopAuthenticationMechanism.Anonymous) { return AuthenticateAsAnonymous(session, username, canFallback); } else { return AuthenticateWithSuppliedMechanism(session, profile.Credentials, username, authMechanism); } }
internal static PopSession CreateSession(IPopSessionProfile profile) { try { PopSession session = null; var result = PopSessionCreator.CreateSession(profile, null, createSslStreamCallback, out session); if (result.Succeeded) return session; else throw new WebException(result.ResultText, null, WebExceptionStatus.ProtocolError, new PopWebResponse(result)); } catch (PopUpgradeConnectionException ex) { throw new WebException(ex.Message, ex.InnerException, WebExceptionStatus.SecureChannelFailure, null); } catch (PopConnectionException ex) { throw new WebException("connection error", ex, WebExceptionStatus.ConnectFailure, null); } catch (PopException ex) { if (ex.InnerException == null) throw new WebException(ex.Message, ex, WebExceptionStatus.RequestCanceled, null); else throw new WebException("internal error", ex, WebExceptionStatus.UnknownError, null); } catch (TimeoutException ex) { throw new WebException("timed out", ex, WebExceptionStatus.Timeout, null); } catch (WebException) { throw; } }