forked from ruby/net-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 WIP: Use net-imap's SASL implementation
Ideally, `net-smtp` and `net-imap` should both depend on a shared `sasl` or `net-sasl` gem, rather than keep the SASL implementation inside one or the other.
- Loading branch information
Showing
11 changed files
with
163 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Net | ||
class SMTP | ||
class Authenticator | ||
|
||
# This curries arguments to support the Authenticator API used by v0.4.0. | ||
# | ||
# Net::SMTP#authenticate still uses the old API, so v0.4.0 compatible | ||
# Authenticators can still be added and used with it. | ||
class CompatibilityAdapter | ||
def initialize(mechanism) | ||
@mechanism = mechanism.to_s.tr("_", "-").upcase | ||
end | ||
|
||
def new(smtp) | ||
@smtp = smtp | ||
self | ||
end | ||
|
||
def sasl_authenticator(user = nil, secret = nil, *args, **kwargs, &block) | ||
mechanism = mechanism | ||
args = [] | ||
args << user unless user.nil? && secret.nil? | ||
args << secret unless secret.nil? | ||
SASL.authenticator(@mechanism, *args, **kwargs, &block) | ||
end | ||
|
||
def auth(*args, **kwargs, &block) | ||
sasl = sasl_authenticator(*args, **kwargs, &block) | ||
Adapter.new(@smtp, @mechanism, sasl).auth | ||
end | ||
end | ||
|
||
auth_classes.default_proc = ->h, mech { CompatibilityAdapter.new(mech) } | ||
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module Net | ||
class SMTP | ||
class Authenticator | ||
|
||
def self.auth(client, mechanism, *args, **kwargs, &block) | ||
sasl = SASL.authenticator(mechanism, *args, **kwargs, &block) | ||
Adapter.new(client, mechanism, sasl).auth | ||
end | ||
|
||
class ClientAdapter | ||
attr_reader :client, :mechanism, :sasl | ||
|
||
def initialize(client, mechanism, sasl) | ||
@client, @mechanism, @sasl = client, mechanism, sasl | ||
end | ||
|
||
def auth | ||
response = encode_ir sasl.process nil if send_initial_response? | ||
result = run_auth_command response do |challenge| | ||
encode sasl.process decode challenge | ||
end | ||
handle_failure result | ||
handle_incomplete result if sasl.respond_to?(:done?) && !sasl.done? | ||
handle_success result | ||
end | ||
|
||
def decode(string) string.unpack1("m0") end | ||
def encode(string) [string].pack('m0') end | ||
def encode_ir(string) string.empty? ? "=" : encode(string) end | ||
|
||
# Returns protocol and server capabilities for both IR and mechanism | ||
def send_initial_response? | ||
capable_initial_response? && | ||
server_supports?(mechanism) && | ||
sasl.respond_to?(:initial_response?) && | ||
sasl.initial_response? | ||
end | ||
|
||
# Protocol clients should override to return boolean based on client | ||
# preferences and server/protocol capabilities. | ||
def capable_initial_response?; true end | ||
|
||
# Protocol clients should override to return boolean based on server's | ||
# advertized support. | ||
def server_supports?(mechanism) true end | ||
end | ||
|
||
class Adapter < ClientAdapter | ||
def capable_initial_response?; true end # TODO: check capabilities | ||
def server_supports?(mechanism) true end # TODO: check capabilities | ||
|
||
# sends the appropriate auth command (encoding initial client response) | ||
# yields each continuation's encoded data | ||
# block may run in same thread or in another thread as a callback | ||
# sends each encoded client response | ||
# returns the command's final response or result object | ||
def run_auth_command(sasl_response, &block) | ||
cmdargs = ["AUTH", mechanism, sasl_response].compact | ||
res = client.send_command_yielding_continuations(*cmdargs, &block) | ||
raise SMTPAuthenticationError.new(res) unless res.success? | ||
res | ||
end | ||
|
||
def handle_failure(res) | ||
raise SMTPAuthenticationError.new(res) unless res.success? | ||
end | ||
|
||
def handle_incomplete(res) raise res.exception_class.new(res) end | ||
def handle_success(res) res end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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