Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ build-iPhoneSimulator/

# Used by RuboCop. Remote config files pulled in from inherit_from directive.
# .rubocop-https?--*

# IDE Files
.idea/
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
# smoothcode-client-ruby
# 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
```
***
1 change: 1 addition & 0 deletions lib/smoothcode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'smoothcode/auth'
17 changes: 17 additions & 0 deletions lib/smoothcode/auth.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/smoothcode/utils.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions smoothcode.gemspec
Original file line number Diff line number Diff line change
@@ -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