diff --git a/.gitignore b/.gitignore index e3200e0..3335ce1 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ build-iPhoneSimulator/ # Used by RuboCop. Remote config files pulled in from inherit_from directive. # .rubocop-https?--* + +# IDE Files +.idea/ diff --git a/README.md b/README.md index 9f5f2f7..2f027a4 100644 --- a/README.md +++ b/README.md @@ -1 +1,33 @@ -# smoothcode-client-ruby \ No newline at end of file +# SmoothCode Client Ruby + +## Introduction +Ruby Client that exposes utility functions to authenticate SmoothCode requests + +## Installation +```shell +gem install smoothcode +``` + +## Usage +This library exposes 2 methods +* `dashboard_request?(shop)` - This method verifies if the request for accessing the dashboard is coming from `SmoothCode` +```ruby +require 'smoothcode' + +# SmoothCode sends query parameters to the URL +# shop -> Shopify Shop in the form: `test.myshopify.com` +# hmac -> HMAC of the shop signed by your App Client Secret (can be obtained from SmoothCode Dashboard in App Settings) + +SmoothCodeAuth.new(request_hmac, client_secret).dashboard_request?(shop) # returns True if the request is valid +``` + +* `webhook_request?(webhook_data)` - This method verifies if the webhook request is coming from `SmoothCode` +```ruby +require 'smoothcode' + +# SmoothCode sends hmac in the Authorization Header of the request +# It is hmac of the webhook data signed by your App Client Secret + +SmoothCodeAuth.new(request_hmac, client_secret).webhook_request?(webhook_data) # returns True if the request is valid +``` +*** diff --git a/lib/smoothcode.rb b/lib/smoothcode.rb new file mode 100644 index 0000000..486221c --- /dev/null +++ b/lib/smoothcode.rb @@ -0,0 +1 @@ +require 'smoothcode/auth' diff --git a/lib/smoothcode/auth.rb b/lib/smoothcode/auth.rb new file mode 100644 index 0000000..87aff64 --- /dev/null +++ b/lib/smoothcode/auth.rb @@ -0,0 +1,17 @@ +require_relative 'utils' +require 'json' + +class SmoothCodeAuth + def initialize(request_hmac, client_secret) + @request_hmac = request_hmac + @client_secret = client_secret + end + + def dashboard_request?(shop) + generate_hmac(@client_secret, shop) == @request_hmac + end + + def webhook_request?(webhook_data) + generate_hmac(@client_secret, webhook_data.to_json) == @request_hmac + end +end diff --git a/lib/smoothcode/utils.rb b/lib/smoothcode/utils.rb new file mode 100644 index 0000000..9bb503f --- /dev/null +++ b/lib/smoothcode/utils.rb @@ -0,0 +1,7 @@ +require 'openssl' + +def generate_hmac(secret, base_string, digest_alg = 'sha256') + digest = OpenSSL::Digest.new(digest_alg) + + OpenSSL::HMAC.hexdigest(digest, secret, base_string) +end diff --git a/smoothcode.gemspec b/smoothcode.gemspec new file mode 100644 index 0000000..8b5a74b --- /dev/null +++ b/smoothcode.gemspec @@ -0,0 +1,11 @@ +Gem::Specification.new do |s| + s.name = 'smoothcode' + s.version = '0.0.2' + s.summary = 'Ruby client to interact with SmoothCode' + s.description = 'Ruby client to interact with SmoothCode' + s.authors = ['SmoothCode'] + s.email = 'hello@smoothcode.io' + s.files = %w[lib/smoothcode.rb lib/smoothcode/auth.rb lib/smoothcode/utils.rb] + s.homepage = 'https://github.com/Smooth-Code-IO/smoothcode-client-ruby' + s.license = 'MIT' +end