Move caching in to main action

This commit is contained in:
2023-11-18 12:06:39 +11:00
parent 0442e7cd6c
commit 07b0b92da6
2 changed files with 5 additions and 49 deletions

View File

@@ -11,6 +11,7 @@ module Adamantium
class Action < Hanami::Action class Action < Hanami::Action
include Deps["logger", include Deps["logger",
"settings", "settings",
"view_cache.cacher",
not_found_view: "views.not_found", not_found_view: "views.not_found",
error_view: "views.error", error_view: "views.error",
sentry: "sentry.client"] sentry: "sentry.client"]
@@ -21,30 +22,8 @@ module Adamantium
handle_exception ROM::TupleCountMismatchError => :not_found handle_exception ROM::TupleCountMismatchError => :not_found
handle_exception StandardError => :handle_error handle_exception StandardError => :handle_error
def authenticate!(req, res) def cache(key:, content:)
halt 400 if req.env["HTTP_AUTHORIZATION"] && req.params[:access_token] cacher.call(key: key, content: content, expiry: TimeMath.min.advance(Time.now, +10))
if Hanami.env == :development || Hanami.env == :test
req.env[:scopes] = verify_token(nil)
return true
end
# Pull out and verify the authorization header or access_token
if req.env["HTTP_AUTHORIZATION"]
header = req.env["HTTP_AUTHORIZATION"].match(/Bearer (.*)$/)
access_token = header[1] unless header.nil?
elsif req.params["access_token"]
access_token = req.params["access_token"]
else
logger.error "Received request without a token"
halt 401
end
req.env[:access_token] = access_token
# Verify the token and extract scopes
req.env[:scopes] = verify_token(access_token)
end end
def not_found(_req, res, _exception) def not_found(_req, res, _exception)
@@ -61,28 +40,5 @@ module Adamantium
res.render error_view res.render error_view
res.headers["Cache-Control"] = "no-store, max-age=0" res.headers["Cache-Control"] = "no-store, max-age=0"
end end
def verify_scope(req:, scope:)
req.env[:scopes].include? scope
end
private
def verify_token(access_token)
return %i[create update delete undelete media] if settings.shortcut_key == access_token
return %i[create update delete undelete media] if Hanami.env == :development || Hanami.env == :test
resp = HTTParty.get(settings.micropub_token_endpoint, {
headers: {
"Accept" => "application/x-www-form-urlencoded",
"Authorization" => "Bearer #{access_token}"
}
})
decoded_response = URI.decode_www_form(resp.body).to_h.transform_keys(&:to_sym)
halt 401 unless (decoded_response.include? :scope) && (decoded_response.include? :me)
decoded_response[:scope].gsub("post", "create").split.map(&:to_sym)
end
end end
end end

View File

@@ -4,10 +4,10 @@ module Adamantium
module Actions module Actions
module RecentlyPlayed module RecentlyPlayed
class Index < Action class Index < Action
include Deps["views.recently_played.index", "view_cache.cacher"] include Deps["views.recently_played.index"]
def handle(req, res) def handle(req, res)
res.body = cacher.call(key: "recently_played", content: index.call.to_str, expiry: TimeMath.min.advance(Time.now, +10)) res.body = cache(key: "recently_played", content: index.call.to_str)
end end
end end
end end