Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/107 no auth connect #126

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/SwiftSMTP/AuthMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public enum AuthMethod: String {
case plain = "PLAIN"
/// XOAUTH2 authentication. Requires a valid access token.
case xoauth2 = "XOAUTH2"
/// No authentication at all
case none = "NONE"
}
1 change: 1 addition & 0 deletions Sources/SwiftSMTP/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum Command {
case .login: return [.containingChallenge]
case .plain: return [.authSucceeded]
case .xoauth2: return [.authSucceeded]
case .none: return [.commandOK]
}
case .authUser: return [.containingChallenge]
case .authPassword: return [.authSucceeded]
Expand Down
33 changes: 33 additions & 0 deletions Sources/SwiftSMTP/SMTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ public struct SMTP {
self.timeout = timeout
}

/// Initializes an `SMTP` instance, specifically for use when communicating with an SMTP
/// host which does not perform authentication.
///
/// - Parameters:
/// - hostname: Hostname of the SMTP server to connect to, i.e. `smtp.example.com`.
/// - port: Port to connect to the server on. Defaults to `465`.
/// - tlsMode: TLSMode `enum` indicating what form of connection security to use.
/// - tlsConfiguration: `TLSConfiguration` used to connect with TLS. If nil, a configuration with no backing
/// certificates is used. See `TLSConfiguration` for other configuration options.
/// - domainName: Client domain name used when communicating with the server. Defaults to `localhost`.
/// - timeout: How long to try connecting to the server to before returning an error. Defaults to `10` seconds.
///
/// - Note:
/// - You may need to enable access for less secure apps for your account on the SMTP server.
/// - Some servers like Gmail support IPv6, and if your network does not, you will first attempt to connect via
/// IPv6, then timeout, and fall back to IPv4. You can avoid this by disabling IPv6 on your machine.
public init(hostname: String,
port: Int32 = 587,
tlsMode: TLSMode = .requireSTARTTLS,
tlsConfiguration: TLSConfiguration? = nil,
domainName: String = "localhost",
timeout: UInt = 10) {
self.hostname = hostname
self.email = ""
self.password = ""
self.port = port
self.tlsMode = tlsMode
self.tlsConfiguration = tlsConfiguration
self.authMethods = [String: AuthMethod]()
self.domainName = domainName
self.timeout = timeout
}

/// Send an email.
///
/// - Parameters:
Expand Down
50 changes: 47 additions & 3 deletions Sources/SwiftSMTP/SMTPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,40 @@ struct SMTPSocket {
domainName: String,
timeout: UInt) throws {
socket = try Socket.create()
let serverOptions = try setupSocket(hostname: hostname,
port: port,
tlsMode: tlsMode,
tlsConfiguration: tlsConfiguration,
domainName: domainName,
timeout: timeout)
let authMethod = try getAuthMethod(authMethods: authMethods, serverOptions: serverOptions, hostname: hostname)
try login(authMethod: authMethod, email: email, password: password)
}


/// Initializer for an SMTPSocket when you want to connect to a server that does not
/// require authentication to send messages.
init(hostname: String,
port: Int32,
tlsMode: SMTP.TLSMode,
tlsConfiguration: TLSConfiguration?,
domainName: String,
timeout: UInt) throws {
socket = try Socket.create()
_ = try setupSocket(hostname: hostname,
port: port,
tlsMode: tlsMode,
tlsConfiguration: tlsConfiguration,
domainName: domainName,
timeout: timeout)
}

