Mastering Nginx

(Ron) #1

Using the Mail Module


[ 54 ]

Tying these three parts together, we have the complete authentication service:


#!/usr/bin/env rackup

# This is a basic HTTP server, conforming to the authentication
protocol
# required by NGINX's mail module.
#
require 'logger'
require 'rack'

module MailAuth

# setup a protocol-to-port mapping
Port = {
'smtp' => '25',
'pop3' => '110',
'imap' => '143'
}

class Handler

def initialize
# setup logging, as a mail service
@log = Logger.new("| logger -p mail.info")
# replacing the normal timestamp by the service name and pid
@log.datetime_format = "nginx_mail_proxy_auth pid: "
# the "Auth-Server" header must be an IP address
@mailhost = '127.0.0.1'
# set a maximum number of login attempts
@max_attempts = 3
# our authentication 'database' will just be a fixed hash for
# this example
# it should be replaced by a method to connect to LDAP or a
# database
@auths = { "test:1234" => '127.0.1.1' }
end

After the preceding setup and module initialization, we tell Rack which requests
we would like to have handled and define a get method to respond to requests


from NGINX.


def call(env)
# our headers are contained in the environment
@env = env
Free download pdf