Class: Authentication::Authenticator

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

Overview

A object to authenticate client.

Since:

  • 0.0.2

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Authenticator

Returns a new instance of Authenticator.

Parameters:

  • options (Hash)

    the options to create a authenticator with.

Options Hash (options):

  • :session (Hash)

    Hash-like session object.

  • :session_key (Symbol)

    Key of session_id.

  • :finder (Proc)

    A proc which returns client to authenticate.

Since:

  • 0.0.2



9
10
11
12
13
# File 'lib/authentication/authenticator.rb', line 9

def initialize(options)
  @session = options.fetch :session
  @session_key = options.fetch :session_key
  @finder = options.fetch :finder
end

Instance Method Details

#current_clientObject Also known as: current_user

Return current_client. If it does not exist, returns nil.

Returns:

  • (Object)

    The object stored as client or nil

Since:

  • 0.0.2



28
29
30
# File 'lib/authentication/authenticator.rb', line 28

def current_client
  @current_client ||= @finder.call
end

#current_client_idObject Also known as: current_user_id

Return id of given current_client. If it does not exist, returns nil.

Returns:

  • (Object)

    The id of object stored as client or nil

Since:

  • 0.0.2



37
38
39
# File 'lib/authentication/authenticator.rb', line 37

def current_client_id
  @session[@session_key]
end

#logged_in?Boolean

Return current_client exists or not.

Returns:

  • (Boolean)

Since:

  • 0.0.2



45
46
47
# File 'lib/authentication/authenticator.rb', line 45

def logged_in?
  not current_client.nil?
end

#login!(client) ⇒ Object

Set current_client.

Raises:

Since:

  • 0.0.2



18
19
20
21
22
# File 'lib/authentication/authenticator.rb', line 18

def login!(client)
  raise Unauthenticated unless client
  @current_client = client
  @session[@session_key] = client.id
end

#logout!Object

Delete current_client from database and session.

Since:

  • 0.0.2



51
52
53
54
# File 'lib/authentication/authenticator.rb', line 51

def logout!
  return unless current_client
  @current_client = @session[@session_key] = nil
end