omniauth-mydigipass 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-mydigipass.gemspec
4
+ gemspec
5
+
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # OmniAuth Mydigipass.com
2
+
3
+ This is an OmniAuth strategy for authenticating with MYDIGIPASS.COM.
4
+
5
+ If you want to integrate your website with MYDIGIPASS.COM, you will need to
6
+ sign up on http://developer.mydigipass.com and connect your site there.
7
+ There you will get a `client_id` and `client_secret` you need to fill in here.
8
+
9
+
10
+ ## Basic Usage
11
+
12
+ If you are testing your application in the sandbox environment, write
13
+
14
+ use OmniAuth::Builder do
15
+ provider :mydigipass, ENV['MYDIGIPASS_CLIENT_ID'], ENV['MYDIGIPASS_CLIENT_SECRET'],
16
+ :client_options => OmniAuth::Strategies::Mydigipass.default_client_urls(:sandbox => true)
17
+ end
18
+
19
+ Once your application goes in production, you can just write:
20
+
21
+ use OmniAuth::Builder do
22
+ provider :mydigipass, ENV['MYDIGIPASS_CLIENT_ID'], ENV['MYDIGIPASS_CLIENT_SECRET']
23
+ end
24
+
25
+ ## Example Application
26
+
27
+ I have added a small working example application, check it out how it should work. To integrate into rails you should
28
+
29
+ * add the
30
+
31
+ ## Example Integrating with Rails
32
+
33
+ Inside your `config/application.rb` add the following (e.g. at the bottom, inside the configuration block) :
34
+
35
+ # enable omniauth strategies
36
+ Rails.application.config.middleware.use OmniAuth::Builder do
37
+ provider :mydigipass, APP_CONFIG[:client_id], APP_CONFIG[:client_secret]
38
+ end
39
+
40
+ And then you just have to make sure you have something listening at `/auth/:provider/callback`.
41
+ Suppose you add the following routes:
42
+
43
+ match '/auth/:provider/callback', :to => 'home#auth_create'
44
+ match '/auth/failure', :to => 'home#auth_failure'
45
+
46
+ Then, inside your `HomeController` you could write:
47
+
48
+ def auth_failure
49
+ set_flash_message(:notice, "OAuth error: #{params[:message]}")
50
+ redirect_to root_path
51
+ end
52
+
53
+ def auth_create
54
+ user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'].with_indifferent_access)
55
+ logger.debug "Found or created user: #{user.email} [#{user.id}]"
56
+ if user.sign_in_count == 0
57
+ set_flash_message(:notice, "Welcome #{user.email}, thank you for signing up using your dP+ account!")
58
+ else
59
+ set_flash_message(:notice, "Succesfully logged in!")
60
+ end
61
+ sign_in(:user, user, :bypass => true)
62
+ redirect_to dashboard_path
63
+ end
64
+
65
+ When a user signs in through MYDIGIPASS.COM, it could be a new user (signing up), or an existing user.
66
+ The function `find_or_create_from_auth_hash` handles that for me:
67
+
68
+ def self.from_auth_hash(auth_hash)
69
+ logger.debug "User.from_auth_hash: auth_hash = #{auth_hash.inspect} "
70
+ received_uuid = auth_hash[:extra][:raw_info][:uuid]
71
+ received_email = auth_hash[:extra][:raw_info][:email]
72
+
73
+ user = User.find_by_uuid(received_uuid) || User.find_by_email(received_email)
74
+ user = user.nil? ? create_from_auth_hash(received_uuid, received_email) : prevent_login_with_normal_password(user, received_uuid)
75
+ end
76
+
77
+ I try to find the user, by `uuid` or `email`. If I find the user by `uuid`, she has logged on before with MYDIGIPASS.COM
78
+ If I find a matching mail, link the uuid to that user. If I do not find a user, create one with the given `email` and `uuid`.
79
+ I also made sure that users can then only login with their MYDIGIPASS.COM and no longer normally, but that is optional of course.
80
+
81
+ ## License
82
+
83
+ Copyright (c) 2012 Nathan Van der Auwera
84
+
85
+ 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:
86
+
87
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
88
+
89
+ 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,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run specs"
6
+ RSpec::Core::RakeTask.new
7
+
8
+ desc 'Default: Run specs'
9
+ task :default => :spec
data/example/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rack', '~> 1.3.6'
4
+
5
+ gem 'sinatra'
6
+ gem 'omniauth-mydigipass', :path => '../'
data/example/config.ru ADDED
@@ -0,0 +1,34 @@
1
+ require 'bundler/setup'
2
+ require 'sinatra'
3
+ require 'omniauth'
4
+ require 'omniauth-mydigipass'
5
+
6
+
7
+ class App < Sinatra::Base
8
+ get '/' do
9
+ content_type 'text/html'
10
+ <<-HTML
11
+ <h1>Test OAuth2 with MYDIGIPASS.COM</h1>
12
+ <script type="text/javascript" src="https://sandbox.mydigipass.com/dp_connect.js"></script>
13
+ <a class="dpplus-connect" data-client-id="2z4z3zn6ezuov82e4dfu73q3z" data-redirect-uri="http://localhost:3002/auth/mydigipass/callback" href="#">connect with mydigipass.com</a>
14
+ HTML
15
+ end
16
+
17
+ get '/auth/:name/callback' do
18
+ @auth = request.env['omniauth.auth']
19
+ erb :callback
20
+ end
21
+
22
+ get '/auth/failure' do
23
+ @request = request
24
+ erb :failure
25
+ end
26
+ end
27
+
28
+ use Rack::Session::Cookie
29
+ use OmniAuth::Builder do
30
+ provider :mydigipass, '2z4z3zn6ezuov82e4dfu73q3z', '1mcskxim7nomrafvfg7s36pjv',
31
+ :client_options => OmniAuth::Strategies::Mydigipass.default_client_urls(:sandbox => true)
32
+ end
33
+
34
+ run App.new
@@ -0,0 +1,14 @@
1
+ <html>
2
+ <body>
3
+ <h1>Authentication Successfull via <%= @auth.provider %></h1>
4
+ <h2>info:</h2>
5
+ <ul>
6
+ <%- @auth.info.each do |key, value| %>
7
+ <li><strong><%= key %>:</strong> <%= value.inspect %></li>
8
+ <% end %>
9
+ </ul>
10
+ <a href='/'>Sign out</a>
11
+ <h2>raw auth:</h2>
12
+ <pre style="white-space: pre-wrap"><%= Rack::Utils.escape_html @auth.inspect %></pre>
13
+ </body>
14
+ </html>
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <h1>There was an error:</h1>
4
+ <pre style="white-space: pre-wrap"><%= Rack::Utils.escape_html @request.inspect %></pre>
5
+ </body>
6
+ </html>
@@ -0,0 +1,2 @@
1
+ require "omniauth-mydigipass/version"
2
+ require 'omniauth/strategies/mydigipass'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Mydigipass
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Mydigipass < OmniAuth::Strategies::OAuth2
6
+
7
+ def self.default_client_urls(options = {})
8
+ local_base_uri = options[:sandbox] ? 'https://sandbox.mydigipass.com' : 'https://mydigipass.com'
9
+ {
10
+ :site => local_base_uri,
11
+ :authorize_url => local_base_uri + '/oauth/authenticate',
12
+ :token_url => local_base_uri + '/oauth/token'
13
+ }
14
+ end
15
+
16
+
17
+ # Give your strategy a name.
18
+ option :name, "mydigipass"
19
+
20
+ # for the sandbox environment, use http://sandbox.mydigipass.com
21
+ option :base_uri, "https://mydigipass.com"
22
+
23
+ #option :client_options, {
24
+ # :site => base_uri,
25
+ # :authorize_url => base_uri + '/oauth/authenticate',
26
+ # :token_url => base_uri + '/oauth/token'
27
+ # }
28
+
29
+ option :client_options, default_client_urls
30
+
31
+
32
+ # These are called after authentication has succeeded.
33
+ uid { raw_info['uuid'] }
34
+
35
+ info do
36
+ {
37
+ :name => "#{raw_info['first_name']} #{raw_info['last_name']}",
38
+ :email => raw_info['email'],
39
+ :nickname => raw_info['login'],
40
+ :first_name => raw_info['first_name'],
41
+ :last_name => raw_info['last_name'],
42
+ :location => "#{raw_info['address_1']}, #{raw_info['zip']} #{raw_info['city']}, #{raw_info['country']}",
43
+ }
44
+ end
45
+
46
+ extra do
47
+ {'raw_info' => raw_info}
48
+ end
49
+
50
+ def raw_info
51
+ @raw_info ||= access_token.get('/oauth/user_data').parsed
52
+ end
53
+
54
+ def base_uri
55
+ default_options[:base_uri]
56
+ end
57
+
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-mydigipass/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nathan Van der Auwera"]
6
+ gem.email = ["nathan@dixis.com"]
7
+ gem.summary = %Q{OmniAuth strategy for MYDIGIPASS.COM}
8
+ gem.description = %Q{OmniAuth strategy for MYDIGIPASS.COM, which can be used for sandbox or production}
9
+ gem.homepage = "https://github.com/nathanvda/omniauth-mydigipass"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "omniauth-mydigipass"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = OmniAuth::Mydigipass::VERSION
17
+
18
+ gem.add_dependency 'omniauth', '~> 1.0'
19
+ gem.add_dependency 'omniauth-oauth2', '~> 1.0'
20
+ gem.add_development_dependency 'rspec', '~> 2.7'
21
+ gem.add_development_dependency 'rack-test'
22
+ gem.add_development_dependency 'simplecov'
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-mydigipass'
3
+
4
+ describe OmniAuth::Strategies::Mydigipass do
5
+ subject do
6
+ OmniAuth::Strategies::Mydigipass.new(nil, @options || {})
7
+ end
8
+
9
+ it_should_behave_like 'an oauth2 strategy'
10
+
11
+ describe '#client' do
12
+ it 'should have the correct mydigipass.com site' do
13
+ subject.client.site.should eq("https://mydigipass.com")
14
+ end
15
+
16
+ it 'should have the correct authorization url' do
17
+ subject.client.options[:authorize_url].should eq("https://mydigipass.com/oauth/authenticate")
18
+ end
19
+
20
+ it 'should have the correct token url' do
21
+ subject.client.options[:token_url].should eq('https://mydigipass.com/oauth/token')
22
+ end
23
+ end
24
+
25
+ describe '#callback_path' do
26
+ it 'should have the correct callback path' do
27
+ subject.callback_path.should eq('/auth/mydigipass/callback')
28
+ end
29
+ end
30
+
31
+ context "when connecting to the sandbox" do
32
+ it 'should have the correct mydigipass.com site' do
33
+ @options = { :client_options => OmniAuth::Strategies::Mydigipass.default_client_urls(:sandbox => true) }
34
+ subject.client.site.should eq("https://sandbox.mydigipass.com")
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'omniauth'
8
+ require 'omniauth-mydigipass'
9
+
10
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
11
+
12
+ RSpec.configure do |config|
13
+ config.include Rack::Test::Methods
14
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
+ end
16
+
@@ -0,0 +1,39 @@
1
+ # NOTE it would be useful if this lived in omniauth-oauth2 eventually
2
+ # Thanks to Josh Ellithorpe for this file -Will
3
+
4
+ shared_examples 'an oauth2 strategy' do
5
+ describe '#client' do
6
+ it 'should be initialized with symbolized client_options' do
7
+ @options = { :client_options => { 'authorize_url' => 'https://example.com' } }
8
+ subject.client.options[:authorize_url].should == 'https://example.com'
9
+ end
10
+ end
11
+
12
+ describe '#authorize_params' do
13
+ it 'should include any authorize params passed in the :authorize_params option' do
14
+ @options = { :authorize_params => { :foo => 'bar', :baz => 'zip' } }
15
+ subject.authorize_params['foo'].should eq('bar')
16
+ subject.authorize_params['baz'].should eq('zip')
17
+ end
18
+
19
+ it 'should include top-level options that are marked as :authorize_options' do
20
+ @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
21
+ subject.authorize_params['scope'].should eq('bar')
22
+ subject.authorize_params['foo'].should eq('baz')
23
+ end
24
+ end
25
+
26
+ describe '#token_params' do
27
+ it 'should include any token params passed in the :token_params option' do
28
+ @options = { :token_params => { :foo => 'bar', :baz => 'zip' } }
29
+ subject.token_params['foo'].should eq('bar')
30
+ subject.token_params['baz'].should eq('zip')
31
+ end
32
+
33
+ it 'should include top-level options that are marked as :token_options' do
34
+ @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
35
+ subject.token_params['scope'].should eq('bar')
36
+ subject.token_params['foo'].should eq('baz')
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-mydigipass
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathan Van der Auwera
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
16
+ requirement: &15842580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *15842580
25
+ - !ruby/object:Gem::Dependency
26
+ name: omniauth-oauth2
27
+ requirement: &15842000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *15842000
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &15841420 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.7'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *15841420
47
+ - !ruby/object:Gem::Dependency
48
+ name: rack-test
49
+ requirement: &15840960 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *15840960
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &15840420 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *15840420
69
+ description: OmniAuth strategy for MYDIGIPASS.COM, which can be used for sandbox or
70
+ production
71
+ email:
72
+ - nathan@dixis.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - example/Gemfile
83
+ - example/config.ru
84
+ - example/views/callback.erb
85
+ - example/views/failure.erb
86
+ - lib/omniauth-mydigipass.rb
87
+ - lib/omniauth-mydigipass/version.rb
88
+ - lib/omniauth/strategies/mydigipass.rb
89
+ - omniauth-mydigipass.gemspec
90
+ - spec/omniauth/strategies/mydigipass_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/support/shared_examples.rb
93
+ homepage: https://github.com/nathanvda/omniauth-mydigipass
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.15
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: OmniAuth strategy for MYDIGIPASS.COM
117
+ test_files: []