Created
September 13, 2018 17:19
-
-
Save dav/42340d03a6e7f0e2efb1d94356e85d6c to your computer and use it in GitHub Desktop.
Ruby script for generating Shopify app install link from local ENV, .env or a heroku deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'base64' | |
require 'openssl' | |
HOST = 'https://www.example.com' | |
if ARGV.length != 1 | |
STDERR.puts "Pass in <shop> as param" | |
exit | |
end | |
shop = ARGV[0] | |
@host = 'http://localhost:3000' | |
def fetch_dot_env_secret | |
return nil unless File.exists? '.env' | |
File.open('.env').readlines.each do |line| | |
if line =~ /^SHOPIFY_APP_CLIENT_SECRET=(.*)$/ | |
return Regexp.last_match(1) | |
end | |
end | |
nil | |
end | |
client_secrets = {} | |
if (env_secret = ENV['SHOPIFY_APP_CLIENT_SECRET']) | |
client_secrets['From ENV SHOPIFY_APP_CLIENT_SECRET'] = -> { env_secret } | |
end | |
if (dot_env_secret = fetch_dot_env_secret) | |
client_secrets['From .env'] = -> { dot_env_secret } | |
end | |
if (heroku_secret = `heroku config:get SHOPIFY_APP_CLIENT_SECRET -a my-heroku-app`.chomp) | |
client_secrets['From heroku'] = -> { @host = HOST; heroku_secret } | |
end | |
puts "Select secret to use:" | |
client_secrets.keys.each_with_index do |key, i| | |
puts "#{i}] #{key}" | |
end | |
index = STDIN.gets | |
key = client_secrets.keys[index.to_i] | |
client_secret = (client_secrets[key]).call | |
data = "shop=#{shop}.myshopify.com×tamp=#{Time.now.to_i}" | |
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new(), client_secret, data) | |
puts '-------' | |
puts key | |
puts "#{@host}/my-shopify-app?#{data}&hmac=#{hmac}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment