cloudflare: use new api token auth scheme.
Use Cloudflare's new API token authentication method to talk to Cloudflare. This lets us use API tokens with minimal privileges set. https://blog.cloudflare.com/api-tokens-general-availability/
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
# donmai.us specific
|
# donmai.us specific
|
||||||
|
|
||||||
class CloudflareService
|
class CloudflareService
|
||||||
def key
|
def enabled?
|
||||||
Danbooru.config.cloudflare_key
|
api_token.present? && zone.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def email
|
def api_token
|
||||||
Danbooru.config.cloudflare_email
|
Danbooru.config.cloudflare_api_token
|
||||||
end
|
end
|
||||||
|
|
||||||
def zone
|
def zone
|
||||||
@@ -15,8 +15,7 @@ class CloudflareService
|
|||||||
|
|
||||||
def options
|
def options
|
||||||
Danbooru.config.httparty_options.deep_merge(headers: {
|
Danbooru.config.httparty_options.deep_merge(headers: {
|
||||||
"X-Auth-Email" => email,
|
"Authorization" => "Bearer #{api_token}",
|
||||||
"X-Auth-Key" => key,
|
|
||||||
"Content-Type" => "application/json",
|
"Content-Type" => "application/json",
|
||||||
"User-Agent" => "#{Danbooru.config.app_name}/#{Rails.application.config.x.git_hash}"
|
"User-Agent" => "#{Danbooru.config.app_name}/#{Rails.application.config.x.git_hash}"
|
||||||
})
|
})
|
||||||
@@ -32,6 +31,8 @@ class CloudflareService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def delete(md5, ext)
|
def delete(md5, ext)
|
||||||
|
return unless enabled?
|
||||||
|
|
||||||
url = "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
|
url = "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
|
||||||
files = ["#{md5}.#{ext}", "preview/#{md5}.jpg", "sample/sample-#{md5}.jpg"].map do |name|
|
files = ["#{md5}.#{ext}", "preview/#{md5}.jpg", "sample/sample-#{md5}.jpg"].map do |name|
|
||||||
["danbooru", "safebooru", "raikou1", "raikou2", "raikou3", "raikou4"].map do |subdomain|
|
["danbooru", "safebooru", "raikou1", "raikou2", "raikou3", "raikou4"].map do |subdomain|
|
||||||
|
|||||||
@@ -630,16 +630,16 @@ module Danbooru
|
|||||||
"https://#{hostname}"
|
"https://#{hostname}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Cloudflare data
|
# Cloudflare API token. Used to purge URLs from Cloudflare's cache when a
|
||||||
def cloudflare_email
|
# post is replaced. The token must have 'zone.cache_purge' permissions.
|
||||||
|
# https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys
|
||||||
|
def cloudflare_api_token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The Cloudflare zone ID. This is the domain that cached URLs will be purged from.
|
||||||
def cloudflare_zone
|
def cloudflare_zone
|
||||||
end
|
end
|
||||||
|
|
||||||
def cloudflare_key
|
|
||||||
end
|
|
||||||
|
|
||||||
def recommender_server
|
def recommender_server
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user