identity-gateway 1.1.1
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +11 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/circle.yml +11 -0
- data/identity-gateway.gemspec +31 -0
- data/lib/identity/gateway.rb +36 -0
- data/lib/identity/gateway/configuration.rb +16 -0
- data/lib/identity/gateway/provider.rb +93 -0
- data/lib/identity/gateway/version.rb +5 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8a472a877c9f41b3452ce62134871ead4bafba3b
|
4
|
+
data.tar.gz: 1ebe4c7eb1c3b8745dfd734d76837c48486dab1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d5ae5c137a7dbe728b5482f7c4ed7a40396b1663cfe302bce1212d403972d5081ceb41f1cc8a2c0b0c492e46081737cc0046a7d73f7d6000cde8e4c3015760a1
|
7
|
+
data.tar.gz: 5cd96de33a0d261670061e200a0a4ae738bdb525713334e895f40fa1ae401dd520f25f0ed243acba2ff01c74fe25c8e1e90a3dce6282a9535223a7a015d66b03
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.4
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# Identity::Gateway
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/identity-gateway)
|
4
|
+
[](https://circleci.com/gh/wamland-team/identity-gateway)
|
5
|
+
|
6
|
+
Identity's gateway provider for Ruby and Rails applications. Act as a man in the middle between backend services and Identity.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'identity-gateway'
|
14
|
+
```
|
15
|
+
|
16
|
+
Use Bundler to install the dependency:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Once you've configured it, you can use the `Identity::Gateway::Provider` class in order to act as your authentication mechanism.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# app/controllers/api_controller.rb
|
26
|
+
module Api
|
27
|
+
class ApiController < ApplicationController
|
28
|
+
def authorize_access!
|
29
|
+
@provider = Identity::Gateway::Provider.new(request)
|
30
|
+
@provider.authorize!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# app/controllers/api/v1/posts_controller.rb
|
36
|
+
module Api
|
37
|
+
module V1
|
38
|
+
class PostsController < Api::ApiController
|
39
|
+
before_action :authorize_access!, only: [:index]
|
40
|
+
|
41
|
+
def index
|
42
|
+
# Only authenticated users can access it.
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### Current subject
|
50
|
+
|
51
|
+
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.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# app/controllers/api_controller.rb
|
55
|
+
module Api
|
56
|
+
class ApiController < ApplicationController
|
57
|
+
def authorize_access!
|
58
|
+
@provider = Identity::Gateway::Provider.new(request)
|
59
|
+
@provider.authorize!
|
60
|
+
end
|
61
|
+
|
62
|
+
def current_user
|
63
|
+
@current_user ||= @provider.current_resource
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
### Rescuing a denied Authorization in Rails
|
70
|
+
|
71
|
+
Identity::Gateway raises a `Identity::Gateway::Unauthorized` error you can
|
72
|
+
[rescue_from](http://guides.rubyonrails.org/action_controller_overview.html#rescue-from)
|
73
|
+
in your `ApplicationController`. You can customize the `unauthorized_response`
|
74
|
+
method in every controller.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class ApplicationController < ActionController::Base
|
78
|
+
protect_from_forgery with: :null_session
|
79
|
+
|
80
|
+
rescue_from Identity::Gateway::Unauthorized, with: :unauthorized_response
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def unauthorized_response
|
85
|
+
render json: { message: 'You need to sign in before continuing.' }, status: :unauthorized
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
Alternatively, you can globally handle Identity::Gateway::Unauthorized's by having rails handle them as a 401 error and serving a 401 error page. Add the following to application.rb:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
config.action_dispatch.rescue_responses['Identity::Gateway::Unauthorized'] = :unauthorized
|
94
|
+
```
|
95
|
+
|
96
|
+
## Configuration
|
97
|
+
|
98
|
+
You can configure Identity::Gateway by creating an initializer `config/initializers/identity_gateway.rb` and passing it a `configure` block:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
Identity::Gateway.configure do |config|
|
102
|
+
# Define options here
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
## Options
|
107
|
+
|
108
|
+
### model
|
109
|
+
|
110
|
+
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:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
config.model = 'User'
|
114
|
+
```
|
115
|
+
|
116
|
+
### provider_url
|
117
|
+
|
118
|
+
This option allows you to define the url to your Identity server:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
config.provider_url = 'https://identity.domain.com'
|
122
|
+
```
|
123
|
+
|
124
|
+
### identity_path
|
125
|
+
|
126
|
+
This option allows you to define the path on Identity that return the information about the current user. Generally, this path will be `/me`:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
config.identity_path = '/me'
|
130
|
+
```
|
131
|
+
|
132
|
+
### version_header
|
133
|
+
|
134
|
+
This option allows you to define the `Accept` header use to determinate which version of Identity's API you which to use:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
config.version_header = 'application/vnd.wamland+json; version=1'
|
138
|
+
```
|
139
|
+
|
140
|
+
## Contributing
|
141
|
+
|
142
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wamland-team/identity-gateway.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -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__)
|
data/bin/setup
ADDED
data/circle.yml
ADDED
@@ -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 = '>= 2.2.2'
|
25
|
+
|
26
|
+
spec.add_dependency 'httparty', '~> 0.13'
|
27
|
+
spec.add_development_dependency 'bundler'
|
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,93 @@
|
|
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
|
+
# Revoke access token.
|
45
|
+
#
|
46
|
+
# ==== Returns
|
47
|
+
# * <tt>HTTParty::Response</tt>.
|
48
|
+
def revoke_access!
|
49
|
+
self.class.post(
|
50
|
+
"#{@settings.provider_url}/oauth/revoke",
|
51
|
+
body: { token: request_token }.to_json,
|
52
|
+
headers: api_headers
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
protected
|
57
|
+
|
58
|
+
# Try to authorize token against provider.
|
59
|
+
#
|
60
|
+
# ==== Returns
|
61
|
+
# * <tt>ActiveRecord|Error</tt> - User object, error otherwise.
|
62
|
+
def authorize_from_provider
|
63
|
+
@response = self.class.get(
|
64
|
+
@settings.provider_url + @settings.identity_path,
|
65
|
+
headers: api_headers
|
66
|
+
)
|
67
|
+
|
68
|
+
raise Unauthorized if @response.code == 401 || @response.parsed_response.nil?
|
69
|
+
@subject = @model.from_oauth_provider(@response.parsed_response)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Get oauth token from headers.
|
73
|
+
#
|
74
|
+
# ==== Returns
|
75
|
+
# * <tt>String</tt> - token.
|
76
|
+
def request_token
|
77
|
+
(@request.headers['Authorization'] || '').gsub('Bearer ', '')
|
78
|
+
end
|
79
|
+
|
80
|
+
# Required headers.
|
81
|
+
#
|
82
|
+
# ==== Returns
|
83
|
+
# * <tt>Hash</tt> - Hash of headers.
|
84
|
+
def api_headers
|
85
|
+
{
|
86
|
+
'Content-Type' => 'application/json',
|
87
|
+
'Accept' => @settings.version_header || '',
|
88
|
+
'Authorization' => @request.headers['Authorization'] || ''
|
89
|
+
}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: identity-gateway
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Loic Kartono
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-22 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.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '11.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '11.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.47.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.47.1
|
83
|
+
description: Identity's gateway provider for Ruby and Rails applications
|
84
|
+
email:
|
85
|
+
- kartono.loic@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".ruby-version"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- circle.yml
|
102
|
+
- identity-gateway.gemspec
|
103
|
+
- lib/identity/gateway.rb
|
104
|
+
- lib/identity/gateway/configuration.rb
|
105
|
+
- lib/identity/gateway/provider.rb
|
106
|
+
- lib/identity/gateway/version.rb
|
107
|
+
homepage: https://github.com/wamland-team/identity-gateway
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 2.2.2
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.4.5.1
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Identity's gateway provider
|
131
|
+
test_files: []
|