Sha256: e3f653348edb356e3ffe3fe649e42642f1a42d92443207e0f7c47ee65de8741d

Contents?: true

Size: 1.34 KB

Versions: 21

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module NeetoCommonsBackend
  module Authenticatable
    extend ActiveSupport::Concern

    included do
      before_action :authenticate_user_using_x_auth_token

      respond_to :json

      attr_reader :user

      private

        def authenticate_user_using_x_auth_token
          return if user_signed_in?

          email = request.headers["X-Auth-Email"]
          auth_token = request.headers["X-Auth-Token"]

          @user = email &&
            @organization &&
            User.find_first_by_auth_conditions(email:, organization_id: @organization.id)

          if valid_user_token?(auth_token)
            sign_in user, store: false
            User.current = user
          else
            message = if user && !user.active?
              t("devise.failure.deactivated")
            else
              t("devise.failure.timeout")
            end

            render_error(message, :unauthorized)
          end
        end

        def valid_user_token?(auth_token)
          user && user.active? && Devise.secure_compare(user.authentication_token, auth_token)
        end

        def authenticate_organization_api_key!
          unless @organization.api_key == request.headers["X-Neeto-API-Key"]
            render_error(t("authentication.unauthorized"), :unauthorized) and return
          end
        end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
neeto-commons-backend-1.0.109 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.108 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.107 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.106 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.105 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.104 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.103 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.102 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.101 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.100 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.99 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.98 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.97 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.96 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.95 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.94 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.93 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.92 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.91 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb
neeto-commons-backend-1.0.90 lib/neeto_commons_backend/controllers/concerns/authenticatable.rb