-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Net::SMTP::Authenticator class and auth_* methods are separated f…
…rom the Net::SMTP class. This allows you to create an arbitrary authentication plugin without a monkey patch. Create a class with an `auth` method that inherits Net::SMTP::Authenticator. The `auth` method has two arguments, `user` and `secret`. Send an instruction to the SMTP server by using the `continue` or `finish` method. For more information, see lib/net/smtp/auto _*.rb.
- Loading branch information
Showing
6 changed files
with
136 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
unless defined? OpenSSL | ||
begin | ||
require 'digest/md5' | ||
rescue LoadError | ||
end | ||
end | ||
|
||
class Net::SMTP | ||
class AuthCramMD5 < Net::SMTP::Authenticator | ||
auth_type :cram_md5 | ||
|
||
def auth(user, secret) | ||
challenge = continue('AUTH CRAM-MD5') | ||
crammed = cram_md5_response(secret, challenge.unpack1('m')) | ||
finish(base64_encode("#{user} #{crammed}")) | ||
end | ||
|
||
IMASK = 0x36 | ||
OMASK = 0x5c | ||
|
||
# CRAM-MD5: [RFC2195] | ||
def cram_md5_response(secret, challenge) | ||
tmp = digest_class::MD5.digest(cram_secret(secret, IMASK) + challenge) | ||
digest_class::MD5.hexdigest(cram_secret(secret, OMASK) + tmp) | ||
end | ||
|
||
CRAM_BUFSIZE = 64 | ||
|
||
def cram_secret(secret, mask) | ||
secret = digest_class::MD5.digest(secret) if secret.size > CRAM_BUFSIZE | ||
buf = secret.ljust(CRAM_BUFSIZE, "\0") | ||
0.upto(buf.size - 1) do |i| | ||
buf[i] = (buf[i].ord ^ mask).chr | ||
end | ||
buf | ||
end | ||
|
||
def digest_class | ||
@digest_class ||= if defined?(OpenSSL::Digest) | ||
OpenSSL::Digest | ||
elsif defined?(::Digest) | ||
::Digest | ||
else | ||
raise '"openssl" or "digest" library is required' | ||
end | ||
end | ||
end | ||
end |
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,11 @@ | ||
class Net::SMTP | ||
class AuthLogin < Net::SMTP::Authenticator | ||
auth_type :login | ||
|
||
def auth(user, secret) | ||
continue('AUTH LOGIN') | ||
continue(base64_encode(user)) | ||
finish(base64_encode(secret)) | ||
end | ||
end | ||
end |
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,9 @@ | ||
class Net::SMTP | ||
class AuthPlain < Net::SMTP::Authenticator | ||
auth_type :plain | ||
|
||
def auth(user, secret) | ||
finish('AUTH PLAIN ' + base64_encode("\0#{user}\0#{secret}")) | ||
end | ||
end | ||
end |
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,46 @@ | ||
module Net | ||
class SMTP | ||
class Authenticator | ||
def self.auth_classes | ||
@classes ||= {} | ||
end | ||
|
||
def self.auth_type(type) | ||
Authenticator.auth_classes[type] = self | ||
end | ||
|
||
def self.auth_class(type) | ||
Authenticator.auth_classes[type.intern] | ||
end | ||
|
||
attr_reader :smtp | ||
|
||
def initialize(smtp) | ||
@smtp = smtp | ||
end | ||
|
||
# @param arg [String] message to server | ||
# @return [String] message from server | ||
def continue(arg) | ||
res = smtp.get_response arg | ||
raise res.exception_class.new(res) unless res.continue? | ||
res.string.split[1] | ||
end | ||
|
||
# @param arg [String] message to server | ||
# @return [Net::SMTP::Response] response from server | ||
def finish(arg) | ||
res = smtp.get_response arg | ||
raise SMTPAuthenticationError.new(res) unless res.success? | ||
res | ||
end | ||
|
||
# @param str [String] | ||
# @return [String] Base64 encoded string | ||
def base64_encode(str) | ||
# expects "str" may not become too long | ||
[str].pack('m0') | ||
end | ||
end | ||
end | ||
end |
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