Class: Challah::TokenTechnique
- Inherits:
-
Object
- Object
- Challah::TokenTechnique
- 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
-
#user_model ⇒ Object
Returns the value of attribute user_model.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #header_key ⇒ Object
-
#initialize(session) ⇒ TokenTechnique
constructor
A new instance of TokenTechnique.
- #persist? ⇒ Boolean
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_model ⇒ Object
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
#authenticate ⇒ Object
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.[: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_key ⇒ Object
32 33 34 |
# File 'lib/challah/techniques/token_technique.rb', line 32 def header_key Challah.[:token_header] || "X-Auth-Token" end |
#persist? ⇒ Boolean
36 37 38 |
# File 'lib/challah/techniques/token_technique.rb', line 36 def persist? false end |