Module: SimpleTelnetServer::HasLogin

Defined in:
lib/em-simple_telnet_server/has_login.rb

Overview

Adds functionality for simple authentication and authorization.

By default, login credentials are defined right in the class definition using ClassMethods::has_login. If something more dynamic is needed, just override #authenticate.

After authentication, #entered_username, #entered_password, and #authorized_role are set.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authorized_roleSymbol (readonly)

Returns the associated role of the valid login credentials.

Returns:

  • (Symbol)

    the associated role of the valid login credentials



41
42
43
# File 'lib/em-simple_telnet_server/has_login.rb', line 41

def authorized_role
  @authorized_role
end

#entered_passwordString (readonly)

Returns the password username.

Returns:

  • (String)

    the password username



38
39
40
# File 'lib/em-simple_telnet_server/has_login.rb', line 38

def entered_password
  @entered_password
end

#entered_usernameString (readonly)

Note:

This doesn’t necessarily mean the user is logged in. Use #authorized? to check for that.

Returns the entered username.

Returns:

  • (String)

    the entered username



35
36
37
# File 'lib/em-simple_telnet_server/has_login.rb', line 35

def entered_username
  @entered_username
end

Class Method Details

.included(klass) ⇒ Object

Extends klass with ClassMethods.



11
12
13
# File 'lib/em-simple_telnet_server/has_login.rb', line 11

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#authenticate(user, pass) ⇒ Symbol?

Checks user and pass against all known login credentials.

Parameters:

  • user (String)

    entered username

  • pass (String)

    entered password

Returns:

  • (Symbol)

    associated role, if credentials are known

  • (nil)

    if credentials are not known



134
135
136
137
138
139
# File 'lib/em-simple_telnet_server/has_login.rb', line 134

def authenticate(user, pass)
  self.class..each do |role, credentials|
    return role if credentials == [ user, pass ]
  end
  return nil
end

#initiate_authenticationObject

Initiates authentication. This means setting the connection state to waiting_for_username and sending the login prompt.



50
51
52
53
# File 'lib/em-simple_telnet_server/has_login.rb', line 50

def initiate_authentication
  @connection_state = :waiting_for_username
  
end

#needs_authentication?true

Returns true, as this module is about authentication.

Returns:

  • (true)

    true, as this module is about authentication



44
45
46
# File 'lib/em-simple_telnet_server/has_login.rb', line 44

def needs_authentication?
  true
end

#process_bufferObject

If the user was requested to enter his username, the username is read. If the user was requested to enter his password, the password is read.

Otherwise, the normal (super) behavior proceeds.

In all cases, it ensures that the buffer is cleared at the end.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/em-simple_telnet_server/has_login.rb', line 62

def process_buffer
  if waiting_for_username?
    read_username_from_buffer

  elsif waiting_for_password?
    read_password_from_buffer

  else
    super
  end
ensure
  @buffer.clear
end

#read_password_from_bufferObject

Reads the password from buffer and authorizes the user if he can be authenticated. Otherwise the connection state is set back to :waiting_for_username and the login prompt is sent.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/em-simple_telnet_server/has_login.rb', line 100

def read_password_from_buffer
  @entered_password = @buffer.chomp
  if role = authenticate(@entered_username, @entered_password)
    @authorized_role = role
    authorize
  else
    @connection_state = :waiting_for_username
    @entered_username = @entered_password = nil
    
    
  end
end

#read_username_from_bufferObject

Reads the username from buffer. Sends the password prompt afterwards (#send_password_prompt) and sets the connection state to :waiting_for_password.



116
117
118
119
120
# File 'lib/em-simple_telnet_server/has_login.rb', line 116

def read_username_from_buffer
  @entered_username = @buffer.strip
  send_password_prompt
  @connection_state = :waiting_for_password
end

#send_login_failedObject

Sends the message “Sorry, please try again.” and the login prompt.



123
124
125
# File 'lib/em-simple_telnet_server/has_login.rb', line 123

def 
  send_data "Sorry, please try again.\n"
end

#send_login_promptObject

Sends the login prompt.



77
78
79
# File 'lib/em-simple_telnet_server/has_login.rb', line 77

def 
  send_data options[:login_prompt]
end

#send_password_promptObject

Sends the password prompt.



82
83
84
# File 'lib/em-simple_telnet_server/has_login.rb', line 82

def send_password_prompt
  send_data options[:password_prompt]
end

#waiting_for_password?Boolean

Returns whether we are waiting for the user to enter the password.

Returns:

  • (Boolean)

    whether we are waiting for the user to enter the password



92
93
94
# File 'lib/em-simple_telnet_server/has_login.rb', line 92

def waiting_for_password?
  @connection_state == :waiting_for_password
end

#waiting_for_username?Boolean

Returns whether we are waiting for the user to enter the username.

Returns:

  • (Boolean)

    whether we are waiting for the user to enter the username



87
88
89
# File 'lib/em-simple_telnet_server/has_login.rb', line 87

def waiting_for_username?
  @connection_state == :waiting_for_username
end