grape_simple_auth 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +90 -0
- data/LICENSE.txt +21 -0
- data/README.md +155 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/grape_simple_auth.gemspec +40 -0
- data/lib/generators/grape_simple_auth/install_generator.rb +9 -0
- data/lib/generators/templates/initializer.rb +7 -0
- data/lib/grape_simple_auth.rb +30 -0
- data/lib/grape_simple_auth/auth_methods/auth_methods.rb +44 -0
- data/lib/grape_simple_auth/auth_strategies/swagger.rb +33 -0
- data/lib/grape_simple_auth/base_strategy.rb +5 -0
- data/lib/grape_simple_auth/configuration.rb +27 -0
- data/lib/grape_simple_auth/errors/invalid_scope.rb +9 -0
- data/lib/grape_simple_auth/errors/invalid_token.rb +9 -0
- data/lib/grape_simple_auth/extension.rb +17 -0
- data/lib/grape_simple_auth/helpers.rb +4 -0
- data/lib/grape_simple_auth/oauth2.rb +81 -0
- data/lib/grape_simple_auth/version.rb +3 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aa27a75038f3c3f703c22b7a3e2e28693b3d84d55eb10de0b98f8d950d13c7b0
|
4
|
+
data.tar.gz: 56d458627aebd0603c17d318c645aa2123a2d745eecd68030f412b8e5ec660ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d45a62cd7c1da9c1798f2e2c048acb646f17c4b518152dbe202757802ee7dbbbaa1746769b05db8b59b915bf818cb32bb5675c490ef2eb49fcde86de48b0ebcd
|
7
|
+
data.tar.gz: 4976109572f04fbac0ef46d666d09c74b9ea38e4fb4a064ca75315e078b5213e3eeddeb52c8fd7d49603a4d9a87126f813aaa5f4ba1165889b4beda9a33b9eee
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at helmy@extrainteger.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grape_simple_auth (0.1.1)
|
5
|
+
grape (~> 0)
|
6
|
+
httparty (~> 0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.2.2)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
axiom-types (0.1.1)
|
17
|
+
descendants_tracker (~> 0.0.4)
|
18
|
+
ice_nine (~> 0.11.0)
|
19
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
20
|
+
builder (3.2.3)
|
21
|
+
coercible (1.0.0)
|
22
|
+
descendants_tracker (~> 0.0.1)
|
23
|
+
concurrent-ruby (1.1.4)
|
24
|
+
descendants_tracker (0.0.4)
|
25
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
26
|
+
diff-lcs (1.3)
|
27
|
+
equalizer (0.0.11)
|
28
|
+
grape (0.19.2)
|
29
|
+
activesupport
|
30
|
+
builder
|
31
|
+
hashie (>= 2.1.0)
|
32
|
+
multi_json (>= 1.3.2)
|
33
|
+
multi_xml (>= 0.5.2)
|
34
|
+
mustermann-grape (~> 1.0.0)
|
35
|
+
rack (>= 1.3.0)
|
36
|
+
rack-accept
|
37
|
+
virtus (>= 1.0.0)
|
38
|
+
hashie (3.6.0)
|
39
|
+
httparty (0.16.3)
|
40
|
+
mime-types (~> 3.0)
|
41
|
+
multi_xml (>= 0.5.2)
|
42
|
+
i18n (1.3.0)
|
43
|
+
concurrent-ruby (~> 1.0)
|
44
|
+
ice_nine (0.11.2)
|
45
|
+
mime-types (3.2.2)
|
46
|
+
mime-types-data (~> 3.2015)
|
47
|
+
mime-types-data (3.2018.0812)
|
48
|
+
minitest (5.11.3)
|
49
|
+
multi_json (1.13.1)
|
50
|
+
multi_xml (0.6.0)
|
51
|
+
mustermann (1.0.3)
|
52
|
+
mustermann-grape (1.0.0)
|
53
|
+
mustermann (~> 1.0.0)
|
54
|
+
rack (2.0.6)
|
55
|
+
rack-accept (0.4.5)
|
56
|
+
rack (>= 0.4)
|
57
|
+
rake (10.5.0)
|
58
|
+
rspec (3.7.0)
|
59
|
+
rspec-core (~> 3.7.0)
|
60
|
+
rspec-expectations (~> 3.7.0)
|
61
|
+
rspec-mocks (~> 3.7.0)
|
62
|
+
rspec-core (3.7.0)
|
63
|
+
rspec-support (~> 3.7.0)
|
64
|
+
rspec-expectations (3.7.0)
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
66
|
+
rspec-support (~> 3.7.0)
|
67
|
+
rspec-mocks (3.7.0)
|
68
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
69
|
+
rspec-support (~> 3.7.0)
|
70
|
+
rspec-support (3.7.0)
|
71
|
+
thread_safe (0.3.6)
|
72
|
+
tzinfo (1.2.5)
|
73
|
+
thread_safe (~> 0.1)
|
74
|
+
virtus (1.0.5)
|
75
|
+
axiom-types (~> 0.1)
|
76
|
+
coercible (~> 1.0)
|
77
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
78
|
+
equalizer (~> 0.0, >= 0.0.9)
|
79
|
+
|
80
|
+
PLATFORMS
|
81
|
+
ruby
|
82
|
+
|
83
|
+
DEPENDENCIES
|
84
|
+
bundler (~> 1.16)
|
85
|
+
grape_simple_auth!
|
86
|
+
rake (~> 10.0)
|
87
|
+
rspec (~> 3.0)
|
88
|
+
|
89
|
+
BUNDLED WITH
|
90
|
+
1.16.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Yunan Helmy
|
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,155 @@
|
|
1
|
+
# Grape Simple Auth
|
2
|
+
|
3
|
+
Grape Simple Auth is a Grape middleware to connect your API resources with your API authenticator.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'grape_simple_auth'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install grape_simple_auth
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Install generator
|
25
|
+
|
26
|
+
On your first install, run this generator :
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
rails g grape_simple_auth:install
|
30
|
+
```
|
31
|
+
|
32
|
+
### Configuration
|
33
|
+
|
34
|
+
After you install generator, it is expecting you to add details :
|
35
|
+
|
36
|
+
1. `ENV["AUTH_BASE_URL"]`
|
37
|
+
2. `ENV["VERIFY_ENDPOINT]`
|
38
|
+
|
39
|
+
### Usage with Grape
|
40
|
+
|
41
|
+
You will need to use the middleware in your main API :
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# use middleware
|
45
|
+
use ::GrapeSimpleAuth::Oauth2
|
46
|
+
```
|
47
|
+
|
48
|
+
You could also use the helpers :
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# use helpers
|
52
|
+
helpers ::GrapeSimpleAuth::Helpers
|
53
|
+
```
|
54
|
+
|
55
|
+
And also don't forget to rescue the invalid token :
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
# rescue invalid token
|
59
|
+
rescue_from GrapeSimpleAuth::Errors::InvalidToken do |e|
|
60
|
+
error!(e, 401)
|
61
|
+
end
|
62
|
+
rescue_from GrapeSimpleAuth::Errors::InvalidScope do |e|
|
63
|
+
error!(e, 401)
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
### Protecting your endpoint
|
68
|
+
|
69
|
+
In your endpoint you need to define which protected endpoint by adding this DSL :
|
70
|
+
|
71
|
+
1. `oauth2`
|
72
|
+
2. `oauth2 "email"`
|
73
|
+
|
74
|
+
Example :
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
desc "Your protected endpoint"
|
78
|
+
oauth2
|
79
|
+
get :protected do
|
80
|
+
# your code goes here
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
desc "Your protected endpoint with defined scope"
|
86
|
+
oauth2 "email"
|
87
|
+
get :protected do
|
88
|
+
# your code goes here
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
## Nice feature
|
93
|
+
|
94
|
+
From your protected endpoint you could get :
|
95
|
+
|
96
|
+
1. `the_access_token` => Your access token
|
97
|
+
2. `credentials` => Full credentials
|
98
|
+
3. `current_user` => Current user (Openstruct, which means you could get `current_user.id`)
|
99
|
+
|
100
|
+
## Integration with API authenticator
|
101
|
+
|
102
|
+
Simple Auth needs your API authenticator to output specific payload :
|
103
|
+
|
104
|
+
1. `data` -> `info` => It will be your current_user
|
105
|
+
2. `data` -> `credential` => It will be your credential
|
106
|
+
|
107
|
+
Any payload inside `info` will be returned as `current_user`. Any payload inside `credential` will be returned as `credentials`
|
108
|
+
|
109
|
+
Example output of your verification endpoint :
|
110
|
+
|
111
|
+
```json
|
112
|
+
{
|
113
|
+
"data": {
|
114
|
+
"info": {
|
115
|
+
"id": "123123",
|
116
|
+
"email": "someone@somewhere.com",
|
117
|
+
"first_name": "John",
|
118
|
+
"last_name": "Doe",
|
119
|
+
},
|
120
|
+
"credential": {
|
121
|
+
"access_token": "",
|
122
|
+
"scopes": [],
|
123
|
+
"token_type": "bearer",
|
124
|
+
"expires_in": 7200,
|
125
|
+
"refresh_token": "",
|
126
|
+
"created_at": 1545487942
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}
|
130
|
+
```
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
## TODO
|
135
|
+
|
136
|
+
- Add rspec test
|
137
|
+
- Configurable class of current_user instead of `OpenStruct`
|
138
|
+
|
139
|
+
## Development
|
140
|
+
|
141
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
142
|
+
|
143
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
144
|
+
|
145
|
+
## Contributing
|
146
|
+
|
147
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/extrainteger/simple_auth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
148
|
+
|
149
|
+
## License
|
150
|
+
|
151
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
152
|
+
|
153
|
+
## Code of Conduct
|
154
|
+
|
155
|
+
Everyone interacting in the GrapeSimpleAuth project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/extrainteger/simple_auth/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "grape_simple_auth"
|
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
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "grape_simple_auth/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grape_simple_auth"
|
8
|
+
spec.version = GrapeSimpleAuth::VERSION
|
9
|
+
spec.authors = ["Yunan Helmy"]
|
10
|
+
spec.email = ["helmy@extrainteger.com"]
|
11
|
+
|
12
|
+
spec.summary = "Grape Simple Auth is a Grape middleware to connect your API resources with your API authenticator."
|
13
|
+
spec.description = "Grape Simple Auth is a Grape middleware to connect your API resources with your API authenticator."
|
14
|
+
spec.homepage = "https://github.com/extrainteger/simple_auth"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
spec.add_dependency "httparty", "~> 0.16.3"
|
39
|
+
spec.add_dependency "grape", "~> 1.1.0"
|
40
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "grape_simple_auth/version"
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require 'grape'
|
5
|
+
|
6
|
+
require 'grape_simple_auth/configuration'
|
7
|
+
|
8
|
+
require 'grape_simple_auth/oauth2'
|
9
|
+
require 'grape_simple_auth/extension'
|
10
|
+
require 'grape_simple_auth/helpers'
|
11
|
+
|
12
|
+
require 'grape_simple_auth/base_strategy'
|
13
|
+
require 'grape_simple_auth/auth_strategies/swagger'
|
14
|
+
require 'grape_simple_auth/auth_methods/auth_methods'
|
15
|
+
|
16
|
+
require 'grape_simple_auth/errors/invalid_token'
|
17
|
+
require 'grape_simple_auth/errors/invalid_scope'
|
18
|
+
|
19
|
+
module GrapeSimpleAuth
|
20
|
+
extend GrapeSimpleAuth::Configuration
|
21
|
+
|
22
|
+
define_setting :url, "http://localhost:4000"
|
23
|
+
define_setting :verify_endpoint, "/v1/valid_token/verify"
|
24
|
+
define_setting :auth_strategy, "swagger"
|
25
|
+
|
26
|
+
def self.verify_url
|
27
|
+
url + verify_endpoint
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module GrapeSimpleAuth
|
2
|
+
module AuthMethods
|
3
|
+
attr_accessor :the_access_token, :current_user, :credentials
|
4
|
+
|
5
|
+
def protected_endpoint=(protected)
|
6
|
+
@protected_endpoint = protected
|
7
|
+
end
|
8
|
+
|
9
|
+
def protected_endpoint?
|
10
|
+
@protected_endpoint || false
|
11
|
+
end
|
12
|
+
|
13
|
+
def the_access_token
|
14
|
+
@_the_access_token
|
15
|
+
end
|
16
|
+
|
17
|
+
def the_access_token=(token)
|
18
|
+
@_the_access_token = token
|
19
|
+
end
|
20
|
+
|
21
|
+
def current_user=(info)
|
22
|
+
@_current_user = JSON.parse(info.to_json, object_class: DataStruct)
|
23
|
+
end
|
24
|
+
|
25
|
+
def current_user
|
26
|
+
@_current_user
|
27
|
+
end
|
28
|
+
|
29
|
+
def credentials=(data)
|
30
|
+
@credentials = JSON.parse(data.to_json, object_class: DataStruct)
|
31
|
+
end
|
32
|
+
|
33
|
+
def credentials
|
34
|
+
@credentials
|
35
|
+
end
|
36
|
+
|
37
|
+
class DataStruct < OpenStruct
|
38
|
+
def as_json(*args)
|
39
|
+
super.as_json['table']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module GrapeSimpleAuth
|
2
|
+
module AuthStrategies
|
3
|
+
class Swagger < GrapeSimpleAuth::BaseStrategy
|
4
|
+
|
5
|
+
def endpoint_protected?
|
6
|
+
has_authorizations? && !!authorization_type_oauth2
|
7
|
+
end
|
8
|
+
|
9
|
+
def has_auth_scopes?
|
10
|
+
endpoint_protected? && !authorization_type_oauth2.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def auth_scopes
|
14
|
+
authorization_type_oauth2.map { |hash| hash[:scope].to_sym }
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def has_authorizations?
|
20
|
+
!!endpoint_authorizations
|
21
|
+
end
|
22
|
+
|
23
|
+
def endpoint_authorizations
|
24
|
+
api_context.options[:route_options][:authorizations]
|
25
|
+
end
|
26
|
+
|
27
|
+
def authorization_type_oauth2
|
28
|
+
endpoint_authorizations[:oauth2]
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module GrapeSimpleAuth
|
2
|
+
module Configuration
|
3
|
+
def setup
|
4
|
+
yield self
|
5
|
+
end
|
6
|
+
|
7
|
+
def define_setting(name, default = nil)
|
8
|
+
class_variable_set("@@#{name}", default)
|
9
|
+
|
10
|
+
define_class_method "#{name}=" do |value|
|
11
|
+
class_variable_set("@@#{name}", value)
|
12
|
+
end
|
13
|
+
|
14
|
+
define_class_method name do
|
15
|
+
class_variable_get("@@#{name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def define_class_method(name, &block)
|
22
|
+
(class << self; self; end).instance_eval do
|
23
|
+
define_method name, &block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module GrapeSimpleAuth
|
2
|
+
module Extension
|
3
|
+
|
4
|
+
def oauth2(*scopes)
|
5
|
+
description = if respond_to?(:route_setting) # >= grape-0.10.0
|
6
|
+
route_setting(:description) || route_setting(:description, {})
|
7
|
+
else
|
8
|
+
@last_description ||= {}
|
9
|
+
end
|
10
|
+
|
11
|
+
description[:auth] = { scopes: scopes }
|
12
|
+
description[:authorizations] = { oauth2: scopes.map { |x| { scope: x } } }
|
13
|
+
end
|
14
|
+
|
15
|
+
Grape::API.extend self
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rack/auth/abstract/request'
|
2
|
+
|
3
|
+
module GrapeSimpleAuth
|
4
|
+
class Oauth2 < Grape::Middleware::Base
|
5
|
+
attr_reader :auth_strategy
|
6
|
+
|
7
|
+
def context
|
8
|
+
env['api.endpoint']
|
9
|
+
end
|
10
|
+
|
11
|
+
def the_request=(env)
|
12
|
+
@_the_request = ActionDispatch::Request.new(env)
|
13
|
+
end
|
14
|
+
|
15
|
+
def request
|
16
|
+
@_the_request
|
17
|
+
end
|
18
|
+
|
19
|
+
def token
|
20
|
+
token = if request.headers["Authorization"].present?
|
21
|
+
request.headers["Authorization"].include?("bearer") ? request.headers["Authorization"].try("split", "bearer").try(:last).try(:strip) : request.headers["Authorization"]
|
22
|
+
else
|
23
|
+
request.parameters["access_token"]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
############
|
29
|
+
# Authorization control.
|
30
|
+
############
|
31
|
+
|
32
|
+
def endpoint_protected?
|
33
|
+
auth_strategy.endpoint_protected?
|
34
|
+
end
|
35
|
+
|
36
|
+
def auth_scopes
|
37
|
+
return *nil unless auth_strategy.has_auth_scopes?
|
38
|
+
auth_strategy.auth_scopes
|
39
|
+
end
|
40
|
+
|
41
|
+
def authorize!(*scopes)
|
42
|
+
response = HTTParty.get(GrapeSimpleAuth.verify_url, {query: {access_token: token}})
|
43
|
+
if response.code == 200
|
44
|
+
scopes = response.parsed_response["data"]["credential"]["scopes"]
|
45
|
+
unless auth_strategy.auth_scopes & scopes == auth_strategy.auth_scopes
|
46
|
+
raise GrapeSimpleAuth::Errors::InvalidScope
|
47
|
+
end
|
48
|
+
return response
|
49
|
+
end
|
50
|
+
raise GrapeSimpleAuth::Errors::InvalidToken
|
51
|
+
end
|
52
|
+
|
53
|
+
############
|
54
|
+
# Grape middleware methods
|
55
|
+
############
|
56
|
+
|
57
|
+
def before
|
58
|
+
set_auth_strategy(GrapeSimpleAuth.auth_strategy)
|
59
|
+
auth_strategy.api_context = context
|
60
|
+
context.extend(GrapeSimpleAuth::AuthMethods)
|
61
|
+
|
62
|
+
context.protected_endpoint = endpoint_protected?
|
63
|
+
return unless context.protected_endpoint?
|
64
|
+
|
65
|
+
self.the_request = env
|
66
|
+
resp = authorize!(*auth_scopes)
|
67
|
+
context.the_access_token = token
|
68
|
+
context.current_user = resp.parsed_response["data"]["info"] rescue nil
|
69
|
+
context.credentials = resp.parsed_response["data"]["credential"] rescue nil
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def set_auth_strategy(strategy)
|
76
|
+
@auth_strategy = GrapeSimpleAuth::AuthStrategies.const_get(strategy.to_s.capitalize.to_s).new
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape_simple_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yunan Helmy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: httparty
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.16.3
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.16.3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: grape
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.1.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.1.0
|
83
|
+
description: Grape Simple Auth is a Grape middleware to connect your API resources
|
84
|
+
with your API authenticator.
|
85
|
+
email:
|
86
|
+
- helmy@extrainteger.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- grape_simple_auth.gemspec
|
103
|
+
- lib/generators/grape_simple_auth/install_generator.rb
|
104
|
+
- lib/generators/templates/initializer.rb
|
105
|
+
- lib/grape_simple_auth.rb
|
106
|
+
- lib/grape_simple_auth/auth_methods/auth_methods.rb
|
107
|
+
- lib/grape_simple_auth/auth_strategies/swagger.rb
|
108
|
+
- lib/grape_simple_auth/base_strategy.rb
|
109
|
+
- lib/grape_simple_auth/configuration.rb
|
110
|
+
- lib/grape_simple_auth/errors/invalid_scope.rb
|
111
|
+
- lib/grape_simple_auth/errors/invalid_token.rb
|
112
|
+
- lib/grape_simple_auth/extension.rb
|
113
|
+
- lib/grape_simple_auth/helpers.rb
|
114
|
+
- lib/grape_simple_auth/oauth2.rb
|
115
|
+
- lib/grape_simple_auth/version.rb
|
116
|
+
homepage: https://github.com/extrainteger/simple_auth
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata:
|
120
|
+
allowed_push_host: https://rubygems.org/
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.7.7
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Grape Simple Auth is a Grape middleware to connect your API resources with
|
141
|
+
your API authenticator.
|
142
|
+
test_files: []
|