lib/simple_token_authentication/safe/acts_as_token_authentication_handler.rb
# For this example, we are simply using token authentication
# via parameters. However, anyone could use Rails's token
# authentication features to get the token from a header.
For this example, we are simply using token authentication
via parameters. However, anyone could use Rails's token
authentication features to get the token from a header.
For this example we are simply using token authentication via parameters . However anyone could use Rails ' s token authentication features to get the token from a header .
def authenticate_entity_from_token!
      params_token_name = "#{entity_name.underscore}_token".to_sym
      params_email_name = "#{entity_name.singularize.underscore}_email".to_sym
      if token = params[params_token_name].blank? && request.headers[header_token_name]
        params[params_token_name] = token
      end
      if email = params[params_email_name].blank? && request.headers[header_email_name]
        params[params_email_name] = email
      end
      email = params[params_email_name].presence
      entity = nil
      if entity_class.respond_to? "find_by"
        entity = email && entity_class.find_by(email: email)
      elsif entity_class.respond_to? "find_by_email"
        entity = email && entity_class.find_by_email(email)
      end
      if entity && Devise.secure_compare(entity.authentication_token, params[params_token_name])
        sign_in entity, store: SimpleTokenAuthentication.sign_in_token
      end
    end
def authenticate_entity_from_token! params_token_name = "#{entity_name.underscore}_token" . to_sym params_email_name = "#{entity_name.singularize.underscore}_email" . to_sym if token = params [ params_token_name ] . blank? && request . headers [ header_token_name ] params [ params_token_name ] = token end if email = params [ params_email_name ] . blank? && request . headers [ header_email_name ] params [ params_email_name ] = email end email = params [ params_email_name ] . presence entity = nil if entity_class . respond_to? "find_by" entity = email && entity_class . find_by ( email : email ) elsif entity_class . respond_to? "find_by_email" entity = email && entity_class . find_by_email ( email ) end if entity && Devise . secure_compare ( entity . authentication_token , params [ params_token_name ] ) sign_in entity , store : SimpleTokenAuthentication . sign_in_token end end
For this example, we are simply using token authentication
via parameters.
For this example we are simply using token authentication via parameters .
# Set the authentication token params if not already present, # see http://stackoverflow.com/questions/11017348/rails-api-authentication-by-headers-token # See https://github.com/ryanb/cancan/blob/1.6.10/lib/cancan/controller_resource.rb#L108-L111 # Notice how we use Devise.secure_compare to compare the token # in the database with the token given in the params, mitigating # timing attacks. # Notice we are passing store false, so the entity is not # actually stored in the session and a token is needed # for every request. If you want the token to work as a # sign in token, you can simply remove store: false.