omniauth-shopify-oauth2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-shopify-oauth2 (1.0.0)
5
+ omniauth-oauth2 (~> 1.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.6)
11
+ diff-lcs (1.1.3)
12
+ faraday (0.7.6)
13
+ addressable (~> 2.2)
14
+ multipart-post (~> 1.1)
15
+ rack (~> 1.1)
16
+ hashie (1.2.0)
17
+ multi_json (1.0.4)
18
+ multipart-post (1.1.4)
19
+ oauth2 (0.5.2)
20
+ faraday (~> 0.7)
21
+ multi_json (~> 1.0)
22
+ omniauth (1.0.2)
23
+ hashie (~> 1.2)
24
+ rack
25
+ omniauth-oauth2 (1.0.0)
26
+ oauth2 (~> 0.5.0)
27
+ omniauth (~> 1.0)
28
+ rack (1.4.1)
29
+ rake (0.9.2.2)
30
+ rspec (2.7.0)
31
+ rspec-core (~> 2.7.0)
32
+ rspec-expectations (~> 2.7.0)
33
+ rspec-mocks (~> 2.7.0)
34
+ rspec-core (2.7.1)
35
+ rspec-expectations (2.7.0)
36
+ diff-lcs (~> 1.1.2)
37
+ rspec-mocks (2.7.0)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ omniauth-shopify-oauth2!
44
+ rake
45
+ rspec (~> 2.7.0)
@@ -0,0 +1,67 @@
1
+ # OmniAuth Shopify
2
+
3
+ Shopify OAuth2 Strategy for OmniAuth 1.0.
4
+
5
+ ## Installing
6
+
7
+ Add to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'omniauth-shopify-oauth2'
11
+ ```
12
+
13
+ Then `bundle install`.
14
+
15
+ ## Usage
16
+
17
+ `OmniAuth::Strategies::Shopify` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
18
+
19
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
20
+
21
+ ```ruby
22
+ Rails.application.config.middleware.use OmniAuth::Builder do
23
+ provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET']
24
+ end
25
+ ```
26
+
27
+ ## Configuring
28
+
29
+ You can configure the scope, which you pass in to the `provider` method via a `Hash`:
30
+
31
+ * `scope`: A comma-separated list of permissions you want to request from the user. See the Shopify API docs for a full list of available permissions: http://api.shopify.com.
32
+
33
+ * `setup`: A lambda which dynamically sets the `site`. You must initiate the omniauth process by passing in a shop query parameter of the shop you're requesting permissions for. Ex. http://myapp.com/auth/shopify?shop=example.myshopify.com
34
+
35
+ For example, to request `read_products`, `read_orders` and `write_content` permissions and display the authentication page:
36
+
37
+ ```ruby
38
+ Rails.application.config.middleware.use OmniAuth::Builder do
39
+ provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'],
40
+ :scope => 'read_products,read_orders,write_content',
41
+ :setup => lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING'])
42
+ env['omniauth.strategy'].options[:client_options][:site] = "http://#{params['shop']}" }
43
+ end
44
+ ```
45
+
46
+ ## Authentication Hash
47
+
48
+ Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
49
+
50
+ ```ruby
51
+ {
52
+ :provider => 'shopify',
53
+ :credentials => {
54
+ :token => 'afasd923kjh0934kf', # OAuth 2.0 access_token, which you store and use to authenticate API requests
55
+ }
56
+ }
57
+ ```
58
+
59
+ ## License
60
+
61
+ Copyright (c) 2012 by Denis Odorcic
62
+
63
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rack'
4
+
5
+ gem 'sinatra'
6
+ gem 'omniauth-shopify-oauth2', :path => '../'
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ omniauth-shopify-oauth2 (1.0.0)
5
+ omniauth-oauth2 (~> 1.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.6)
11
+ faraday (0.7.6)
12
+ addressable (~> 2.2)
13
+ multipart-post (~> 1.1)
14
+ rack (~> 1.1)
15
+ hashie (1.2.0)
16
+ multi_json (1.0.4)
17
+ multipart-post (1.1.4)
18
+ oauth2 (0.5.2)
19
+ faraday (~> 0.7)
20
+ multi_json (~> 1.0)
21
+ omniauth (1.0.2)
22
+ hashie (~> 1.2)
23
+ rack
24
+ omniauth-oauth2 (1.0.0)
25
+ oauth2 (~> 0.5.0)
26
+ omniauth (~> 1.0)
27
+ rack (1.4.1)
28
+ rack-protection (1.2.0)
29
+ rack
30
+ sinatra (1.3.2)
31
+ rack (~> 1.3, >= 1.3.6)
32
+ rack-protection (~> 1.2)
33
+ tilt (~> 1.3, >= 1.3.3)
34
+ tilt (1.3.3)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ omniauth-shopify-oauth2!
41
+ rack
42
+ sinatra
@@ -0,0 +1,46 @@
1
+ require 'bundler/setup'
2
+ require 'sinatra/base'
3
+ require 'omniauth-shopify-oauth2'
4
+
5
+ SCOPE = 'read_products,read_orders,read_customers,write_shipping'
6
+
7
+ class App < Sinatra::Base
8
+ get '/auth/:provider/callback' do
9
+ <<-HTML
10
+ <html>
11
+ <head>
12
+ <title>Shopify Oauth2</title>
13
+ </head>
14
+ <body>
15
+ <h3>Authorized</h3>
16
+ <p>Token: #{request.env['omniauth.auth']['credentials']['token']}</p>
17
+ </body>
18
+ </html>
19
+ HTML
20
+ end
21
+
22
+ get '/auth/failure' do
23
+ <<-HTML
24
+ <html>
25
+ <head>
26
+ <title>Shopify Oauth2</title>
27
+ </head>
28
+ <body>
29
+ <h3>Failed Authorization</h3>
30
+ <p>Message: #{params[:message]}</p>
31
+ </body>
32
+ </html>
33
+ HTML
34
+ end
35
+ end
36
+
37
+ use Rack::Session::Cookie
38
+
39
+ use OmniAuth::Builder do
40
+ provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'],
41
+ :scope => SCOPE,
42
+ :setup => lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING'])
43
+ env['omniauth.strategy'].options[:client_options][:site] = "https://#{params['shop']}" }
44
+ end
45
+
46
+ run App.new
@@ -0,0 +1 @@
1
+ require 'omniauth/shopify'
@@ -0,0 +1,2 @@
1
+ require 'omniauth/shopify/version'
2
+ require 'omniauth/strategies/shopify'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Shopify
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Shopify < OmniAuth::Strategies::OAuth2
6
+ # Available scopes: content themes products customers orders script_tags shipping
7
+ # read_* or write_*
8
+ DEFAULT_SCOPE = 'read_products'
9
+
10
+ option :client_options, {
11
+ :authorize_url => '/admin/oauth/authorize',
12
+ :token_url => '/admin/oauth/access_token'
13
+ }
14
+
15
+ option :callback_url
16
+
17
+ def authorize_params
18
+ super.tap do |params|
19
+ params[:scope] ||= DEFAULT_SCOPE
20
+ end
21
+ end
22
+
23
+ def callback_url
24
+ options.callback_url || super
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'omniauth/shopify/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'omniauth-shopify-oauth2'
7
+ s.version = OmniAuth::Shopify::VERSION
8
+ s.authors = ['Denis Odorcic']
9
+ s.email = ['denis.odorcic@shopify.com']
10
+ s.summary = 'Shopify strategy for OmniAuth'
11
+ s.homepage = 'https://github.com/Shopify/omniauth-shopify-oauth2'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
16
+ s.require_paths = ['lib']
17
+
18
+ s.add_runtime_dependency 'omniauth-oauth2', '~> 1.0.0'
19
+
20
+ s.add_development_dependency 'rspec', '~> 2.7.0'
21
+ s.add_development_dependency 'rake'
22
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-shopify-oauth2'
3
+ require 'base64'
4
+
5
+ describe OmniAuth::Strategies::Shopify do
6
+ before :each do
7
+ @request = double('Request')
8
+ @request.stub(:params) { {} }
9
+ @request.stub(:cookies) { {} }
10
+
11
+ @client_id = '123'
12
+ @client_secret = '53cr3tz'
13
+ @options = {:client_options => {:site => 'https://example.myshopify.com'}}
14
+ end
15
+
16
+ subject do
17
+ args = [@client_id, @client_secret, @options].compact
18
+ OmniAuth::Strategies::Shopify.new(nil, *args).tap do |strategy|
19
+ strategy.stub(:request) { @request }
20
+ end
21
+ end
22
+
23
+ describe '#client' do
24
+ it 'has correct shopify site' do
25
+ subject.client.site.should eq('https://example.myshopify.com')
26
+ end
27
+
28
+ it 'has correct authorize url' do
29
+ subject.client.options[:authorize_url].should eq('/admin/oauth/authorize')
30
+ end
31
+
32
+ it 'has correct token url' do
33
+ subject.client.options[:token_url].should eq('/admin/oauth/access_token')
34
+ end
35
+ end
36
+
37
+ describe '#callback_url' do
38
+ it "returns value from #callback_url" do
39
+ url = 'http://auth.myapp.com/auth/callback'
40
+ @options = {:callback_url => url}
41
+ subject.callback_url.should eq(url)
42
+ end
43
+
44
+ it "defaults to callback" do
45
+ url_base = 'http://auth.request.com'
46
+ @request.stub(:url) { "#{url_base}/page/path" }
47
+ subject.stub(:script_name) { "" } # to not depend from Rack env
48
+ subject.callback_url.should eq("#{url_base}/auth/shopify/callback")
49
+ end
50
+ end
51
+
52
+ describe '#authorize_params' do
53
+ it 'includes default scope for read_products' do
54
+ subject.authorize_params.should be_a(Hash)
55
+ subject.authorize_params[:scope].should eq('read_products')
56
+ end
57
+
58
+ it 'includes custom scope' do
59
+ @options = {:scope => 'write_products'}
60
+ subject.authorize_params.should be_a(Hash)
61
+ subject.authorize_params[:scope].should eq('write_products')
62
+ end
63
+ end
64
+
65
+ describe '#credentials' do
66
+ before :each do
67
+ @access_token = double('OAuth2::AccessToken')
68
+ @access_token.stub(:token)
69
+ @access_token.stub(:expires?)
70
+ @access_token.stub(:expires_at)
71
+ @access_token.stub(:refresh_token)
72
+ subject.stub(:access_token) { @access_token }
73
+ end
74
+
75
+ it 'returns a Hash' do
76
+ subject.credentials.should be_a(Hash)
77
+ end
78
+
79
+ it 'returns the token' do
80
+ @access_token.stub(:token) { '123' }
81
+ subject.credentials['token'].should eq('123')
82
+ end
83
+
84
+ it 'returns the expiry status' do
85
+ @access_token.stub(:expires?) { true }
86
+ subject.credentials['expires'].should eq(true)
87
+
88
+ @access_token.stub(:expires?) { false }
89
+ subject.credentials['expires'].should eq(false)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
4
+
5
+ RSpec.configure do |config|
6
+ end
@@ -0,0 +1,36 @@
1
+ shared_examples 'an oauth2 strategy' do
2
+ describe '#client' do
3
+ it 'should be initialized with symbolized client_options' do
4
+ @options = { :client_options => { 'authorize_url' => 'https://example.com' } }
5
+ subject.client.options[:authorize_url].should == 'https://example.com'
6
+ end
7
+ end
8
+
9
+ describe '#authorize_params' do
10
+ it 'should include any authorize params passed in the :authorize_params option' do
11
+ @options = { :authorize_params => { :foo => 'bar', :baz => 'zip' } }
12
+ subject.authorize_params['foo'].should eq('bar')
13
+ subject.authorize_params['baz'].should eq('zip')
14
+ end
15
+
16
+ it 'should include top-level options that are marked as :authorize_options' do
17
+ @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
18
+ subject.authorize_params['scope'].should eq('bar')
19
+ subject.authorize_params['foo'].should eq('baz')
20
+ end
21
+ end
22
+
23
+ describe '#token_params' do
24
+ it 'should include any token params passed in the :token_params option' do
25
+ @options = { :token_params => { :foo => 'bar', :baz => 'zip' } }
26
+ subject.token_params['foo'].should eq('bar')
27
+ subject.token_params['baz'].should eq('zip')
28
+ end
29
+
30
+ it 'should include top-level options that are marked as :token_options' do
31
+ @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
32
+ subject.token_params['scope'].should eq('bar')
33
+ subject.token_params['foo'].should eq('baz')
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-shopify-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Denis Odorcic
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-05 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: omniauth-oauth2
23
+ type: :runtime
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ prerelease: false
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ type: :development
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 2
48
+ - 7
49
+ - 0
50
+ version: 2.7.0
51
+ prerelease: false
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ type: :development
56
+ version_requirements: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ prerelease: false
66
+ requirement: *id003
67
+ description:
68
+ email:
69
+ - denis.odorcic@shopify.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README.md
81
+ - Rakefile
82
+ - example/Gemfile
83
+ - example/Gemfile.lock
84
+ - example/config.ru
85
+ - lib/omniauth-shopify-oauth2.rb
86
+ - lib/omniauth/shopify.rb
87
+ - lib/omniauth/shopify/version.rb
88
+ - lib/omniauth/strategies/shopify.rb
89
+ - omniauth-shopify-oauth2.gemspec
90
+ - spec/omniauth/strategies/shopify_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/support/shared_examples.rb
93
+ has_rdoc: true
94
+ homepage: https://github.com/Shopify/omniauth-shopify-oauth2
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.5.3
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Shopify strategy for OmniAuth
127
+ test_files:
128
+ - spec/omniauth/strategies/shopify_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/shared_examples.rb