omniauth-nationbuilder 0.0.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: a09359c904523452ba46358cf476a4e0c39e28fd
4
+ data.tar.gz: 60aef520596990d65851598a7991203fe0028bb1
5
+ SHA512:
6
+ metadata.gz: 2bc8b79a24ba40dc23920418521826c1041ae036783a0e6231bf7273ee59a89b4bc460a1f607b59bd2c2d2ad35cb31d1340519eaac64cc06b191f6090af40327
7
+ data.tar.gz: 79b3b3de168b33ead7c6a5fdb543b2e90b2fb0ab3985c49b2a70e0dca71f405084b9fa0250aa671643ff6b5a89416b3abfb165d0feb6ee4167ecf8167c773442
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ before_install:
2
+ - gem update --system 2.1.11
3
+ language: ruby
4
+ rvm:
5
+ - "2.0.0"
6
+ - "2.1.0"
7
+ - "2.3.0"
8
+ - "jruby"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-nationbuilder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Chris Jensen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ [![Build Status](https://travis-ci.org/chrisjensen/omniauth-nationbuilder.svg?branch=master)](https://travis-ci.org/chrisjensen/omniauth-nationbuilder)
2
+
3
+ # Omniauth::Nationbuilder
4
+
5
+ Strategy to authenticate with Nationbuilder in OmniAuth.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'omniauth-nationbuilder'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install omniauth-nationbuilder
20
+
21
+ ## Usage
22
+
23
+ Here's an example for adding the middleware to a Rails app in config/initializers/omniauth.rb:
24
+
25
+ ````ruby
26
+ Rails.application.config.middleware.use OmniAuth::Builder do
27
+ provider :nationbuilder, ENV["NATIONBUILDER_CLIENT_ID"], ENV["NATIONBUILDER_CLIENT_SECRET"]
28
+ end
29
+ ````
30
+
31
+ Because every nation has it's own slug, and this may be configured at run time if you are supporting authorisation of multiple nations, this is passed by a url parameter.
32
+
33
+ To authenticate your nation, use
34
+
35
+ /auth/nationbuilder?nation_slug=<YOUR NATIONS SLUG>
36
+
37
+
38
+ ## Auth Hash
39
+
40
+ Here's an example of an authentication hash available in the callback by accessing request.env["omniauth.auth"]:
41
+
42
+ ````ruby
43
+ {
44
+ :provider => "nationbuilder",
45
+ :uid => "YOUR NATIONS SLUG",
46
+ :credentials => {
47
+ :token => "f4d3b3c9528672fa15181dfd9e63a793f4e754186bbeaad7030b053e1398b84f",
48
+ :expires => false
49
+ },
50
+ :extra => {
51
+ :token_type => "bearer",
52
+ :created_at => 1465867529,
53
+ :access_token => "f4d3b3c9528672fa15181dfd9e63a793f4e754186bbeaad7030b053e1398b84f",
54
+ :refresh_token => nil,
55
+ :expires_at => nil
56
+ }
57
+ }
58
+ ````
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require File.join('bundler', 'gem_tasks')
3
+ require File.join('rspec', 'core', 'rake_task')
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Nationbuilder
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require File.join('omniauth', 'strategies', 'nationbuilder')
@@ -0,0 +1,63 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Nationbuilder < OmniAuth::Strategies::OAuth2
6
+ # Give your strategy a name.
7
+ option :name, "nationbuilder"
8
+
9
+ # We need the nation slug from the user and will use that as the unique id
10
+ # for this strategy
11
+ option :nation_slug, nil
12
+ option :slug_param, 'nation_slug'
13
+
14
+ # Build a form to get the nations slug if one has not been supplied
15
+ def get_slug
16
+ f = OmniAuth::Form.new(:title => 'NationBuilder Authentication')
17
+ f.label_field("Your Nation's Slug", options.slug_param)
18
+ f.input_field('url', options.slug_param)
19
+ f.button "Connect to your Nation"
20
+ f.to_response
21
+ end
22
+
23
+ # Configure site before super initialises the OAuth2 Client
24
+ def client
25
+ options.client_options[:site] = 'https://' + options.client_options[:slug] + '.nationbuilder.com'
26
+ super
27
+ end
28
+
29
+ # Returns the slug, nil if one has not been specified anywhere
30
+ def slug
31
+ s = options.nation_slug || request.params[options.slug_param.to_s]
32
+ s = nil if s == ''
33
+ s
34
+ end
35
+
36
+ # Override the normal OAuth request_phase to get a slug from the user if
37
+ # one hasn't been supplied, and insert the slug into the site url
38
+ def request_phase
39
+ if slug
40
+ # Store slug on the session
41
+ session["omniauth.nationbuilder.slug"] = options.client_options[:slug] = slug
42
+ super
43
+ else
44
+ get_slug
45
+ end
46
+ end
47
+
48
+ def callback_phase
49
+ options.client_options[:slug] = session.delete("omniauth.nationbuilder.slug")
50
+ super
51
+ end
52
+
53
+ # These are called after authentication has succeeded. If
54
+ # possible, you should try to set the UID without making
55
+ # additional calls (if the user id is returned with the token
56
+ # or as a URI parameter). This may not be possible with all
57
+ # providers.
58
+ uid { options.client_options[:slug] }
59
+
60
+ extra { access_token }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Nationbuilder
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require File.join('omniauth', 'nationbuilder')
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth-nationbuilder/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "omniauth-nationbuilder"
8
+ gem.version = Omniauth::Nationbuilder::VERSION
9
+ gem.authors = ["Chris Jensen"]
10
+ gem.email = ["chris@broadthought.co"]
11
+ gem.description = %q{Omniauth strategy for NationBuilder using OAuth2}
12
+ gem.summary = %q{Omniauth strategy for NationBuilder using OAuth2}
13
+ gem.homepage = "https://github.com/chrisjensen/omniauth-nationbuilder"
14
+ gem.licenses = ['MIT']
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_runtime_dependency 'omniauth', '~> 1.1', '>= 1.1.1'
22
+ gem.add_runtime_dependency 'omniauth-oauth2', '~> 1.3', '>= 1.3.1'
23
+
24
+ gem.add_development_dependency 'rspec', '~> 2.14'
25
+ gem.add_development_dependency 'rake'
26
+
27
+ gem.add_development_dependency 'rack-test', '~> 0.5'
28
+ gem.add_development_dependency 'webmock', '~> 1.7'
29
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-nationbuilder'
3
+
4
+ describe OmniAuth::Strategies::Nationbuilder, :type => :strategy do
5
+ def app
6
+ strategy = OmniAuth::Strategies::Nationbuilder
7
+
8
+ Rack::Builder.new {
9
+ use Rack::Session::Cookie, :secret => 'suppress no secret warning'
10
+ use strategy
11
+ run lambda {|env| [404, {'Content-Type' => 'text/plain'}, [nil || env.key?('omniauth.auth').to_s]] }
12
+ }.to_app
13
+ end
14
+
15
+ subject do
16
+ OmniAuth::Strategies::Nationbuilder.new(app, 'appid', 'secret', @options || {})
17
+ end
18
+
19
+ before do
20
+ OmniAuth.config.failure_raise_out_environments = ['development','test','']
21
+ end
22
+
23
+ describe '/auth/nationbuilder' do
24
+ context 'with nation_slug unset' do
25
+ before do
26
+ get '/auth/nationbuilder'
27
+ end
28
+
29
+ it 'should respond with OK' do
30
+ expect(last_response).to be_ok
31
+ end
32
+
33
+ it 'should respond with HTML' do
34
+ expect(last_response.content_type).to eq('text/html')
35
+ end
36
+
37
+ it 'should prompt for nation slug' do
38
+ expect(last_response.body).to match %r{<input[^>]*nation_slug}
39
+ end
40
+ end
41
+
42
+ context 'with nation_slug in get param' do
43
+ let(:slug) { 'mynation' }
44
+ let(:redirect_url) { 'https://' + slug + '.nationbuilder.com' }
45
+
46
+ it 'should redirect to the NationBuilder oauth url' do
47
+ get '/auth/nationbuilder?nation_slug=' + slug
48
+ expect(last_response).to be_redirect
49
+ expect(last_response.headers['Location']).to match(%r{^#{redirect_url}.*})
50
+ end
51
+ end
52
+ end
53
+
54
+ ### NOTE ###
55
+ # This section involves heavy stubbing of Omniauth and OAuth methods to verify
56
+ # a small amount of code.
57
+ # There is probably a better way to test this, but would require better knowledge of
58
+ # rack middleware than I can bring
59
+ describe 'session' do
60
+ before do
61
+ OmniAuth.config.test_mode = true
62
+ end
63
+
64
+ after do
65
+ OmniAuth.config.test_mode = false
66
+ end
67
+
68
+ let(:rack_cookies) do
69
+ {}
70
+ end
71
+
72
+ let(:nation_slug) { 'nationofkinggeorge' }
73
+
74
+ before do
75
+ allow(subject).to receive(:request) { double('Rack::Test::Request', {:params => {'nation_slug' => nation_slug }, :query_string => 'query_string', :env => { 'rack.session' => rack_cookies }}) }
76
+
77
+ allow(subject).to receive(:full_host) { 'example.org' }
78
+ allow(subject).to receive(:script_name) { 'test' }
79
+ allow(subject).to receive(:build_access_token) { {} }
80
+ allow(subject).to receive(:env) { {} }
81
+ allow(subject).to receive(:call_app!) { {} }
82
+
83
+ allow(subject).to receive(:session) { rack_cookies }
84
+ end
85
+
86
+ describe 'request_phase' do
87
+
88
+ it 'should save the slug on the session' do
89
+ subject.request_phase
90
+ expect(subject.session['omniauth.nationbuilder.slug']).to eq(nation_slug)
91
+ end
92
+ end
93
+
94
+ describe '/auth/nationbuilder/callback' do
95
+ let(:client) do
96
+ OAuth2::Client.new('abc', 'def') do |builder|
97
+ builder.request :url_encoded
98
+ end
99
+ end
100
+
101
+ let(:nb_response) do
102
+ {
103
+ "access_token" => "token-string",
104
+ "token_type" => "bearer",
105
+ "created_at" => 1465867529
106
+ }
107
+ end
108
+
109
+ let(:access_token) do
110
+ OAuth2::AccessToken.from_hash(client, nb_response )
111
+ end
112
+
113
+ before do
114
+ allow(subject).to receive(:access_token).and_return(access_token)
115
+ subject.options.provider_ignores_state = true;
116
+ end
117
+
118
+ it "should provide nationbuilder auth hash in extra" do
119
+ expect(subject.auth_hash.extra).to include(nb_response)
120
+ end
121
+
122
+ it "should set uid from session" do
123
+ subject.session['omniauth.nationbuilder.slug'] = nation_slug
124
+ subject.callback_phase
125
+ expect(subject.auth_hash).to include("uid" => nation_slug)
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'rspec'
4
+ require 'rack/test'
5
+ require 'webmock/rspec'
6
+
7
+ RSpec.configure do |config|
8
+ config.include WebMock::API
9
+ config.include Rack::Test::Methods
10
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-nationbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Jensen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: omniauth-oauth2
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.1
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '1.3'
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.3.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '2.14'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: '2.14'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rack-test
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '0.5'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: '0.5'
95
+ - !ruby/object:Gem::Dependency
96
+ name: webmock
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '1.7'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ version: '1.7'
109
+ description: Omniauth strategy for NationBuilder using OAuth2
110
+ email:
111
+ - chris@broadthought.co
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - .gitignore
117
+ - .travis.yml
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/omniauth-nationbuilder.rb
123
+ - lib/omniauth-nationbuilder/version.rb
124
+ - lib/omniauth/nationbuilder.rb
125
+ - lib/omniauth/nationbuilder/version.rb
126
+ - lib/omniauth/strategies/nationbuilder.rb
127
+ - omniauth-nationbuilder.gemspec
128
+ - spec/omniauth/strategies/nationbuilder_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: https://github.com/chrisjensen/omniauth-nationbuilder
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Omniauth strategy for NationBuilder using OAuth2
154
+ test_files:
155
+ - spec/omniauth/strategies/nationbuilder_spec.rb
156
+ - spec/spec_helper.rb