omniauth-clef 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YmI5NTMwMmU3NDExNTZlZjE5NTEzN2EyZDk3M2QyODQ3NmNkY2I2Mw==
5
+ data.tar.gz: !binary |-
6
+ NjgzOGUzYTY5NzRlYzViN2EwNzJiMDZmODE3MmYwMjFkYTUwNWI0Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ N2UwZTc4YjczMGM1ZTEyYmM5M2Q1MWU1NjBmOTE3ZmE0OWY3MmU5ZTY3MzFl
10
+ NTUzNDY1YjJiNzMzNjM0MzZmOGMxMzhlZTFmOWFiYWIwNWM3M2NhYzQwYjAw
11
+ MzVlODA5OThmMDZiZjE3NWRjOWJmYmE1NzIxYWQ4ZTdhYzc5ZDk=
12
+ data.tar.gz: !binary |-
13
+ NTNlY2U5MjRlNzhmOGI0NWY5N2E4N2VhMDM4MjVkOWUwYzU0NDQ5Y2M2M2Jj
14
+ ZWYzMWM4MzQ2NWY3ODMxZDRlYTJmYzJlM2RhNDViZjY4ZGZiMmNjNzRiMjQ2
15
+ ZWU5OGYzODVhMDk4MzJhOTJjOWE2NDZjMzBhYzc1Y2Q5NzcyMDU=
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ .rspec
4
+ /Gemfile.lock
5
+ pkg/*
6
+ .powenv
7
+ tmp
8
+ bin
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby
6
+ branches:
7
+ only:
8
+ - master
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'jruby-openssl', :platform => :jruby
@@ -0,0 +1,83 @@
1
+ # OmniAuth Clef
2
+
3
+ The official Clef OAuth2 Strategy for OmniAuth 1.0.
4
+
5
+ For details on the client side authentication scheme for Clef, refer to the documentation at: http://docs.clef.io
6
+
7
+ ## Installing
8
+
9
+ Add to your `Gemfile`:
10
+
11
+ ```ruby
12
+ gem 'omniauth'
13
+ gem 'omniauth-clef'
14
+ ```
15
+
16
+ Then `bundle install`.
17
+
18
+ ## Usage
19
+
20
+ `OmniAuth::Strategies::Clef` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
21
+
22
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
23
+
24
+ ```ruby
25
+ Rails.application.config.middleware.use OmniAuth::Builder do
26
+ provider :clef, ENV['CLEF_ID'], ENV['CLEF_SECRET']
27
+ end
28
+ ```
29
+
30
+ ### Custom Callback URL/Path
31
+
32
+ You can set a custom `callback_url` or `callback_path` option to override the default value. See [OmniAuth::Strategy#callback_url](https://github.com/intridea/omniauth/blob/master/lib/omniauth/strategy.rb#L411) for more details on the default.
33
+
34
+ ## Auth Hash
35
+
36
+ Here's an example *Auth Hash* available in `request.env['omniauth.auth']`:
37
+
38
+ ```ruby
39
+ {
40
+ :provider => 'clef',
41
+ :uid => '1234567',
42
+ :info => {
43
+ :email => 'test@example.com',
44
+ :first_name => 'Clef',
45
+ :last_name => 'User',
46
+ :phone_number => '2021119999'
47
+ },
48
+ :extra => {
49
+ :raw_info => {
50
+ :id => '1234567',
51
+ :first_name => 'Clef',
52
+ :last_name => 'User',
53
+ :email => 'test@example.com',
54
+ :phone_number => '2021119999'
55
+ }
56
+ }
57
+ }
58
+ ```
59
+
60
+ ## Supported Rubies
61
+
62
+ Actively tested with the following Ruby versions:
63
+
64
+ - MRI 1.9.3
65
+ - MRI 1.9.2
66
+ - MRI 1.8.7
67
+ - JRuby 1.6.5
68
+
69
+ *NB.* For JRuby, you'll need to install the `jruby-openssl` gem. There's no way to automatically specify this in a Rubygem gemspec, so you need to manually add it your project's own Gemfile:
70
+
71
+ ```ruby
72
+ gem 'jruby-openssl', :platform => :jruby
73
+ ```
74
+
75
+ ## License
76
+
77
+ Copyright (c) 2012 by Jesse Pollak
78
+
79
+ 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:
80
+
81
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
82
+
83
+ 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,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs << 'test'
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1 @@
1
+ require 'omniauth/clef'
@@ -0,0 +1,2 @@
1
+ require 'omniauth/clef/version'
2
+ require 'omniauth/strategies/clef'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Clef
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'omniauth-oauth2'
2
+ require 'pry'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ class Clef < OmniAuth::Strategies::OAuth2
7
+
8
+ option :name, "clef"
9
+ option :client_options, {
10
+ :site => 'https://clef.io/api/v1',
11
+ :request_url => "https://clef.io/iframes/qr",
12
+ :token_url => 'authorize'
13
+ }
14
+ option :provider_ignores_state, true
15
+ option :auth_token_params, {
16
+ param_name: 'access_token',
17
+ mode: :query
18
+ }
19
+
20
+ uid { raw_info['id'] }
21
+
22
+ info do
23
+ prune!({
24
+ 'email' => raw_info['email'],
25
+ 'first_name' => raw_info['first_name'],
26
+ 'last_name' => raw_info['last_name'],
27
+ 'phone_number' => raw_info['phone_number'],
28
+ })
29
+ end
30
+
31
+ extra do
32
+ hash = {}
33
+ hash['raw_info'] = raw_info unless skip_info?
34
+ prune! hash
35
+ end
36
+
37
+ def request_phase
38
+ url = options.client_options.request_url
39
+ url << "?app_id=#{options.client_id}"
40
+ url << "&redirect_url=#{callback_url}"
41
+ redirect url
42
+ end
43
+
44
+ def build_access_token
45
+ verifier = request.params['code']
46
+ client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)), deep_symbolize(options.auth_token_params))
47
+ end
48
+
49
+ def raw_info
50
+ @raw_info ||= access_token.get('info').parsed || {}
51
+ @raw_info = @raw_info['info'] if !@raw_info['info'].nil?
52
+ @raw_info
53
+ end
54
+
55
+ private
56
+
57
+ def prune!(hash)
58
+ hash.delete_if do |_, value|
59
+ prune!(value) if value.is_a?(Hash)
60
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'omniauth/clef/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'omniauth-clef'
7
+ s.version = OmniAuth::Clef::VERSION
8
+ s.authors = ['Jesse Pollak']
9
+ s.email = ['jpollak92@gmail.com']
10
+ s.summary = 'Clef strategy for OmniAuth'
11
+ s.homepage = 'https://github.com/jessepollak/omniauth-clef'
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.1'
19
+ s.add_runtime_dependency 'pry'
20
+
21
+ s.add_development_dependency 'minitest'
22
+ s.add_development_dependency 'mocha'
23
+ s.add_development_dependency 'rake'
24
+ end
@@ -0,0 +1,54 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'mocha/setup'
4
+ require 'omniauth/strategies/clef'
5
+
6
+ OmniAuth.config.test_mode = true
7
+
8
+ module BlockTestHelper
9
+ def test(name, &blk)
10
+ method_name = "test_#{name.gsub(/\s+/, '_')}"
11
+ raise "Method already defined: #{method_name}" if instance_methods.include?(method_name.to_sym)
12
+ define_method method_name, &blk
13
+ end
14
+ end
15
+
16
+ module CustomAssertions
17
+ def assert_has_key(key, hash, msg = nil)
18
+ msg = message(msg) { "Expected #{hash.inspect} to have key #{key.inspect}" }
19
+ assert hash.has_key?(key), msg
20
+ end
21
+
22
+ def refute_has_key(key, hash, msg = nil)
23
+ msg = message(msg) { "Expected #{hash.inspect} not to have key #{key.inspect}" }
24
+ refute hash.has_key?(key), msg
25
+ end
26
+ end
27
+
28
+ class TestCase < MiniTest::Unit::TestCase
29
+ extend BlockTestHelper
30
+ include CustomAssertions
31
+ end
32
+
33
+ class StrategyTestCase < TestCase
34
+ def setup
35
+ @request = stub('Request')
36
+ @request.stubs(:params).returns({})
37
+ @request.stubs(:cookies).returns({})
38
+ @request.stubs(:env).returns({})
39
+
40
+ @client_id = '123'
41
+ @client_secret = '53cr3tz'
42
+ end
43
+
44
+ def strategy
45
+ @strategy ||= begin
46
+ args = [@client_id, @client_secret, @options].compact
47
+ OmniAuth::Strategies::Clef.new(nil, *args).tap do |strategy|
48
+ strategy.stubs(:request).returns(@request)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ Dir[File.expand_path('../support/**/*', __FILE__)].each &method(:require)
@@ -0,0 +1,70 @@
1
+ # NOTE it would be useful if this lived in omniauth-oauth2 eventually
2
+ module OAuth2StrategyTests
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include ClientTests
6
+ include AuthorizeParamsTests
7
+ include CSRFAuthorizeParamsTests
8
+ include TokenParamsTests
9
+ end
10
+ end
11
+
12
+ module ClientTests
13
+ extend BlockTestHelper
14
+
15
+ test 'should be initialized with symbolized client_options' do
16
+ @options = { :client_options => { 'authorize_url' => 'https://example.com' } }
17
+ assert_equal 'https://example.com', strategy.client.options[:authorize_url]
18
+ end
19
+ end
20
+
21
+ module AuthorizeParamsTests
22
+ extend BlockTestHelper
23
+
24
+ test 'should include any authorize params passed in the :authorize_params option' do
25
+ @options = { :authorize_params => { :foo => 'bar', :baz => 'zip' } }
26
+ assert_equal 'bar', strategy.authorize_params['foo']
27
+ assert_equal 'zip', strategy.authorize_params['baz']
28
+ end
29
+
30
+ test 'should include top-level options that are marked as :authorize_options' do
31
+ @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
32
+ assert_equal 'bar', strategy.authorize_params['scope']
33
+ assert_equal 'baz', strategy.authorize_params['foo']
34
+ end
35
+
36
+ test 'should exclude top-level options that are not passed' do
37
+ @options = { :authorize_options => [:bar] }
38
+ refute_has_key :bar, strategy.authorize_params
39
+ refute_has_key 'bar', strategy.authorize_params
40
+ end
41
+ end
42
+
43
+ module CSRFAuthorizeParamsTests
44
+ extend BlockTestHelper
45
+
46
+ test 'should store random state in the session when none is present in authorize or request params' do
47
+ assert_includes strategy.authorize_params.keys, 'state'
48
+ refute_empty strategy.authorize_params['state']
49
+ refute_empty strategy.session['omniauth.state']
50
+ assert_equal strategy.authorize_params['state'], strategy.session['omniauth.state']
51
+ end
52
+
53
+ end
54
+
55
+ module TokenParamsTests
56
+ extend BlockTestHelper
57
+
58
+ test 'should include any authorize params passed in the :token_params option' do
59
+ @options = { :token_params => { :foo => 'bar', :baz => 'zip' } }
60
+ assert_equal 'bar', strategy.token_params['foo']
61
+ assert_equal 'zip', strategy.token_params['baz']
62
+ end
63
+
64
+ test 'should include top-level options that are marked as :token_options' do
65
+ @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
66
+ assert_equal 'bar', strategy.token_params['scope']
67
+ assert_equal 'baz', strategy.token_params['foo']
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,106 @@
1
+ require 'helper'
2
+ require 'omniauth-clef'
3
+
4
+ class StrategyTest < StrategyTestCase
5
+ include OAuth2StrategyTests
6
+ end
7
+
8
+ class ClientTest < StrategyTestCase
9
+ test 'has correct Clef site' do
10
+ assert_equal 'https://clef.io/api/v1', strategy.client.site
11
+ end
12
+
13
+ test 'has correct token url' do
14
+ assert_equal 'authorize', strategy.client.options[:token_url]
15
+ end
16
+ end
17
+
18
+ class UidTest < StrategyTestCase
19
+ def setup
20
+ super
21
+ strategy.stubs(:raw_info).returns({ 'id' => '123' })
22
+ end
23
+
24
+ test 'returns the id from raw_info' do
25
+ assert_equal '123', strategy.uid
26
+ end
27
+ end
28
+
29
+ class InfoTestOptionalDataPresent < StrategyTestCase
30
+ def setup
31
+ super
32
+ @raw_info ||= { 'email' => 'test@example.com' }
33
+ strategy.stubs(:raw_info).returns(@raw_info)
34
+ end
35
+
36
+ test 'returns the email' do
37
+ assert_equal 'test@example.com', strategy.info['email']
38
+ end
39
+
40
+ test 'returns the first name' do
41
+ @raw_info['first_name'] = 'Fred'
42
+ assert_equal 'Fred', strategy.info['first_name']
43
+ end
44
+
45
+ test 'returns the last name' do
46
+ @raw_info['last_name'] = 'Smith'
47
+ assert_equal 'Smith', strategy.info['last_name']
48
+ end
49
+
50
+ test 'returns the phone number' do
51
+ @raw_info['phone_number'] = '1119991111'
52
+ assert_equal '1119991111', strategy.info['phone_number']
53
+ end
54
+ end
55
+
56
+ class InfoTestOptionalDataNotPresent < StrategyTestCase
57
+ def setup
58
+ super
59
+ @raw_info ||= { 'email' => 'test@example.com' }
60
+ strategy.stubs(:raw_info).returns(@raw_info)
61
+ end
62
+
63
+ test 'has no first name key' do
64
+ refute_has_key 'first_name', strategy.info
65
+ end
66
+
67
+ test 'has no last name key' do
68
+ refute_has_key 'last_name', strategy.info
69
+ end
70
+
71
+ test 'has no phone number key' do
72
+ refute_has_key 'phone_number', strategy.info
73
+ end
74
+ end
75
+
76
+ class RawInfoTest < StrategyTestCase
77
+ def setup
78
+ super
79
+ @access_token = stub('OAuth2::AccessToken')
80
+ end
81
+
82
+ test 'performs a GET to https://clef.io/api/v1/info' do
83
+ strategy.stubs(:access_token).returns(@access_token)
84
+ @access_token.expects(:get).with('info').returns(stub_everything('OAuth2::Response'))
85
+ strategy.raw_info
86
+ end
87
+
88
+ test 'returns a Hash' do
89
+ strategy.stubs(:access_token).returns(@access_token)
90
+ raw_response = stub('Faraday::Response')
91
+ raw_response.stubs(:body).returns('{ "ohai": "thar" }')
92
+ raw_response.stubs(:status).returns(200)
93
+ raw_response.stubs(:headers).returns({'Content-Type' => 'application/json' })
94
+ oauth2_response = OAuth2::Response.new(raw_response)
95
+ @access_token.stubs(:get).with('info').returns(oauth2_response)
96
+ assert_kind_of Hash, strategy.raw_info
97
+ assert_equal 'thar', strategy.raw_info['ohai']
98
+ end
99
+
100
+ test 'returns an empty hash when the response is false' do
101
+ strategy.stubs(:access_token).returns(@access_token)
102
+ oauth2_response = stub('OAuth2::Response', :parsed => false)
103
+ @access_token.stubs(:get).with('info').returns(oauth2_response)
104
+ assert_kind_of Hash, strategy.raw_info
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-clef
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jesse Pollak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - jpollak92@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - lib/omniauth-clef.rb
96
+ - lib/omniauth/clef.rb
97
+ - lib/omniauth/clef/version.rb
98
+ - lib/omniauth/strategies/clef.rb
99
+ - omniauth-clef.gemspec
100
+ - test/helper.rb
101
+ - test/support/shared_examples.rb
102
+ - test/test.rb
103
+ homepage: https://github.com/jessepollak/omniauth-clef
104
+ licenses: []
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.0
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Clef strategy for OmniAuth
126
+ test_files:
127
+ - test/helper.rb
128
+ - test/support/shared_examples.rb
129
+ - test/test.rb