grape_oauth2 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +18 -0
  5. data/.travis.yml +42 -0
  6. data/Gemfile +23 -0
  7. data/README.md +820 -0
  8. data/Rakefile +11 -0
  9. data/gemfiles/active_record.rb +25 -0
  10. data/gemfiles/mongoid.rb +14 -0
  11. data/gemfiles/sequel.rb +24 -0
  12. data/grape_oauth2.gemspec +27 -0
  13. data/grape_oauth2.png +0 -0
  14. data/lib/grape_oauth2.rb +129 -0
  15. data/lib/grape_oauth2/configuration.rb +143 -0
  16. data/lib/grape_oauth2/configuration/class_accessors.rb +36 -0
  17. data/lib/grape_oauth2/configuration/validation.rb +71 -0
  18. data/lib/grape_oauth2/endpoints/authorize.rb +34 -0
  19. data/lib/grape_oauth2/endpoints/token.rb +72 -0
  20. data/lib/grape_oauth2/gem_version.rb +24 -0
  21. data/lib/grape_oauth2/generators/authorization.rb +44 -0
  22. data/lib/grape_oauth2/generators/base.rb +26 -0
  23. data/lib/grape_oauth2/generators/token.rb +62 -0
  24. data/lib/grape_oauth2/helpers/access_token_helpers.rb +54 -0
  25. data/lib/grape_oauth2/helpers/oauth_params.rb +41 -0
  26. data/lib/grape_oauth2/mixins/active_record/access_grant.rb +47 -0
  27. data/lib/grape_oauth2/mixins/active_record/access_token.rb +75 -0
  28. data/lib/grape_oauth2/mixins/active_record/client.rb +35 -0
  29. data/lib/grape_oauth2/mixins/mongoid/access_grant.rb +58 -0
  30. data/lib/grape_oauth2/mixins/mongoid/access_token.rb +88 -0
  31. data/lib/grape_oauth2/mixins/mongoid/client.rb +41 -0
  32. data/lib/grape_oauth2/mixins/sequel/access_grant.rb +68 -0
  33. data/lib/grape_oauth2/mixins/sequel/access_token.rb +86 -0
  34. data/lib/grape_oauth2/mixins/sequel/client.rb +46 -0
  35. data/lib/grape_oauth2/responses/authorization.rb +10 -0
  36. data/lib/grape_oauth2/responses/base.rb +56 -0
  37. data/lib/grape_oauth2/responses/token.rb +10 -0
  38. data/lib/grape_oauth2/scopes.rb +74 -0
  39. data/lib/grape_oauth2/strategies/authorization_code.rb +38 -0
  40. data/lib/grape_oauth2/strategies/base.rb +47 -0
  41. data/lib/grape_oauth2/strategies/client_credentials.rb +20 -0
  42. data/lib/grape_oauth2/strategies/password.rb +22 -0
  43. data/lib/grape_oauth2/strategies/refresh_token.rb +47 -0
  44. data/lib/grape_oauth2/unique_token.rb +20 -0
  45. data/lib/grape_oauth2/version.rb +14 -0
  46. data/spec/configuration/config_spec.rb +231 -0
  47. data/spec/configuration/version_spec.rb +12 -0
  48. data/spec/dummy/endpoints/custom_authorization.rb +25 -0
  49. data/spec/dummy/endpoints/custom_token.rb +35 -0
  50. data/spec/dummy/endpoints/status.rb +25 -0
  51. data/spec/dummy/grape_oauth2_config.rb +11 -0
  52. data/spec/dummy/orm/active_record/app/config/db.rb +7 -0
  53. data/spec/dummy/orm/active_record/app/models/access_code.rb +3 -0
  54. data/spec/dummy/orm/active_record/app/models/access_token.rb +3 -0
  55. data/spec/dummy/orm/active_record/app/models/application.rb +3 -0
  56. data/spec/dummy/orm/active_record/app/models/application_record.rb +3 -0
  57. data/spec/dummy/orm/active_record/app/models/user.rb +10 -0
  58. data/spec/dummy/orm/active_record/app/twitter.rb +36 -0
  59. data/spec/dummy/orm/active_record/config.ru +7 -0
  60. data/spec/dummy/orm/active_record/db/schema.rb +53 -0
  61. data/spec/dummy/orm/mongoid/app/config/db.rb +6 -0
  62. data/spec/dummy/orm/mongoid/app/config/mongoid.yml +21 -0
  63. data/spec/dummy/orm/mongoid/app/models/access_code.rb +3 -0
  64. data/spec/dummy/orm/mongoid/app/models/access_token.rb +3 -0
  65. data/spec/dummy/orm/mongoid/app/models/application.rb +3 -0
  66. data/spec/dummy/orm/mongoid/app/models/user.rb +11 -0
  67. data/spec/dummy/orm/mongoid/app/twitter.rb +34 -0
  68. data/spec/dummy/orm/mongoid/config.ru +5 -0
  69. data/spec/dummy/orm/sequel/app/config/db.rb +1 -0
  70. data/spec/dummy/orm/sequel/app/models/access_code.rb +4 -0
  71. data/spec/dummy/orm/sequel/app/models/access_token.rb +4 -0
  72. data/spec/dummy/orm/sequel/app/models/application.rb +4 -0
  73. data/spec/dummy/orm/sequel/app/models/application_record.rb +2 -0
  74. data/spec/dummy/orm/sequel/app/models/user.rb +11 -0
  75. data/spec/dummy/orm/sequel/app/twitter.rb +47 -0
  76. data/spec/dummy/orm/sequel/config.ru +5 -0
  77. data/spec/dummy/orm/sequel/db/schema.rb +50 -0
  78. data/spec/lib/scopes_spec.rb +50 -0
  79. data/spec/mixins/active_record/access_token_spec.rb +185 -0
  80. data/spec/mixins/active_record/client_spec.rb +95 -0
  81. data/spec/mixins/mongoid/access_token_spec.rb +185 -0
  82. data/spec/mixins/mongoid/client_spec.rb +95 -0
  83. data/spec/mixins/sequel/access_token_spec.rb +185 -0
  84. data/spec/mixins/sequel/client_spec.rb +96 -0
  85. data/spec/requests/flows/authorization_code_spec.rb +67 -0
  86. data/spec/requests/flows/client_credentials_spec.rb +101 -0
  87. data/spec/requests/flows/password_spec.rb +210 -0
  88. data/spec/requests/flows/refresh_token_spec.rb +222 -0
  89. data/spec/requests/flows/revoke_token_spec.rb +103 -0
  90. data/spec/requests/protected_resources_spec.rb +64 -0
  91. data/spec/spec_helper.rb +60 -0
  92. data/spec/support/api_helper.rb +11 -0
  93. metadata +257 -0
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'GET Protected Resources' do
4
+ let(:application) { Application.create(name: 'App1') }
5
+ let(:user) { User.create(username: 'Jack Sparrow', password: '12345678') }
6
+ let(:access_token) { AccessToken.create_for(application, user) }
7
+
8
+ context 'with invalid data' do
9
+ it 'returns Unauthorized without Access Token' do
10
+ get 'api/v1/status'
11
+
12
+ expect(last_response.status).to eq 401
13
+
14
+ expect(json_body[:error]).to eq('unauthorized')
15
+ expect(last_response.headers['WWW-Authenticate']).to eq('Bearer realm="Custom Realm"')
16
+ end
17
+
18
+ it 'returns Unauthorized when token scopes are blank' do
19
+ get 'api/v1/status/single_scope', access_token: access_token.token
20
+
21
+ expect(last_response.status).to eq 403
22
+
23
+ expect(json_body[:error]).not_to be_blank
24
+ end
25
+
26
+ it "returns Unauthorized when token scopes doesn't match required scopes" do
27
+ access_token.update(scopes: 'read')
28
+ get 'api/v1/status/multiple_scopes', access_token: access_token.token
29
+
30
+ expect(last_response.status).to eq 403
31
+
32
+ expect(json_body[:error]).not_to be_blank
33
+ end
34
+ end
35
+
36
+ context 'with valid data' do
37
+ it "returns status for endpoint that doesn't requires any scope" do
38
+ get 'api/v1/status', access_token: access_token.token
39
+
40
+ expect(last_response.status).to eq 200
41
+
42
+ expect(json_body[:value]).to eq('Nice day!')
43
+ expect(json_body[:current_user]).to eq('Jack Sparrow')
44
+ end
45
+
46
+ it 'returns status for endpoint with specific scope' do
47
+ access_token.update(scopes: 'read public')
48
+ get 'api/v1/status/single_scope', access_token: access_token.token
49
+
50
+ expect(last_response.status).to eq 200
51
+
52
+ expect(json_body[:value]).to eq('Access granted')
53
+ end
54
+
55
+ it 'returns status for endpoint with specific set of scopes' do
56
+ access_token.update(scopes: 'read write public')
57
+ get 'api/v1/status/multiple_scopes', access_token: access_token.token
58
+
59
+ expect(last_response.status).to eq 200
60
+
61
+ expect(json_body[:value]).to eq('Access granted')
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,60 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ ENV['ORM'] ||= 'active_record'
3
+
4
+ puts "Configured ORM: '#{ENV['ORM']}'"
5
+
6
+ require 'coveralls'
7
+
8
+ if Coveralls.should_run?
9
+ Coveralls.wear!
10
+ else
11
+ require 'simplecov'
12
+ SimpleCov.start
13
+ end
14
+
15
+ require 'bundler/setup'
16
+ Bundler.setup
17
+
18
+ require 'rack/test'
19
+ require 'database_cleaner'
20
+
21
+ ORM_GEMS_MAPPING = {
22
+ 'sequel' => 'sequel',
23
+ 'active_record' => 'active_record',
24
+ 'mongoid' => 'mongoid'
25
+ }.freeze
26
+
27
+ require ORM_GEMS_MAPPING[ENV['ORM']]
28
+
29
+ require 'grape_oauth2'
30
+
31
+ # Require Rack app by ORM
32
+ require File.expand_path("../dummy/orm/#{ENV['ORM']}/app/twitter", __FILE__)
33
+
34
+ TWITTER_APP = Rack::Builder.parse_file(File.expand_path("../dummy/orm/#{ENV['ORM']}/config.ru", __FILE__)).first
35
+
36
+ require 'support/api_helper'
37
+
38
+ RSpec.configure do |config|
39
+ config.include ApiHelper
40
+
41
+ config.filter_run_excluding skip_if: true
42
+
43
+ config.order = 'random'
44
+
45
+ config.before(:suite) do
46
+ if ENV['ORM'] == 'mongoid'
47
+ DatabaseCleaner[:mongoid].strategy = :truncation
48
+ DatabaseCleaner[:mongoid].clean_with :truncation
49
+ else
50
+ DatabaseCleaner.strategy = :transaction
51
+ DatabaseCleaner.clean_with(:deletion)
52
+ end
53
+ end
54
+
55
+ config.around(:example) do |example|
56
+ DatabaseCleaner.cleaning do
57
+ example.run
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,11 @@
1
+ module ApiHelper
2
+ include Rack::Test::Methods
3
+
4
+ def app
5
+ TWITTER_APP
6
+ end
7
+
8
+ def json_body
9
+ JSON.parse(last_response.body, symbolize_names: true) rescue fail StandardError, 'API request returned invalid json'
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,257 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape_oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nikita Bulai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.3.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.0
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 3.4.0
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.4.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 3.4.0
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.4.0
67
+ - !ruby/object:Gem::Dependency
68
+ name: database_cleaner
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: 1.5.0
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.5.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 1.5.0
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 1.5.0
87
+ description: Provides flexible, ORM-agnostic, fully customizable and simple OAuth2
88
+ support for Grape APIs
89
+ email:
90
+ - bulajnikita@gmail.com
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - ".rspec"
97
+ - ".rubocop.yml"
98
+ - ".travis.yml"
99
+ - Gemfile
100
+ - README.md
101
+ - Rakefile
102
+ - gemfiles/active_record.rb
103
+ - gemfiles/mongoid.rb
104
+ - gemfiles/sequel.rb
105
+ - grape_oauth2.gemspec
106
+ - grape_oauth2.png
107
+ - lib/grape_oauth2.rb
108
+ - lib/grape_oauth2/configuration.rb
109
+ - lib/grape_oauth2/configuration/class_accessors.rb
110
+ - lib/grape_oauth2/configuration/validation.rb
111
+ - lib/grape_oauth2/endpoints/authorize.rb
112
+ - lib/grape_oauth2/endpoints/token.rb
113
+ - lib/grape_oauth2/gem_version.rb
114
+ - lib/grape_oauth2/generators/authorization.rb
115
+ - lib/grape_oauth2/generators/base.rb
116
+ - lib/grape_oauth2/generators/token.rb
117
+ - lib/grape_oauth2/helpers/access_token_helpers.rb
118
+ - lib/grape_oauth2/helpers/oauth_params.rb
119
+ - lib/grape_oauth2/mixins/active_record/access_grant.rb
120
+ - lib/grape_oauth2/mixins/active_record/access_token.rb
121
+ - lib/grape_oauth2/mixins/active_record/client.rb
122
+ - lib/grape_oauth2/mixins/mongoid/access_grant.rb
123
+ - lib/grape_oauth2/mixins/mongoid/access_token.rb
124
+ - lib/grape_oauth2/mixins/mongoid/client.rb
125
+ - lib/grape_oauth2/mixins/sequel/access_grant.rb
126
+ - lib/grape_oauth2/mixins/sequel/access_token.rb
127
+ - lib/grape_oauth2/mixins/sequel/client.rb
128
+ - lib/grape_oauth2/responses/authorization.rb
129
+ - lib/grape_oauth2/responses/base.rb
130
+ - lib/grape_oauth2/responses/token.rb
131
+ - lib/grape_oauth2/scopes.rb
132
+ - lib/grape_oauth2/strategies/authorization_code.rb
133
+ - lib/grape_oauth2/strategies/base.rb
134
+ - lib/grape_oauth2/strategies/client_credentials.rb
135
+ - lib/grape_oauth2/strategies/password.rb
136
+ - lib/grape_oauth2/strategies/refresh_token.rb
137
+ - lib/grape_oauth2/unique_token.rb
138
+ - lib/grape_oauth2/version.rb
139
+ - spec/configuration/config_spec.rb
140
+ - spec/configuration/version_spec.rb
141
+ - spec/dummy/endpoints/custom_authorization.rb
142
+ - spec/dummy/endpoints/custom_token.rb
143
+ - spec/dummy/endpoints/status.rb
144
+ - spec/dummy/grape_oauth2_config.rb
145
+ - spec/dummy/orm/active_record/app/config/db.rb
146
+ - spec/dummy/orm/active_record/app/models/access_code.rb
147
+ - spec/dummy/orm/active_record/app/models/access_token.rb
148
+ - spec/dummy/orm/active_record/app/models/application.rb
149
+ - spec/dummy/orm/active_record/app/models/application_record.rb
150
+ - spec/dummy/orm/active_record/app/models/user.rb
151
+ - spec/dummy/orm/active_record/app/twitter.rb
152
+ - spec/dummy/orm/active_record/config.ru
153
+ - spec/dummy/orm/active_record/db/schema.rb
154
+ - spec/dummy/orm/mongoid/app/config/db.rb
155
+ - spec/dummy/orm/mongoid/app/config/mongoid.yml
156
+ - spec/dummy/orm/mongoid/app/models/access_code.rb
157
+ - spec/dummy/orm/mongoid/app/models/access_token.rb
158
+ - spec/dummy/orm/mongoid/app/models/application.rb
159
+ - spec/dummy/orm/mongoid/app/models/user.rb
160
+ - spec/dummy/orm/mongoid/app/twitter.rb
161
+ - spec/dummy/orm/mongoid/config.ru
162
+ - spec/dummy/orm/sequel/app/config/db.rb
163
+ - spec/dummy/orm/sequel/app/models/access_code.rb
164
+ - spec/dummy/orm/sequel/app/models/access_token.rb
165
+ - spec/dummy/orm/sequel/app/models/application.rb
166
+ - spec/dummy/orm/sequel/app/models/application_record.rb
167
+ - spec/dummy/orm/sequel/app/models/user.rb
168
+ - spec/dummy/orm/sequel/app/twitter.rb
169
+ - spec/dummy/orm/sequel/config.ru
170
+ - spec/dummy/orm/sequel/db/schema.rb
171
+ - spec/lib/scopes_spec.rb
172
+ - spec/mixins/active_record/access_token_spec.rb
173
+ - spec/mixins/active_record/client_spec.rb
174
+ - spec/mixins/mongoid/access_token_spec.rb
175
+ - spec/mixins/mongoid/client_spec.rb
176
+ - spec/mixins/sequel/access_token_spec.rb
177
+ - spec/mixins/sequel/client_spec.rb
178
+ - spec/requests/flows/authorization_code_spec.rb
179
+ - spec/requests/flows/client_credentials_spec.rb
180
+ - spec/requests/flows/password_spec.rb
181
+ - spec/requests/flows/refresh_token_spec.rb
182
+ - spec/requests/flows/revoke_token_spec.rb
183
+ - spec/requests/protected_resources_spec.rb
184
+ - spec/spec_helper.rb
185
+ - spec/support/api_helper.rb
186
+ homepage: http://github.com/nbulaj/grape-oauth2
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: 2.2.2
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubyforge_project:
206
+ rubygems_version: 2.6.3
207
+ signing_key:
208
+ specification_version: 4
209
+ summary: Grape OAuth2 provider
210
+ test_files:
211
+ - spec/support/api_helper.rb
212
+ - spec/configuration/version_spec.rb
213
+ - spec/configuration/config_spec.rb
214
+ - spec/dummy/grape_oauth2_config.rb
215
+ - spec/dummy/orm/sequel/app/twitter.rb
216
+ - spec/dummy/orm/sequel/app/models/user.rb
217
+ - spec/dummy/orm/sequel/app/models/application.rb
218
+ - spec/dummy/orm/sequel/app/models/access_code.rb
219
+ - spec/dummy/orm/sequel/app/models/access_token.rb
220
+ - spec/dummy/orm/sequel/app/models/application_record.rb
221
+ - spec/dummy/orm/sequel/app/config/db.rb
222
+ - spec/dummy/orm/sequel/config.ru
223
+ - spec/dummy/orm/sequel/db/schema.rb
224
+ - spec/dummy/orm/active_record/app/twitter.rb
225
+ - spec/dummy/orm/active_record/app/models/user.rb
226
+ - spec/dummy/orm/active_record/app/models/application.rb
227
+ - spec/dummy/orm/active_record/app/models/access_code.rb
228
+ - spec/dummy/orm/active_record/app/models/access_token.rb
229
+ - spec/dummy/orm/active_record/app/models/application_record.rb
230
+ - spec/dummy/orm/active_record/app/config/db.rb
231
+ - spec/dummy/orm/active_record/config.ru
232
+ - spec/dummy/orm/active_record/db/schema.rb
233
+ - spec/dummy/orm/mongoid/app/twitter.rb
234
+ - spec/dummy/orm/mongoid/app/models/user.rb
235
+ - spec/dummy/orm/mongoid/app/models/application.rb
236
+ - spec/dummy/orm/mongoid/app/models/access_code.rb
237
+ - spec/dummy/orm/mongoid/app/models/access_token.rb
238
+ - spec/dummy/orm/mongoid/app/config/mongoid.yml
239
+ - spec/dummy/orm/mongoid/app/config/db.rb
240
+ - spec/dummy/orm/mongoid/config.ru
241
+ - spec/dummy/endpoints/custom_token.rb
242
+ - spec/dummy/endpoints/status.rb
243
+ - spec/dummy/endpoints/custom_authorization.rb
244
+ - spec/mixins/sequel/client_spec.rb
245
+ - spec/mixins/sequel/access_token_spec.rb
246
+ - spec/mixins/active_record/client_spec.rb
247
+ - spec/mixins/active_record/access_token_spec.rb
248
+ - spec/mixins/mongoid/client_spec.rb
249
+ - spec/mixins/mongoid/access_token_spec.rb
250
+ - spec/requests/flows/client_credentials_spec.rb
251
+ - spec/requests/flows/authorization_code_spec.rb
252
+ - spec/requests/flows/password_spec.rb
253
+ - spec/requests/flows/revoke_token_spec.rb
254
+ - spec/requests/flows/refresh_token_spec.rb
255
+ - spec/requests/protected_resources_spec.rb
256
+ - spec/lib/scopes_spec.rb
257
+ - spec/spec_helper.rb