omniauth-square 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a8b8a2a024dd7a38a02d13cbf52067567c557bc
4
+ data.tar.gz: 98a8a96df1a87b05577a9d6d8743db5839d2f251
5
+ SHA512:
6
+ metadata.gz: 70658d92c5fc8dcaa4b794c9162d85d5549ad2e71293e82b7d76ee194ebc532cb42fcdc1a5862f1384a315f19bac880b30fef33a6f69121317e146c21174e5f6
7
+ data.tar.gz: b8b2f6e4c76a595d7aaedf28af0e50c5493644416f75260dca54faf13cce8214a317c2a44000e097515bb0368baf5c4d764d6e06ae83c206e5152c31532ec170
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ..gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # OmniAuth Square
2
+
3
+ This gem contains the Square strategy for OmniAuth.
4
+
5
+ Square uses the OAuth2 flow, you can read about it here: http://connect.squareup.com
6
+
7
+ ## This gem is unfinished at the moment.
8
+
9
+ ## How To Use It
10
+
11
+ So let's say you're using Rails, you need to add the strategy to your `Gemfile`:
12
+
13
+ gem 'omniauth-square'
14
+
15
+ You can pull them in directly from github e.g.:
16
+
17
+ gem 'omniauth-square', :git => 'https://github.com/danieljacobarcher/omniauth-square.git'
18
+
19
+ Once these are in, you need to add the following to your `config/initializers/omniauth.rb`:
20
+
21
+ Rails.application.config.middleware.use OmniAuth::Builder do
22
+ provider :square, "consumer_key", "consumer_secret"
23
+ end
24
+
25
+ You will obviously have to put in your key and secret, which you get when you register your app with Square (they call them Application Key and Secret Key).
26
+
27
+ Now just follow the README at: https://github.com/intridea/omniauth
28
+
29
+ ## License
30
+
31
+ Copyright (c) 2013 by [Daniel Archer](https://github.com/danieljacobarcher/), [Jen Aprahamian](https://github.com/jennifermarie/), [Adam Bouck](https://github.com/abouck/)
32
+
33
+ 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:
34
+
35
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36
+
37
+ 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.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,2 @@
1
+ require "omniauth-square/version"
2
+ require "omniauth/strategies/square"
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Square
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,74 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Square < OmniAuth::Strategies::OAuth2
6
+ DEFAULT_SCOPE = 'merchant_id'
7
+
8
+ option :client_options, {
9
+ :site => 'https://squareup.com/',
10
+ :authorize_url => 'https://squareup.com/oauth2/authorize',
11
+ :token_url => 'https://squareup.com/oauth2/token'
12
+ }
13
+
14
+ option :access_token_options, {
15
+ mode: :query,
16
+ header_format: 'OAuth %s'
17
+ }
18
+
19
+ option :provider_ignores_state, true
20
+
21
+ def request_phase
22
+ super
23
+ end
24
+
25
+ uid { raw_info['merchant_id'] }
26
+
27
+ info do
28
+ end
29
+
30
+ credentials do
31
+ hash = {'token' => access_token.token}
32
+ hash.merge!('refresh_token' => access_token.refresh_token) if access_token.expires? && access_token.refresh_token
33
+ hash.merge!('expires_at' => access_token.expires_at) if access_token.expires?
34
+ hash.merge!('expires' => access_token.expires?)
35
+ hash.merge!('scope' => raw_info["scopes"] ? raw_info["scopes"].join(" ") : nil)
36
+ prune!(hash)
37
+ end
38
+
39
+ def raw_info
40
+ unless skip_info?
41
+ @raw_info ||= access_token.get('https://connect.squareup.com/v1/me').parsed
42
+ else
43
+ {}
44
+ end
45
+ end
46
+
47
+ def authorize_params
48
+ super.tap do |params|
49
+ %w[state request_type session].each do |v|
50
+ if request.params[v]
51
+ params[v.to_sym] = request.params[v]
52
+
53
+ # to support omniauth-oauth2's auto csrf protection
54
+ session['omniauth.state'] = params[:state] if v == 'state'
55
+ session['omniauth.request_type'] = params[:request_type] if v == 'request_type'
56
+ end
57
+ end
58
+
59
+ params[:scope] ||= DEFAULT_SCOPE
60
+ end
61
+ end
62
+
63
+ private
64
+ def prune!(hash)
65
+ hash.delete_if do |_, value|
66
+ prune!(value) if value.is_a?(Hash)
67
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ OmniAuth.config.add_camelization 'square', 'Square'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "omniauth-square/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "omniauth-square"
7
+ s.version = Omniauth::Square::VERSION
8
+ s.authors = ["Daniel Archer", "Jennifer Aprahamian", "Adam Bouck"]
9
+ s.email = ["me@dja.io", "j.aprahamian@gmail.com", "adam.j.bouck@gmail.com"]
10
+ s.homepage = "https://github.com/danieljacobarcher/omniauth-square"
11
+ s.summary = %q{Square OAuth strategy for OmniAuth}
12
+ s.description = %q{Square OAuth strategy for OmniAuth}
13
+ s.license = "MIT"
14
+
15
+ s.rubyforge_project = "omniauth-square"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency 'omniauth-oauth2'
23
+ s.add_development_dependency 'rspec', '~> 2.7'
24
+ s.add_development_dependency 'rack-test'
25
+ s.add_development_dependency 'simplecov'
26
+ s.add_development_dependency 'webmock'
27
+ end
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-square'
3
+
4
+ describe OmniAuth::Strategies::Square do
5
+ before :each do
6
+ @request = double('Request')
7
+ @request.stub(:params) { {} }
8
+ @client_id = '123'
9
+ @client_secret = 'afalsf'
10
+ @raw_info = {
11
+ 'name' => 'Sebastian Rabuini',
12
+ 'email' => 'sebas@wasabit.com.ar',
13
+ 'bio' => 'Sebas',
14
+ 'blog_url' => 'sebas_blog',
15
+ 'online_bio_url' => 'http://wasabitlabs.com',
16
+ 'twitter_url' => 'http://twitter.com/#!/sebasr',
17
+ 'facebook_url' => 'http://www.facebook.com/sebastian.rabuini',
18
+ 'linkedin_url' => 'http://www.linkedin.com/in/srabuini',
19
+ 'follower_count' => 6,
20
+ 'investor' => false,
21
+ 'locations' => [
22
+ {'id' => 1963, 'tag_type' => 'LocationTag', 'name' => 'buenos aires',
23
+ 'display_name' => 'Buenos Aires',
24
+ 'angellist_url' => 'https://angel.co/buenos-aires'}
25
+ ],
26
+ 'roles' => [
27
+ {'id' => 14726, 'tag_type' => 'RoleTag', 'name' => 'developer',
28
+ 'display_name' => 'Developer',
29
+ 'angellist_url' => 'https://angel.co/developer'},
30
+ {'id' => 14725, 'tag_type' => 'RoleTag', 'name' => 'entrepreneur',
31
+ 'display_name' => 'Entrepreneur',
32
+ 'angellist_url' => 'https://angel.co/entrepreneur-1'}
33
+ ],
34
+ 'skills' => [
35
+ {"id" => 82532, "tag_type" => "SkillTag", "name" => "ruby on rails",
36
+ "display_name" => "Ruby on Rails",
37
+ "angellist_url" => "https://angel.co/ruby-on-rails-1"}
38
+ ],
39
+ 'scopes' => ["email","comment","message","talent"],
40
+ 'angellist_url' => 'https://angel.co/sebasr',
41
+ 'image' => 'https://s3.amazonaws.com/photos.angel.co/users/90585-medium_jpg?1327684569'
42
+ }
43
+ end
44
+
45
+ subject do
46
+ args = [@client_id, @client_secret, @options].compact
47
+ OmniAuth::Strategies::Square.new(nil, *args).tap do |strategy|
48
+ strategy.stub(:request) { @request }
49
+ end
50
+ end
51
+
52
+ it_should_behave_like 'an oauth2 strategy'
53
+
54
+ describe '#client' do
55
+ it 'has correct AngelList site' do
56
+ subject.client.site.should eq('https://angel.co/')
57
+ end
58
+
59
+ it 'has correct authorize url' do
60
+ subject.client.options[:authorize_url].should eq('https://angel.co/api/oauth/authorize')
61
+ end
62
+
63
+ it 'has correct token url' do
64
+ subject.client.options[:token_url].should eq('https://angel.co/api/oauth/token')
65
+ end
66
+ end
67
+
68
+ describe '#info' do
69
+ before :each do
70
+ subject.stub(:raw_info) { @raw_info }
71
+ end
72
+
73
+ context 'when data is present in raw info' do
74
+ it 'returns the combined name' do
75
+ subject.info['name'].should eq('Sebastian Rabuini')
76
+ end
77
+
78
+ it 'returns the bio' do
79
+ subject.info['bio'].should eq('Sebas')
80
+ end
81
+
82
+ it 'returns the image' do
83
+ subject.info['image'].should eq(@raw_info['image'])
84
+ end
85
+
86
+ it "return the email" do
87
+ subject.info['email'].should eq('sebas@wasabit.com.ar')
88
+ end
89
+
90
+ it "return skills" do
91
+ subject.info['skills'].first['name'].should eq("ruby on rails")
92
+ end
93
+ end
94
+ end
95
+
96
+ describe '#authorize_params' do
97
+ before :each do
98
+ subject.stub(:session => {})
99
+ end
100
+
101
+ it 'includes default scope for email' do
102
+ subject.authorize_params['scope'].should eq('email')
103
+ end
104
+ end
105
+
106
+ describe '#credentials' do
107
+ before :each do
108
+ @access_token = double('OAuth2::AccessToken')
109
+ @access_token.stub(:token) { '123' } # Token is always required
110
+ @access_token.stub(:expires?)
111
+ @access_token.stub(:expires_at)
112
+ @access_token.stub(:refresh_token)
113
+ subject.stub(:access_token) { @access_token }
114
+ subject.stub(:raw_info) { @raw_info }
115
+ end
116
+
117
+ it 'returns a Hash' do
118
+ subject.credentials.should be_a(Hash)
119
+ end
120
+
121
+ it 'returns the token' do
122
+ subject.credentials['token'].should eq('123')
123
+ end
124
+
125
+ it "return scopes" do
126
+ subject.credentials['scope'].should eq("email comment message talent")
127
+ end
128
+
129
+ it 'returns the expiry status' do
130
+ @access_token.stub(:expires?) { true }
131
+ subject.credentials['expires'].should eq(true)
132
+
133
+ @access_token.stub(:expires?) { false }
134
+ subject.credentials['expires'].should eq(false)
135
+ end
136
+
137
+ it 'returns the refresh token and expiry time when expiring' do
138
+ ten_mins_from_now = (Time.now + 360).to_i
139
+ @access_token.stub(:expires?) { true }
140
+ @access_token.stub(:refresh_token) { '321' }
141
+ @access_token.stub(:expires_at) { ten_mins_from_now }
142
+ subject.credentials['refresh_token'].should eq('321')
143
+ subject.credentials['expires_at'].should eq(ten_mins_from_now)
144
+ end
145
+
146
+ it 'does not return the refresh token when it is nil and expiring' do
147
+ @access_token.stub(:expires?) { true }
148
+ @access_token.stub(:refresh_token) { nil }
149
+ subject.credentials['refresh_token'].should be_nil
150
+ subject.credentials.should_not have_key('refresh_token')
151
+ end
152
+
153
+ it 'does not return the refresh token when not expiring' do
154
+ @access_token.stub(:expires?) { false }
155
+ @access_token.stub(:refresh_token) { 'XXX' }
156
+ subject.credentials['refresh_token'].should be_nil
157
+ subject.credentials.should_not have_key('refresh_token')
158
+ end
159
+ end
160
+ 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,23 @@
1
+ # NOTE it would be useful if this lived in omniauth-oauth2 eventually
2
+ shared_examples 'an oauth2 strategy' do
3
+ describe '#client' do
4
+ it 'should be initialized with symbolized client_options' do
5
+ @options = { :client_options => { 'authorize_url' => 'https://example.com' } }
6
+ subject.client.options[:authorize_url].should == 'https://example.com'
7
+ end
8
+ end
9
+
10
+ describe '#token_params' do
11
+ it 'should include any authorize params passed in the :authorize_params option' do
12
+ @options = { :token_params => { :foo => 'bar', :baz => 'zip' } }
13
+ subject.token_params['foo'].should eq('bar')
14
+ subject.token_params['baz'].should eq('zip')
15
+ end
16
+
17
+ it 'should include top-level options that are marked as :authorize_options' do
18
+ @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
19
+ subject.token_params['scope'].should eq('bar')
20
+ subject.token_params['foo'].should eq('baz')
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-square
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Archer
8
+ - Jennifer Aprahamian
9
+ - Adam Bouck
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-12-13 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: omniauth-oauth2
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rspec
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '2.7'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: '2.7'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rack-test
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: simplecov
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: webmock
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ description: Square OAuth strategy for OmniAuth
86
+ email:
87
+ - me@dja.io
88
+ - j.aprahamian@gmail.com
89
+ - adam.j.bouck@gmail.com
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - .gitignore
95
+ - Gemfile
96
+ - README.md
97
+ - Rakefile
98
+ - lib/omniauth-square.rb
99
+ - lib/omniauth-square/version.rb
100
+ - lib/omniauth/strategies/square.rb
101
+ - omniauth-square.gemspec
102
+ - spec/omniauth/strategies/square_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/support/shared_examples.rb
105
+ homepage: https://github.com/danieljacobarcher/omniauth-square
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project: omniauth-square
125
+ rubygems_version: 2.1.11
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Square OAuth strategy for OmniAuth
129
+ test_files:
130
+ - spec/omniauth/strategies/square_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/support/shared_examples.rb