Class: Autumn::Authentication::Password

Inherits:
Base
  • Object
show all
Defined in:
lib/autumn/authentication.rb

Overview

Authenticates by a password provided in secret. When a user PRIVMSG’s the leaf the correct password, the leaf adds that user’s nick to a list of authorized nicks. These credentials expire when the person changes his nick, logs out, leaves the channel, etc. They also expire if a certain amount of time passes without running any protected commands.

Constant Summary collapse

DEFAULT_EXPIRE_TIME =

The default period of time that must occur with no use of protected commands after which a user’s credentials expire.

5*60

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Password

Creates a new authenticator. You provide a valid password with the password option. If that option is not provided, an exception is raised. You can pass a number of seconds to the expire_time option; this is the amount of time that must pass with no protected commands for a nick’s authorization to expire. If the expire_time option is not given, a default value of five minutes is used.



179
180
181
182
183
184
185
186
187
# File 'lib/autumn/authentication.rb', line 179

def initialize(options={})
  @password = options[:password]
  @expire_time = options[:expire_time]
  @expire_time ||= DEFAULT_EXPIRE_TIME
  raise "You must provide a password to use password-based authentication" unless @password
  @authorized_nicks = Hash.new { |hsh, key| hsh[key] = Set.new }
  @last_protected_action = Hash.new { |hsh, key| hsh[key] = Hash.new(Time.at(0)) }
  @an_lock = Mutex.new
end

Instance Method Details

#authenticate(stem, channel, sender, leaf) ⇒ Object

:nodoc:



215
216
217
218
219
220
221
222
223
224
# File 'lib/autumn/authentication.rb', line 215

def authenticate(stem, channel, sender, leaf) # :nodoc:
  @an_lock.synchronize do
    if Time.now - @last_protected_action[stem][sender[:nick]] > @expire_time then
      revoke stem, sender[:nick]
    else
      @last_protected_action[stem][sender[:nick]] = Time.now
    end
    @authorized_nicks[stem].include? sender[:nick]
  end
end

#irc_kick_event(stem, sender, arguments) ⇒ Object

:nodoc:



207
208
209
# File 'lib/autumn/authentication.rb', line 207

def irc_kick_event(stem, sender, arguments) # :nodoc:
  @an_lock.synchronize { revoke stem, arguments[:nick] }
end

#irc_nick_event(stem, sender, arguments) ⇒ Object

:nodoc:



200
201
202
203
204
205
# File 'lib/autumn/authentication.rb', line 200

def irc_nick_event(stem, sender, arguments) # :nodoc:
  @an_lock.synchronize do
    revoke stem, sender[:nick]
    revoke stem, arguments[:nick]
  end
end

#irc_privmsg_event(stem, sender, arguments) ⇒ Object

:nodoc:



189
190
191
192
193
194
195
196
197
198
# File 'lib/autumn/authentication.rb', line 189

def irc_privmsg_event(stem, sender, arguments) # :nodoc:
  if arguments[:recipient] and arguments[:message] == @password then
    @an_lock.synchronize do
      @authorized_nicks[stem] << sender[:nick]
      @last_protected_action[stem][sender[:nick]] = Time.now
      #TODO values are not always deleted; this hash has the possibility to slowly grow and consume more memory
    end
    stem.message "Your password has been accepted, and you are now authorized.", sender[:nick]
  end
end

#irc_quit_event(stem, sender, arguments) ⇒ Object

:nodoc:



211
212
213
# File 'lib/autumn/authentication.rb', line 211

def irc_quit_event(stem, sender, arguments) # :nodoc:
  @an_lock.synchronize { revoke stem, sender[:nick] }
end

#unauthorizedObject

:nodoc:



226
227
228
# File 'lib/autumn/authentication.rb', line 226

def unauthorized # :nodoc:
  "You must authenticate with an administrator password to do that."
end