Sha256: 7059205116b3671569b22f9b2182232513ccd4c1b24c71162a0454df0ec13360

Contents?: true

Size: 809 Bytes

Versions: 10

Compression:

Stored size: 809 Bytes

Contents

# frozen_string_literal: true

#
# From https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
#
require "devise"

module Renalware
  module API
    class TokenAuthenticatedAPIController < ApplicationController
      before_action :authenticate_user_from_token!
      before_action :authenticate_user! # fallback

      private

      def authenticate_user_from_token!
        username = params[:username].presence
        user = username && User.find_by(username: username)

        # Notice how we use Devise.secure_compare to compare the token
        # in the database with the token given in the params, mitigating
        # timing attacks.
        if user && ::Devise.secure_compare(user.authentication_token, params[:token])
          sign_in user, store: false
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.1.0 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.167 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.166 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.165 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.164 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.163 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.162 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.161 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.160 app/controllers/renalware/api/token_authenticated_api_controller.rb