omniauth-harvest 0.1.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,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-harvest (0.1.0)
5
+ omniauth-oauth2 (~> 1.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.3)
11
+ faraday (0.8.0)
12
+ multipart-post (~> 1.1)
13
+ hashie (1.2.0)
14
+ httpauth (0.1)
15
+ multi_json (1.3.4)
16
+ multipart-post (1.1.5)
17
+ oauth2 (0.6.1)
18
+ faraday (~> 0.7)
19
+ httpauth (~> 0.1)
20
+ multi_json (~> 1.3)
21
+ omniauth (1.1.0)
22
+ hashie (~> 1.2)
23
+ rack
24
+ omniauth-oauth2 (1.0.2)
25
+ oauth2 (~> 0.6.0)
26
+ omniauth (~> 1.0)
27
+ rack (1.4.1)
28
+ rake (0.9.2.2)
29
+ rspec (2.10.0)
30
+ rspec-core (~> 2.10.0)
31
+ rspec-expectations (~> 2.10.0)
32
+ rspec-mocks (~> 2.10.0)
33
+ rspec-core (2.10.0)
34
+ rspec-expectations (2.10.0)
35
+ diff-lcs (~> 1.1.3)
36
+ rspec-mocks (2.10.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ omniauth-harvest!
43
+ rake
44
+ rspec
@@ -0,0 +1,60 @@
1
+ # OmniAuth Harvest
2
+
3
+ Harvest OAuth2 Strategy for OmniAuth 1.0. Largely inspired by Shopify's OmniAuth strategy: https://github.com/Shopify/omniauth-shopify-oauth2
4
+
5
+ ## Installing
6
+
7
+ Add to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'omniauth-harvest'
11
+ ```
12
+
13
+ Then run `bundle install`.
14
+
15
+ ## Usage
16
+
17
+ Read the OmniAuth 1.0 docs for detailed instructions on using OmniAuth: https://github.com/intridea/omniauth.
18
+
19
+ You will need to set up your application in your Harvest account `https://<companyname>.harvestapp.com/oauth2_clients`. Support for receiving these details from users (for accessing multiple accounts) will be coming soon.
20
+
21
+ To use this in Rails, for example, add the following in `config/initializers/omniauth.rb`:
22
+
23
+ ```ruby
24
+ Rails.application.config.middleware.use OmniAuth::Builder do
25
+ provider :harvest, ENV['HARVEST_IDENTIFIER'], ENV['HARVEST_SECRET']
26
+ end
27
+ ```
28
+
29
+ There is also a simple Sinatra example in ./example/config.ru
30
+
31
+ ## Authentication Hash
32
+
33
+ Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
34
+
35
+ ```ruby
36
+ {
37
+ :provider => 'harvest',
38
+ :credentials => {
39
+ :token => 'afasd923kjh0934kf', # OAuth 2.0 access_token; use this for authenticating API requests
40
+ }
41
+ }
42
+ ```
43
+
44
+ ## License
45
+ Copyright (c) 2012 by Core
46
+
47
+ 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:
48
+
49
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
50
+
51
+ 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.
52
+
53
+ ### Based on the Shopify OmniAuth Strategy
54
+ Copyright (c) 2012 by Shopify Inc
55
+
56
+ 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:
57
+
58
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
59
+
60
+ 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-harvest', :path => '../'
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ omniauth-harvest (0.1.0)
5
+ omniauth-oauth2 (~> 1.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ faraday (0.8.0)
11
+ multipart-post (~> 1.1)
12
+ hashie (1.2.0)
13
+ httpauth (0.1)
14
+ multi_json (1.3.4)
15
+ multipart-post (1.1.5)
16
+ oauth2 (0.6.1)
17
+ faraday (~> 0.7)
18
+ httpauth (~> 0.1)
19
+ multi_json (~> 1.3)
20
+ omniauth (1.1.0)
21
+ hashie (~> 1.2)
22
+ rack
23
+ omniauth-oauth2 (1.0.2)
24
+ oauth2 (~> 0.6.0)
25
+ omniauth (~> 1.0)
26
+ rack (1.4.1)
27
+ rack-protection (1.2.0)
28
+ rack
29
+ sinatra (1.3.2)
30
+ rack (~> 1.3, >= 1.3.6)
31
+ rack-protection (~> 1.2)
32
+ tilt (~> 1.3, >= 1.3.3)
33
+ tilt (1.3.3)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ omniauth-harvest!
40
+ rack
41
+ sinatra
@@ -0,0 +1,41 @@
1
+ require 'bundler/setup'
2
+ require 'sinatra/base'
3
+ require 'omniauth-harvest'
4
+
5
+ class App < Sinatra::Base
6
+ get '/auth/:provider/callback' do
7
+ <<-HTML
8
+ <html>
9
+ <head>
10
+ <title>Harvest Oauth2</title>
11
+ </head>
12
+ <body>
13
+ <h3>Authorized</h3>
14
+ <p>Token: #{request.env['omniauth.auth']['credentials']['token']}</p>
15
+ </body>
16
+ </html>
17
+ HTML
18
+ end
19
+
20
+ get '/auth/failure' do
21
+ <<-HTML
22
+ <html>
23
+ <head>
24
+ <title>Harvest Oauth2</title>
25
+ </head>
26
+ <body>
27
+ <h3>Failed Authorization</h3>
28
+ <p>Message: #{params[:message]}</p>
29
+ </body>
30
+ </html>
31
+ HTML
32
+ end
33
+ end
34
+
35
+ use Rack::Session::Cookie
36
+
37
+ use OmniAuth::Builder do
38
+ provider :harvest, ENV['HARVEST_IDENTIFIER'], ENV['HARVEST_SECRET']
39
+ end
40
+
41
+ run App.new
@@ -0,0 +1 @@
1
+ require 'omniauth/harvest'
@@ -0,0 +1,2 @@
1
+ require 'omniauth/harvest/version'
2
+ require 'omniauth/strategies/harvest'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Harvest
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Harvest < OmniAuth::Strategies::OAuth2
6
+ option :client_options, {
7
+ :site => 'https://api.harvestapp.com',
8
+ :authorize_url => '/oauth2/authorize',
9
+ :token_url => '/oauth2/token'
10
+ }
11
+
12
+ def authorize_params
13
+ super.tap do |params|
14
+ params[:response_type] = "code"
15
+ params[:client_id] = client.id
16
+ params[:redirect_uri] ||= callback_url
17
+ end
18
+ end
19
+
20
+ def request_phase
21
+ super
22
+ end
23
+
24
+ def build_access_token
25
+ token_params = {
26
+ :code => request.params['code'],
27
+ :redirect_uri => callback_url,
28
+ :client_id => client.id,
29
+ :client_secret => client.secret,
30
+ :grant_type => 'authorization_code'
31
+ }
32
+ client.get_token(token_params)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'omniauth/harvest/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'omniauth-harvest'
7
+ s.version = OmniAuth::Harvest::VERSION
8
+ s.authors = ['Robert May']
9
+ s.email = ['robotmay@gmail.com']
10
+ s.summary = 'Harvest strategy for OmniAuth'
11
+ s.homepage = 'https://github.com/corewebdesign/omniauth-harvest'
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'
21
+ s.add_development_dependency 'rake'
22
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-harvest'
3
+ require 'base64'
4
+
5
+ describe OmniAuth::Strategies::Harvest 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 = {}
14
+ end
15
+
16
+ subject do
17
+ args = [@client_id, @client_secret, @options].compact
18
+ OmniAuth::Strategies::Harvest.new(nil, *args).tap do |strategy|
19
+ strategy.stub(:request) { @request }
20
+ end
21
+ end
22
+
23
+ describe '#client' do
24
+ it 'has correct authorize url' do
25
+ subject.client.options[:authorize_url].should eq('/oauth2/authorize')
26
+ end
27
+
28
+ it 'has correct token url' do
29
+ subject.client.options[:token_url].should eq('/oauth2/token')
30
+ end
31
+ end
32
+
33
+ describe '#credentials' do
34
+ before :each do
35
+ @access_token = double('OAuth2::AccessToken')
36
+ @access_token.stub(:token)
37
+ @access_token.stub(:expires?)
38
+ @access_token.stub(:expires_at)
39
+ @access_token.stub(:refresh_token)
40
+ subject.stub(:access_token) { @access_token }
41
+ end
42
+
43
+ it 'returns a Hash' do
44
+ subject.credentials.should be_a(Hash)
45
+ end
46
+
47
+ it 'returns the token' do
48
+ @access_token.stub(:token) { '123' }
49
+ subject.credentials['token'].should eq('123')
50
+ end
51
+
52
+ it 'returns the expiry status' do
53
+ @access_token.stub(:expires?) { true }
54
+ subject.credentials['expires'].should eq(true)
55
+
56
+ @access_token.stub(:expires?) { false }
57
+ subject.credentials['expires'].should eq(false)
58
+ end
59
+ end
60
+ 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,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-harvest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert May
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth-oauth2
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description:
63
+ email:
64
+ - robotmay@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - README.md
73
+ - Rakefile
74
+ - example/Gemfile
75
+ - example/Gemfile.lock
76
+ - example/config.ru
77
+ - lib/omniauth-harvest.rb
78
+ - lib/omniauth/harvest.rb
79
+ - lib/omniauth/harvest/version.rb
80
+ - lib/omniauth/strategies/harvest.rb
81
+ - omniauth-harvest.gemspec
82
+ - spec/omniauth/strategies/harvest_spec.rb
83
+ - spec/spec_helper.rb
84
+ - spec/support/shared_examples.rb
85
+ homepage: https://github.com/corewebdesign/omniauth-harvest
86
+ licenses: []
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.24
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Harvest strategy for OmniAuth
109
+ test_files:
110
+ - spec/omniauth/strategies/harvest_spec.rb
111
+ - spec/spec_helper.rb
112
+ - spec/support/shared_examples.rb