Class: Challah::TokenTechnique

Inherits:
Object
  • Object
show all
Defined in:
lib/challah/techniques/token_technique.rb

Overview

Allows authentication with a token URL parameter or X-Auth-Token header. Useful for API-based authentication.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ TokenTechnique

Returns a new instance of TokenTechnique.



8
9
10
11
12
13
14
# File 'lib/challah/techniques/token_technique.rb', line 8

def initialize(session)
  if session.request && session.request.headers[header_key]
    @token = session.request.headers[header_key].to_s
  else
    @token = session.params[:token].to_s
  end
end

Instance Attribute Details

#user_modelObject

Returns the value of attribute user_model.



6
7
8
# File 'lib/challah/techniques/token_technique.rb', line 6

def user_model
  @user_model
end

Instance Method Details

#authenticateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/challah/techniques/token_technique.rb', line 16

def authenticate
  # Token authorization functionality is only enabled with the :token_enabled option.
  # This is turned off by default and must be manually enabled for security reasons.
  return nil unless Challah.options[:token_enabled]

  return nil unless token.present?

  if user = user_model.where(api_key: token).first
    if user.valid_session?
      return user
    end
  end

  nil
end

#header_keyObject



32
33
34
# File 'lib/challah/techniques/token_technique.rb', line 32

def header_key
  Challah.options[:token_header] || "X-Auth-Token"
end

#persist?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/challah/techniques/token_technique.rb', line 36

def persist?
  false
end