omniauth-idq 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a7320a2a3724a31a365664d927d3e9b16f301abd0383177930a71ff766457422
4
+ data.tar.gz: 5ca0d27d47cee961c3fcdc4e494f8d410d2ec7b4f22e746d673189a7a5d8a137
5
+ SHA512:
6
+ metadata.gz: 193ca66b03b0bad55a4f4e7e13bb7107c58f9d8134d5581fa2e8e8830d21a48a52f721c206423251d322e9805bc954d53e36544320c97c0581f8c21c54e941d8
7
+ data.tar.gz: 6aaea3e2c768e27c56172ac0cd71c53e9bb698f61317cd79c4173bccbc9149e72e1260f7ee5594919a021328e6cc9486a26640b7727b8517df0c81e1a8ba8173
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rake'
4
+ # Specify your gem's dependencies in omniauth-idq.gemspec
5
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016-2017 inBay Technologies Inc.
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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/README.md ADDED
@@ -0,0 +1,146 @@
1
+ OmniAuth IdQ Strategy
2
+ ==============
3
+
4
+ This gem contains the IdQ strategy for OmniAuth.
5
+
6
+ For more information about the idQ API: http://idquanta.com/
7
+
8
+ Usage
9
+ -------------
10
+
11
+ If you are using rails, you need to add the gem to your `Gemfile`:
12
+
13
+ gem 'omniauth-idq'
14
+
15
+ You can pull in the gem directly from GitHub e.g.:
16
+
17
+ gem "omniauth-idq", :git => "git://github.com/inbaytech/omniauth-idq.git"
18
+
19
+ The gem can be configured with your application specific OAuth2 settings via the `config/initializers/omniauth.rb` initializer as follows:
20
+
21
+ ```ruby
22
+ OmniAuth.config.logger = Rails.logger
23
+
24
+ Rails.application.config.middleware.use OmniAuth::Builder do
25
+ # Configure idQ OAuth2 Provider for OmniAuth
26
+ provider :idq, 'client_id', 'client_secret', client_options: {}
27
+ end
28
+ ```
29
+
30
+ Note: The file `config/initializers/omniauth.rb` may not exit on your system, please create it.
31
+
32
+ You can obtain your OAuth2 application credentials consisting of `client_id` and `client_secret`, from your account management console at https://idquanta.com.
33
+
34
+
35
+ ## Authentication
36
+ After the gem is installed and configured, user authentication follows the standard OmniAuth procedure.
37
+ For the idQ provider, the authentication path is `/auth/idq`.
38
+
39
+ #### Example
40
+ If you are running a local Rails development server, your authentication URL will likely look like this:
41
+ http://localhost:3000/auth/idq
42
+
43
+ #### Further Reading
44
+ More information on OmniAuth can be found at https://github.com/intridea/omniauth
45
+
46
+
47
+ Delegated Authorization
48
+ -----------------------
49
+ This module also supports idQ Delegated Authorization.
50
+
51
+ 1. In the controller handling the business logic that requires Delegated Authorization, you first need to make a REST call to the idQ Trust as a Service backend to register your Delegated Authorization request.
52
+
53
+ 2. Retrieve the `push_token` from the REST call response JSON.
54
+
55
+ 3. Redirect the user into a standard OmniAuth flow, together with the `push_token` to trigger the Delegated Authorization logic. You can add additional parameters to the request if you need to remember application state, such as the action or context the Delegated Authorization was triggered in. These Parameters are stored in the OmniAuth `auth_params[]` hash and are available to your callback handler.
56
+
57
+ #### Example
58
+ The following is an example of an imagined `ItemController` that handles a `GET` request to a specific `item` resource by `id`. Before rendering the view associated with the requested `item`, we first kick off a Delegated Authorization request and wait for authorization approval.
59
+
60
+ ```ruby
61
+ require 'rest-client'
62
+ class ItemController < ApplicationController
63
+ # GET /item/:id
64
+ def show
65
+ # Find item in Database
66
+ @item = Item.find(item_params[:id])
67
+
68
+ # Payload to register Delegated Authorization Request
69
+ payload = {
70
+ title: "Authorize Access",
71
+ message: "#{@current_user.name} is attempting to access #{@item.name}. Please approve or deny this access request.",
72
+ target: @idQ_ID,
73
+ client_id: ENV['IDQ_CLIENT_ID'],
74
+ client_secret: ENV['IDQ_CLIENT_SECRET'],
75
+ push_id: SecureRandom.uuid,
76
+ }
77
+
78
+ # URL to send REST request to
79
+ url = ENV['IDQ_BASE_URL'] + ENV['IDQ_DA_REGISTER_PATH']
80
+
81
+ # Send REST request to idQ Trust as a Service Backend
82
+ begin
83
+ response = RestClient.post url, payload, {content_type: "application/x-www-form-urlencoded"}
84
+
85
+ # Retrieve response as JSON
86
+ rjs = JSON.parse(response)
87
+
88
+ # Obtain push token from response JSON
89
+ pt = rjs['push_token']
90
+
91
+ # Redirect the user into a standard OAuth2 flow
92
+ redirect_to "/auth/idq?push_token=#{pt}&action=item_show&item_id=#{@item.id}" and return
93
+ rescue
94
+ flash[:danger] = "Error sending delegated authorization request!"
95
+ render 'show' and return
96
+ end
97
+ end
98
+ end
99
+ ```
100
+
101
+ The following is an implementation of an imagined callback handler that processes the answer to the Delegated Authorization request. Since the callbacks for both Authentication and Authorization are the same under OmniAuth, the first step is to determine what type of callback is being handled (authentication or authorization).
102
+
103
+ ``` ruby
104
+ class CallbackController < ApplicationController
105
+
106
+ # General callback URL handler. Either handle Authentication, or Delegated Authorization.
107
+ def handle_callback
108
+ if is_authorization_response?
109
+ handle_authorization
110
+ else
111
+ handle_authentication
112
+ end
113
+ end
114
+
115
+ # Handler for Delegated Authorization Responses
116
+ def handle_authorization
117
+ # ItemController::show
118
+ if auth_params['action'] == 'item_show'
119
+ item_id = auth_params['item_id']
120
+ @item = Item.find(item_id)
121
+ if is_approved?(auth_hash)
122
+ render 'show_item' and return
123
+ else
124
+ render 'access_denied' and return
125
+ end
126
+ end
127
+ end
128
+
129
+ protected
130
+
131
+ # Helpers
132
+ def is_approved?(auth_hash)
133
+ response_code = auth_hash['extra']['raw_info']['response_code']
134
+ return response_code == '1'
135
+ end
136
+
137
+ def is_authorization_response?
138
+ if auth_hash['extra']['raw_info']['response_code']
139
+ true
140
+ else
141
+ false
142
+ end
143
+ end
144
+
145
+ end
146
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Default: run specs.'
8
+ task :default => :spec
@@ -0,0 +1,112 @@
1
+ require 'omniauth-oauth2'
2
+ require 'multi_json'
3
+
4
+ # Delegated Authorization Strategy for the OAuth2 client
5
+ module OAuth2
6
+ module Strategy
7
+ class DelegatedAuthorization < Base
8
+ # The required query parameters for the authorize URL
9
+ # is an exact copy of the same method in the AuthCode Strategy
10
+ # @param [Hash] params additional query parameters
11
+ def authorize_params(params = {})
12
+ params.merge('response_type' => 'code', 'client_id' => @client.id)
13
+ end
14
+
15
+ # The authorization URL endpoint of the provider
16
+ # Similar to the AuthCode Strategy, but in this case call
17
+ # Client::delegated_authorization_url() to construct the authorization URL
18
+ # @param [Hash] params additional query parameters for the URL
19
+ def authorize_url(params = {})
20
+ @client.delegated_authorization_url(authorize_params.merge(params))
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ # Extend the OAuth2 Client with support for our Delegated Authorization Strategy
27
+ module OAuth2
28
+ class Client
29
+ # The authorize endpoint URL of the OAuth2 provider
30
+ # we add support for generating a Delegated Authorization URL
31
+ # @param [Hash] params additional query parameters
32
+ def delegated_authorization_url(params = {})
33
+ params = (params || {}).merge(redirection_params)
34
+ connection.build_url(options[:delegated_authorization_url], params).to_s
35
+ end
36
+
37
+ # Creates a Delegated Authorization OAuth2 Strategy for the OAuth2 client
38
+ def delegated_authorization
39
+ @delegated_authorization ||= OAuth2::Strategy::DelegatedAuthorization.new(self)
40
+ end
41
+ end
42
+ end
43
+
44
+ # OmniAuth::Strategy for idQ extends the standard OmniAuth::OAuth2 Strategy
45
+ module OmniAuth
46
+ module Strategies
47
+ class Idq < OmniAuth::Strategies::OAuth2
48
+ alias_method :request_phase_original, :request_phase
49
+ # Provider Name
50
+ option :name, 'idq'
51
+
52
+ # Defaults can be overwritten upon initialization of the provider
53
+ # in src/config/initializers/omniauth.rb
54
+ option :client_options, {
55
+ :site => "https://taas.idquanta.com",
56
+ :token_url => "/idqoauth/api/v1/token",
57
+ :authorize_url => "/idqoauth/api/v1/auth",
58
+ :delegated_authorization_url => '/idqoauth/api/v1/pauth'
59
+ }
60
+
61
+ uid {
62
+ raw_info['username']
63
+ }
64
+
65
+ info do
66
+ {
67
+ email: raw_info['email']
68
+ }
69
+ end
70
+
71
+ extra do
72
+ {
73
+ raw_info: raw_info
74
+ }
75
+ end
76
+
77
+ # Patched request_phase
78
+ def request_phase
79
+ # If a push_token was passed in, we want to carry out a delegated authorization
80
+ if request.params['push_token']
81
+ redirect client.delegated_authorization.authorize_url({:redirect_uri => callback_url}.merge(authorize_params))
82
+ else
83
+ # Otherwise proceed with the original omniauth-oauth2 request_phase
84
+ request_phase_original
85
+ end
86
+ end
87
+
88
+ # Allow push_token param through
89
+ def authorize_params
90
+ super.merge(push_token: request.params['push_token'], response_to: 'delegated_authorization')
91
+ end
92
+
93
+ # Need to patch the original callback_url method
94
+ # the original puts all params from query_string onto the callback url
95
+ # idQ does not support this behaviour. However, we need the Omniauth
96
+ # feature to store k-v pairs from query string in the request.env['omniauth.params']
97
+ # for handling delegated authorization requests in a stateless fashion.
98
+ def callback_url
99
+ full_host + script_name + callback_path
100
+ end
101
+
102
+ private
103
+
104
+ # Our custom raw_info method which will make idQ user attributes accessible to Omniauth
105
+ def raw_info
106
+ @raw_info ||= MultiJson.decode(access_token.get('/idqoauth/api/v1/user').body)
107
+ rescue ::Errno::ETIMEDOUT
108
+ raise ::Timeout::Error
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Idq
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "omniauth-idq/version"
2
+ require 'omniauth/strategies/idq'
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "omniauth-idq/version"
4
+
5
+ # p OmniAuth::Idq::VERSION
6
+ Gem::Specification.new do |s|
7
+ s.name = "omniauth-idq"
8
+ s.version = OmniAuth::Idq::VERSION
9
+ s.authors = ["inBay Technologies Inc."]
10
+ s.email = ["support@inbaytech.com"]
11
+ s.homepage = "https://github.com/inbaytech/omniauth-idq"
12
+ s.summary = %q{OmniAuth strategy for idQ}
13
+ s.description = %q{OmniAuth strategy for idQ Trust as a Service}
14
+ s.license = "GPL-3.0"
15
+
16
+ s.rubyforge_project = "omniauth-idq"
17
+
18
+ s.files = Dir[
19
+ 'lib/**/*',
20
+ 'spec/**/*',
21
+ '*.gemspec',
22
+ '*.md',
23
+ 'Rakefile',
24
+ 'Gemfile',
25
+ 'LICENSE.md',
26
+ 'README.md'
27
+ ]
28
+ s.require_paths = ["lib"]
29
+
30
+ s.add_runtime_dependency 'omniauth-oauth2', '~> 1.4'
31
+ s.add_development_dependency 'rspec', '~> 2.7'
32
+ s.add_development_dependency 'rack-test', '~> 0'
33
+ s.add_development_dependency 'simplecov', '~> 0'
34
+ s.add_development_dependency 'webmock', '~> 0'
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Idq do
4
+ subject do
5
+ OmniAuth::Strategies::Idq.new({})
6
+ end
7
+
8
+ context "client options" do
9
+ it 'should have correct name' do
10
+ subject.options.name.should eq("idq")
11
+ end
12
+
13
+ it 'should have correct site' do
14
+ subject.options.client_options.site.should eq('https://taas.idquanta.com')
15
+ end
16
+
17
+ it 'should have correct authorize url' do
18
+ subject.options.client_options.authorize_path.should eq('/idqoauth/api/v1/auth)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
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 'webmock/rspec'
8
+ require 'omniauth'
9
+ require 'omniauth-idq'
10
+
11
+ RSpec.configure do |config|
12
+ config.include WebMock::API
13
+ config.include Rack::Test::Methods
14
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-idq
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - inBay Technologies Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-14 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.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
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: simplecov
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: webmock
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: OmniAuth strategy for idQ Trust as a Service
84
+ email:
85
+ - support@inbaytech.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - LICENSE.md
92
+ - README.md
93
+ - Rakefile
94
+ - lib/omniauth-idq.rb
95
+ - lib/omniauth-idq/version.rb
96
+ - lib/omniauth/strategies/idq.rb
97
+ - omniauth-idq.gemspec
98
+ - spec/omniauth/strategies/idq-spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/inbaytech/omniauth-idq
101
+ licenses:
102
+ - GPL-3.0
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project: omniauth-idq
120
+ rubygems_version: 2.7.4
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: OmniAuth strategy for idQ
124
+ test_files: []