Skip to content

Commit

Permalink
add priv key from env for GA 4. Copies #6063 to the new GA4 code
Browse files Browse the repository at this point in the history
  • Loading branch information
orangewolf committed Jul 4, 2023
1 parent 6ff8838 commit 5a29f01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
50 changes: 28 additions & 22 deletions app/services/hyrax/analytics/ga4.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# frozen_string_literal: true

require 'oauth2'
require 'signet/oauth_2/client'

# rubocop:disable Metrics/ModuleLength
module Hyrax
module Analytics
# rubocop:disable Metrics/ModuleLength
module Ga4
extend ActiveSupport::Concern
# rubocop:disable Metrics/BlockLength
class_methods do
# Loads configuration options from config/analytics.yml. Expected structure:
# Loads configuration options from config/analytics.yml. You only need PRIVATE_KEY_PATH or
# PRIVATE_KEY_VALUE. VALUE takes precedence.
# Expected structure:
# `analytics:`
# ` ga4:`
# ` app_name: <%= ENV['GOOGLE_OAUTH_APP_NAME']`
# ` app_version: <%= ENV['GOOGLE_OAUTH_APP_VERSION']`
# ` privkey_path: <%= ENV['GOOGLE_OAUTH_PRIVATE_KEY_PATH']`
# ` privkey_value: <%= ENV['GOOGLE_OAUTH_PRIVATE_KEY_VALUE']`
# ` privkey_secret: <%= ENV['GOOGLE_OAUTH_PRIVATE_KEY_SECRET']`
# ` client_email: <%= ENV['GOOGLE_OAUTH_CLIENT_EMAIL']`
# @return [Config]
Expand All @@ -41,26 +45,24 @@ def self.load_from_yaml
new config
end

REQUIRED_KEYS = %w[analytics_id app_name app_version privkey_path privkey_secret client_email].freeze
KEYS = %w[analytics_id app_name app_version privkey_path privkey_value privkey_secret client_email].freeze
REQUIRED_KEYS = %w[analytics_id app_name app_version privkey_secret client_email].freeze

def initialize(config)
@config = config
end

# @return [Boolean] are all the required values present?
def valid?
config_keys = @config.keys
REQUIRED_KEYS.all? { |required| config_keys.include?(required) }
end

REQUIRED_KEYS.each do |key|
class_eval %{ def #{key}; @config.fetch('#{key}'); end }
return false unless @config['privkey_value'].present? || @config['privkey_path'].present?
REQUIRED_KEYS.all? { |required| @config[required].present? }
end

# This method allows setting the analytics id in the initializer
# @deprecated set the analytics id in either ENV['GOOGLE_ANALYTICS_ID'] or config/analytics.yaml
def analytics_id=(value)
@config['analytics_id'] = value
KEYS.each do |key|
# rubocop:disable Style/EvalWithLocation
class_eval %{ def #{key}; @config.fetch('#{key}'); end }
class_eval %{ def #{key}=(value); @config['#{key}'] = value; end }
# rubocop:enable Style/EvalWithLocation
end
end

Expand All @@ -77,14 +79,18 @@ def oauth_client
end

def auth_client(scope)
raise "Private key file for Google analytics was expected at '#{config.privkey_path}', but no file was found." unless File.exist?(config.privkey_path)
private_key = File.read(config.privkey_path)
private_key = Base64.decode64(config.privkey_value) if config.privkey_value.present?
if private_key.blank?
raise "Private key file for Google analytics was expected at '#{config.privkey_path}', but no file was found." unless File.exist?(config.privkey_path)

private_key = File.read(config.privkey_path)
end
Signet::OAuth2::Client.new token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: scope,
issuer: config.client_email,
signing_key: OpenSSL::PKCS12.new(private_key, config.privkey_secret).key,
sub: config.client_email
audience: 'https://accounts.google.com/o/oauth2/token',
scope: scope,
issuer: config.client_email,
signing_key: OpenSSL::PKCS12.new(private_key, config.privkey_secret).key,
sub: config.client_email
end

# Return a user object linked to a Google Analytics account
Expand Down Expand Up @@ -123,7 +129,7 @@ def to_date_range(period)

[start_date, end_date]
end
# rubocop:enabl e Metrics/MethodLength
# rubocop:enable Metrics/MethodLength

def keyword_conversion(date)
case date
Expand Down Expand Up @@ -199,6 +205,6 @@ def total_visitors(period = 'month', date = default_date_range)
end
# rubocop:enable Metrics/BlockLength
end
# rubocop:enable Metrics/ModuleLength
end
end
# rubocop:enable Metrics/ModuleLength
14 changes: 6 additions & 8 deletions app/services/hyrax/analytics/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.load_from_yaml
filename = Rails.root.join('config', 'analytics.yml')
yaml = YAML.safe_load(ERB.new(File.read(filename)).result)
unless yaml
Rails.logger.error("Unable to fetch any keys from #{filename}.")
Hyrax.logger.error("Unable to fetch any keys from #{filename}.")
return new({})
end
config = yaml.fetch('analytics')&.fetch('google', nil)
Expand All @@ -55,7 +55,6 @@ def initialize(config)
# @return [Boolean] are all the required values present?
def valid?
return false unless @config['privkey_value'].present? || @config['privkey_path'].present?

REQUIRED_KEYS.all? { |required| @config[required].present? }
end

Expand Down Expand Up @@ -87,11 +86,11 @@ def auth_client(scope)
private_key = File.read(config.privkey_path)
end
Signet::OAuth2::Client.new token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: scope,
issuer: config.client_email,
signing_key: OpenSSL::PKCS12.new(private_key, config.privkey_secret).key,
sub: config.client_email
audience: 'https://accounts.google.com/o/oauth2/token',
scope: scope,
issuer: config.client_email,
signing_key: OpenSSL::PKCS12.new(private_key, config.privkey_secret).key,
sub: config.client_email
end

# Return a user object linked to a Google Analytics account
Expand Down Expand Up @@ -209,4 +208,3 @@ def total_visitors(period = 'month', date = default_date_range)
# rubocop:enable Metrics/ModuleLength
end
end
# rubocop:enable Metrics/ModuleLength

0 comments on commit 5a29f01

Please sign in to comment.