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
-
#authorized_role ⇒ Symbol
readonly
The associated role of the valid login credentials.
-
#entered_password ⇒ String
readonly
The password username.
-
#entered_username ⇒ String
readonly
The entered username.
Class Method Summary collapse
-
.included(klass) ⇒ Object
Extends klass with ClassMethods.
Instance Method Summary collapse
-
#authenticate(user, pass) ⇒ Symbol?
Checks user and pass against all known login credentials.
-
#initiate_authentication ⇒ Object
Initiates authentication.
-
#needs_authentication? ⇒ true
True, as this module is about authentication.
-
#process_buffer ⇒ Object
If the user was requested to enter his username, the username is read.
-
#read_password_from_buffer ⇒ Object
Reads the password from buffer and authorizes the user if he can be authenticated.
-
#read_username_from_buffer ⇒ Object
Reads the username from buffer.
-
#send_login_failed ⇒ Object
Sends the message “Sorry, please try again.” and the login prompt.
-
#send_login_prompt ⇒ Object
Sends the login prompt.
-
#send_password_prompt ⇒ Object
Sends the password prompt.
-
#waiting_for_password? ⇒ Boolean
Whether we are waiting for the user to enter the password.
-
#waiting_for_username? ⇒ Boolean
Whether we are waiting for the user to enter the username.
Instance Attribute Details
#authorized_role ⇒ Symbol (readonly)
Returns 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 end |
#entered_password ⇒ String (readonly)
Returns the password username.
38 39 40 |
# File 'lib/em-simple_telnet_server/has_login.rb', line 38 def entered_password @entered_password end |
#entered_username ⇒ String (readonly)
This doesn’t necessarily mean the user is logged in. Use #authorized? to check for that.
Returns 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.
134 135 136 137 138 139 |
# File 'lib/em-simple_telnet_server/has_login.rb', line 134 def authenticate(user, pass) self.class.login_credentials.each do |role, credentials| return role if credentials == [ user, pass ] end return nil end |
#initiate_authentication ⇒ Object
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 send_login_prompt end |
#needs_authentication? ⇒ true
Returns 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_buffer ⇒ Object
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_buffer ⇒ Object
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 else @connection_state = :waiting_for_username @entered_username = @entered_password = nil send_login_failed send_login_prompt end end |
#read_username_from_buffer ⇒ Object
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_failed ⇒ Object
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_login_failed send_data "Sorry, please try again.\n" end |
#send_login_prompt ⇒ Object
Sends the login prompt.
77 78 79 |
# File 'lib/em-simple_telnet_server/has_login.rb', line 77 def send_login_prompt send_data [:login_prompt] end |
#send_password_prompt ⇒ Object
Sends the password prompt.
82 83 84 |
# File 'lib/em-simple_telnet_server/has_login.rb', line 82 def send_password_prompt send_data [:password_prompt] end |
#waiting_for_password? ⇒ Boolean
Returns 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.
87 88 89 |
# File 'lib/em-simple_telnet_server/has_login.rb', line 87 def waiting_for_username? @connection_state == :waiting_for_username end |