private func setupSocket(hostname: String,
port: Int32,
tlsMode: SMTP.TLSMode,
tlsConfiguration: TLSConfiguration?,
domainName: String,
timeout: UInt) throws -> [Response] {
if tlsMode == .requireTLS {
if let tlsConfiguration = tlsConfiguration {
socket.delegate = try tlsConfiguration.makeSSLService()
Expand All @@ -48,8 +82,7 @@ struct SMTPSocket {
throw SMTPError.requiredSTARTTLS
}
}
let authMethod = try getAuthMethod(authMethods: authMethods, serverOptions: serverOptions, hostname: hostname)
try login(authMethod: authMethod, email: email, password: password)
return serverOptions
}

func write(_ text: String) throws {
Expand Down Expand Up @@ -148,9 +181,11 @@ private extension SMTPSocket {
}

func getAuthMethod(authMethods: [String: AuthMethod], serverOptions: [Response], hostname: String) throws -> AuthMethod {
var requiresAuth = false
for option in serverOptions {
let components = option.message.components(separatedBy: " ")
if components.first == "AUTH" {
requiresAuth = true
let _authMethods = components.dropFirst()
for authMethod in _authMethods {
if let matchingAuthMethod = authMethods[authMethod] {
Expand All @@ -159,7 +194,13 @@ private extension SMTPSocket {
}
}
}
throw SMTPError.noAuthMethodsOrRequiresTLS(hostname: hostname)
if requiresAuth {
Copy link

@Juice805 Juice805 Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it a test on a server that supports auth, but does not require it. This check prevents sending.

It looks like you'll need to call the correct SMTPSocket.init from within SMTP.send for those using the new SMTP.init.

Not a contribution

// the server supports AUTH, but no matching methods were found
throw SMTPError.noAuthMethodsOrRequiresTLS(hostname: hostname)
} else {
// the server does not want to hear about AUTH. It's an open relay.
return .none
}
}

func doStarttls(serverOptions: [Response], tlsConfiguration: TLSConfiguration?) throws -> Bool {
Expand Down Expand Up @@ -194,6 +235,9 @@ private extension SMTPSocket {
try loginPlain(email: email, password: password)
case .xoauth2:
try loginXOAuth2(email: email, accessToken: password)
case .none:
// don't do anything
return
}
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/SwiftSMTPTests/Constant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ let testDuration: Double = 15

// 📧📧📧 Fill in your own SMTP login info for local testing
// ⚠️⚠️⚠️ DO NOT CHECK IN YOUR EMAIL CREDENTALS!!!
let noAuthHost: String? = "localhost"
let noAuthPort: Int32 = 1081

let hostname = "mail.kitura.dev"
let myEmail: String? = nil
let myPassword: String? = nil
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftSMTPTests/TestMailSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ class TestMailSender: XCTestCase {
x.fulfill()
}
}

func testSendMailNoAuth() throws {
let x = expectation(description: #function)
defer { waitForExpectations(timeout: testDuration) }

let mail = Mail(from: from, to: [to], subject: #function, text: text)
if let theHost = noAuthHost {
let noAuthSMTP = SMTP(hostname: theHost, port: noAuthPort, tlsMode: .ignoreTLS,
tlsConfiguration: .none)
noAuthSMTP.send(mail) { (err) in
XCTAssertNil(err, String(describing: err))
x.fulfill()
}
} else {
throw XCTSkip("No no-auth SMTP server configured")
}
}

func testSendMailInArray() {
let x = expectation(description: #function)
Expand Down
25 changes: 25 additions & 0 deletions Tests/SwiftSMTPTests/TestSMTPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import XCTest

class TestSMTPSocket: XCTestCase {
static var allTests = [
("testNoAuth", testNoAuth),
("testBadCredentials", testBadCredentials),
("testBadPort", testBadPort),
("testLogin", testLogin),
Expand All @@ -27,6 +28,30 @@ class TestSMTPSocket: XCTestCase {
("testSSL", testSSL)
]

func testNoAuth() throws {
if let noAuthHost = noAuthHost {
let x = expectation(description: #function)
defer { waitForExpectations(timeout: testDuration) }

do {
_ = try SMTPSocket(
hostname: noAuthHost,
port: noAuthPort,
tlsMode: .ignoreTLS,
tlsConfiguration: nil,
domainName: domainName,
timeout: timeout
)
x.fulfill()
} catch {
XCTFail(String(describing: error))
x.fulfill()
}
} else {
throw XCTSkip("No no-auth SMTP server configured")
}
}

func testBadCredentials() throws {
let x = expectation(description: #function)
defer { waitForExpectations(timeout: testDuration) }
Expand Down