identity-gateway 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 154c10b5f5b445e8c42a995250e2c081a081cde4
4
+ data.tar.gz: e10fba837fb8e7295b95d452c5e5e3ada38e4d14
5
+ SHA512:
6
+ metadata.gz: ee1252a86faae0481a91a69b5b11d61a67354ee4cee095066dfc2e952402d87d4bfc5652ab11f7cce6fc30d9abea854fac5f685aed0f7c0a60e0cb212c67578e
7
+ data.tar.gz: 2ede4fe69453b0b9327f161026d469ceb9c9b8626474947c1b96f150447615b89bcc256c92ef3ee6ed181be404c990f7032d2b557dbd7d2d91ce13ab2f092d1f
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,11 @@
1
+ Metrics/LineLength:
2
+ Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ Enabled: false
6
+
7
+ Metrics/ModuleLength:
8
+ Enabled: false
9
+
10
+ Style/Documentation:
11
+ Enabled: false
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in identity-gateway.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Loic Kartono
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,143 @@
1
+ # Identity::Gateway
2
+
3
+ [![Gem Version][GV img]][Gem Version]
4
+ [Gem Version]: https://rubygems.org/gems/identity-gateway
5
+ [GV img]: https://img.shields.io/gem/v/identity-gateway.svg
6
+
7
+ Identity's gateway provider for Ruby and Rails applications. Act as a man in the middle between backend services and Identity.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'identity-gateway', '~> 1.0'
15
+ ```
16
+
17
+ Use Bundler to install the dependency:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ Once you've configured it, you can use the `Identity::Gateway::Provider` class in order to act as your authentication mechanism.
24
+
25
+ ```ruby
26
+ # app/controllers/api_controller.rb
27
+ module Api
28
+ class ApiController < ApplicationController
29
+ def authorize_access!
30
+ @provider = Identity::Gateway::Provider.new(request)
31
+ @provider.authorize!
32
+ end
33
+ end
34
+ end
35
+
36
+ # app/controllers/api/v1/posts_controller.rb
37
+ module Api
38
+ module V1
39
+ class PostsController < Api::ApiController
40
+ before_action :authorize_access!, only: [:index]
41
+
42
+ def index
43
+ # Only authenticated users can access it.
44
+ end
45
+ end
46
+ end
47
+ end
48
+ ```
49
+
50
+ ### Current subject
51
+
52
+ A subject is the instance associated to the model you specify using the `model` option when you configure the gem. Any instance of `Identity::Gateway::Provider` expose a public method name `current_resource` that allows you to retrieve the resource associated to a given access token.
53
+
54
+ ```ruby
55
+ # app/controllers/api_controller.rb
56
+ module Api
57
+ class ApiController < ApplicationController
58
+ def authorize_access!
59
+ @provider = Identity::Gateway::Provider.new(request)
60
+ @provider.authorize!
61
+ end
62
+
63
+ def current_user
64
+ @current_user ||= @provider.current_resource
65
+ end
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### Rescuing a denied Authorization in Rails
71
+
72
+ Identity::Gateway raises a `Identity::Gateway::Unauthorized` error you can
73
+ [rescue_from](http://guides.rubyonrails.org/action_controller_overview.html#rescue-from)
74
+ in your `ApplicationController`. You can customize the `unauthorized_response`
75
+ method in every controller.
76
+
77
+ ```ruby
78
+ class ApplicationController < ActionController::Base
79
+ protect_from_forgery with: :null_session
80
+
81
+ rescue_from Identity::Gateway::Unauthorized, with: :unauthorized_response
82
+
83
+ private
84
+
85
+ def unauthorized_response
86
+ render json: { message: 'You need to sign in before continuing.' }, status: :unauthorized
87
+ end
88
+ end
89
+ ```
90
+
91
+ Alternatively, you can globally handle Identity::Gateway::Unauthorized's by having rails handle them as a 403 error and serving a 403 error page. Add the following to application.rb:
92
+
93
+ ```ruby
94
+ config.action_dispatch.rescue_responses['Identity::Gateway::Unauthorized'] = :forbidden
95
+ ```
96
+
97
+ ## Configuration
98
+
99
+ You can configure Identity::Gateway by creating an initializer `config/initializers/identity_gateway.rb` and passing it a `configure` block:
100
+
101
+ ```ruby
102
+ Identity::Gateway.configure do |config|
103
+ # Define options here
104
+ end
105
+ ```
106
+
107
+ ## Options
108
+
109
+ ### model
110
+
111
+ This option allows you to define the name of the model you wish to associate. For example, if you handle cache for users with a User model, then it could looks like:
112
+
113
+ ```ruby
114
+ config.model = 'User'
115
+ ```
116
+
117
+ ### provider_url
118
+
119
+ This option allows you to define the url to your Identity server:
120
+
121
+ ```ruby
122
+ config.provider_url = 'https://identity.domain.com'
123
+ ```
124
+
125
+ ### identity_path
126
+
127
+ This option allows you to define the path on Identity that return the information about the current user. Generally, this path will be `/me`:
128
+
129
+ ```ruby
130
+ config.identity_path = '/me'
131
+ ```
132
+
133
+ ### version_header
134
+
135
+ This option allows you to define the `Accept` header use to determinate which version of Identity's API you which to use:
136
+
137
+ ```ruby
138
+ config.version_header = 'application/vnd.wamland+json; version=1'
139
+ ```
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wamland-team/identity-gateway.
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'identity/gateway'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'identity/gateway/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'identity-gateway'
8
+ spec.version = Identity::Gateway::VERSION
9
+ spec.authors = ['Loic Kartono']
10
+ spec.email = ['kartono.loic@gmail.com']
11
+
12
+ spec.summary = 'Identity\'s gateway provider'
13
+ spec.description = 'Identity\'s gateway provider for Ruby and Rails applications'
14
+ spec.homepage = 'https://github.com/wamland-team/identity-gateway'
15
+ spec.license = 'MIT'
16
+
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+
24
+ spec.required_ruby_version = '>= 1.9.3'
25
+
26
+ spec.add_dependency 'httparty', '~> 0.14'
27
+ spec.add_development_dependency 'bundler', '~> 1.14.6', '>= 1.14.6'
28
+ spec.add_development_dependency 'rake', '~> 11.3'
29
+ spec.add_development_dependency 'rspec', '~> 3.5'
30
+ spec.add_development_dependency 'rubocop', '~> 0.47.1'
31
+ end
@@ -0,0 +1,36 @@
1
+ require 'identity/gateway/version'
2
+ require 'identity/gateway/configuration'
3
+
4
+ module Identity
5
+ module Gateway
6
+ class << self
7
+ attr_writer :configuration
8
+ end
9
+
10
+ # Access module configuration.
11
+ #
12
+ # ==== Returns
13
+ # * <tt>Identity::Gateway::Configuration</tt> - instance
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ # Reset configuration to defaults.
19
+ #
20
+ # ==== Returns
21
+ # * <tt>Identity::Gateway::Configuration</tt> - instance
22
+ def self.reset
23
+ @configuration = Configuration.new
24
+ end
25
+
26
+ # Configure block.
27
+ #
28
+ # ==== Returns
29
+ # * <tt>void</tt>
30
+ def self.configure
31
+ yield(configuration)
32
+ end
33
+ end
34
+ end
35
+
36
+ require 'identity/gateway/provider'
@@ -0,0 +1,16 @@
1
+ module Identity
2
+ module Gateway
3
+ class Configuration
4
+ attr_accessor :model, :provider_url, :identity_path, :version_header
5
+
6
+ # Initialize a configuration.
7
+ #
8
+ # ==== Returns
9
+ # * <tt>Identity::Gateway::Configuration</tt> - New instance.
10
+ def initialize
11
+ @model = 'User'
12
+ @identity_path = '/me'
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,81 @@
1
+ require 'httparty'
2
+
3
+ module Identity
4
+ module Gateway
5
+ class Unauthorized < StandardError; end
6
+ class Provider
7
+ include HTTParty
8
+
9
+ # Class initializer.
10
+ #
11
+ # ==== Parameters
12
+ # * <tt>request</tt> - Current request.
13
+ #
14
+ # ==== Returns
15
+ # Assigns arguments to instance variables.
16
+ def initialize(request)
17
+ @settings = Identity::Gateway.configuration
18
+ @request = request
19
+ @response = {}
20
+ @subject = nil
21
+ @model = Object.const_get(@settings.model)
22
+ end
23
+
24
+ # Get token resource owner.
25
+ #
26
+ # ==== Returns
27
+ # * <tt>Object</tt> - HTTParty response or identity error.
28
+ def authorize!
29
+ token = request_token
30
+ @subject = @model.find_by(token: token)
31
+ return unless (@subject && @subject.token_has_expired?) || @subject.nil?
32
+ authorize_from_provider
33
+ end
34
+
35
+ # The current instance object associated to
36
+ # the model define in the configuration.
37
+ #
38
+ # ==== Returns
39
+ # * <tt>Object</tt> - instance or nil.
40
+ def current_resource
41
+ @subject
42
+ end
43
+
44
+ protected
45
+
46
+ # Try to authorize token against provider.
47
+ #
48
+ # ==== Returns
49
+ # * <tt>ActiveRecord|Error</tt> - User object, error otherwise.
50
+ def authorize_from_provider
51
+ @response = self.class.get(
52
+ @settings.provider_url + @settings.identity_path,
53
+ headers: api_headers
54
+ )
55
+
56
+ raise Unauthorized if @response.code == 401
57
+ @subject = @model.from_oauth_provider(@response.parsed_response)
58
+ end
59
+
60
+ # Get oauth token from headers.
61
+ #
62
+ # ==== Returns
63
+ # * <tt>String</tt> - token.
64
+ def request_token
65
+ (@request.headers['Authorization'] || '').gsub('Bearer ', '')
66
+ end
67
+
68
+ # Required headers.
69
+ #
70
+ # ==== Returns
71
+ # * <tt>Hash</tt> - Hash of headers.
72
+ def api_headers
73
+ {
74
+ 'Content-Type' => 'application/json',
75
+ 'Accept' => @settings.version_header || '',
76
+ 'Authorization' => @request.headers['Authorization'] || ''
77
+ }
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ module Identity
2
+ module Gateway
3
+ VERSION = '1.0.1'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: identity-gateway
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Loic Kartono
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.14'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.14.6
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.14.6
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 1.14.6
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.14.6
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '11.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '11.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.5'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.5'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.47.1
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.47.1
89
+ description: Identity's gateway provider for Ruby and Rails applications
90
+ email:
91
+ - kartono.loic@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".rubocop.yml"
99
+ - ".travis.yml"
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - Rakefile
104
+ - bin/console
105
+ - bin/setup
106
+ - identity-gateway.gemspec
107
+ - lib/identity/gateway.rb
108
+ - lib/identity/gateway/configuration.rb
109
+ - lib/identity/gateway/provider.rb
110
+ - lib/identity/gateway/version.rb
111
+ homepage: https://github.com/wamland-team/identity-gateway
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 1.9.3
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5.1
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Identity's gateway provider
135
+ test_files: []