使用devise gem对应用程序的所有用户进行身份验证。 我正在尝试实施Active Storage。
让我们说所有用户一旦到达应用就必须经过身份验证:
class ApplicationController < ActionController::Base
before_action :authenticate_user!
...
end
如何保护Active Storage生成的路由?
无需先进行身份验证即可访问上传文件的URL。未经身份验证的用户可以获取Active Storage生成的文件URL。
答案 0 :(得分:11)
这不是一个完整的答案,而是一个起点:
要点:您需要覆盖重定向控制器。
docs for activestorage/app/controllers/active_storage/blobs_controller.rb说:
如果您需要强制执行访问保护 已签名blob引用的安全性通过隐藏因素, 您需要实现自己的身份验证重定向 控制器。
另外,如果您打算使用预览docs for activestorage/app/models/active_storage/blob/representable.rb说
中找到相关信息Active Storage为预览提供了一个[控制器操作],但您可能想要自己创建(for 例如,如果您需要身份验证)。
<强>更新强>
这是一个“应该”用于防止在使用devise
gem时未经授权访问重定向的最小示例。
如果记录的用户将被重定向到的URL如何被保护,我猜是另一个故事。默认情况下,它们会在5分钟后过期,但可以设置为较短的时间段,例如10秒(如果您使用expires_in 10.seconds
替换下面示例中的第6行)
使用以下代码创建文件app/controllers/active_storage/blobs_controller.rb
:
class ActiveStorage::BlobsController < ActiveStorage::BaseController
before_action :authenticate_user!
include ActiveStorage::SetBlob
def show
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to @blob.service_url(disposition: params[:disposition])
end
end
请注意,original code唯一改变的是添加了第二行
before_action :authenticate_user!
更新2:
您可以在ActiveStorage::RepresentationsController
和ActiveStorage::BlobsController
中加入以devise
ActiveStorage
身份验证。
请参阅此处包含https://gist.github.com/dommmel/4e41b204b97238e9aaf35939ae8e1666的要点:
# Rails controller concern to enable Devise authentication for ActiveStorage.
# Put it in +app/controllers/concerns/blob_authenticatable.rb+ and include it when overriding
# +ActiveStorage::BlobsController+ and +ActiveStorage::RepresentationsController+.
#
# Optional configuration:
#
# Set the model that includes devise's database_authenticatable.
# Defaults to Devise.default_scope which defaults to the first
# devise role declared in your routes (usually :user)
#
# blob_authenticatable resource: :admin
#
# To specify how to determine if the current_user is allowed to access the
# blob, override the can_access_blob? method
#
# Minimal example:
#
# class ActiveStorage::BlobsController < ActiveStorage::BaseController
# include ActiveStorage::SetBlob
# include AdminOrUserAuthenticatable
#
# def show
# expires_in ActiveStorage::Blob.service.url_expires_in
# redirect_to @blob.service_url(disposition: params[:disposition])
# end
# end
#
# Complete example:
#
# class ActiveStorage::RepresentationsController < ActiveStorage::BaseController
# include ActiveStorage::SetBlob
# include AdminOrUserAuthenticatable
#
# blob_authenticatable resource: :admin
#
# def show
# expires_in ActiveStorage::Blob.service.url_expires_in
# redirect_to @blob.representation(params[:variation_key]).processed.service_url(disposition: params[:disposition])
# end
#
# private
#
# def can_access_blob?(current_user)
# @blob.attachments.map(&:record).all? { |record| record.user == current_user }
# end
# end
module BlobAuthenticatable
extend ActiveSupport::Concern
included do
around_action :wrap_in_authentication
end
module ClassMethods
def auth_resource
@auth_resource || Devise.default_scope
end
private
def blob_authenticatable(resource:)
@auth_resource = resource
end
end
private
def wrap_in_authentication
is_signed_in_and_authorized = send("#{self.class.auth_resource}_signed_in?") \
& can_access_blob?(send("current_#{self.class.auth_resource}"))
if is_signed_in_and_authorized
yield
else
head :unauthorized
end
end
def can_access_blob?(_user)
true
end
end
答案 1 :(得分:2)
如果要对活动存储提供的所有端点实施身份验证,则可以基于original implementation覆盖ActiveStorage::BaseController
:
# app/controllers/active_storage/base_controller.rb
# frozen_string_literal: true
# The base class for all Active Storage controllers.
class ActiveStorage::BaseController < ActionController::Base
before_action :authenticate_user!
include ActiveStorage::SetCurrent
protect_from_forgery with: :exception
end