grape_oauth2 0.1.1 → 0.2.0

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 +4 -4
  2. data/.gitignore +11 -11
  3. data/Gemfile +23 -23
  4. data/Rakefile +11 -11
  5. data/grape_oauth2.gemspec +26 -27
  6. data/lib/grape_oauth2.rb +129 -129
  7. data/lib/grape_oauth2/configuration.rb +143 -143
  8. data/lib/grape_oauth2/configuration/class_accessors.rb +36 -36
  9. data/lib/grape_oauth2/configuration/validation.rb +71 -71
  10. data/lib/grape_oauth2/endpoints/authorize.rb +34 -34
  11. data/lib/grape_oauth2/endpoints/token.rb +72 -72
  12. data/lib/grape_oauth2/gem_version.rb +24 -24
  13. data/lib/grape_oauth2/generators/authorization.rb +44 -44
  14. data/lib/grape_oauth2/generators/base.rb +26 -26
  15. data/lib/grape_oauth2/generators/token.rb +62 -62
  16. data/lib/grape_oauth2/helpers/access_token_helpers.rb +52 -54
  17. data/lib/grape_oauth2/helpers/oauth_params.rb +41 -41
  18. data/lib/grape_oauth2/mixins/active_record/access_grant.rb +47 -47
  19. data/lib/grape_oauth2/mixins/active_record/access_token.rb +75 -75
  20. data/lib/grape_oauth2/mixins/active_record/client.rb +36 -35
  21. data/lib/grape_oauth2/mixins/mongoid/access_grant.rb +58 -58
  22. data/lib/grape_oauth2/mixins/mongoid/access_token.rb +88 -88
  23. data/lib/grape_oauth2/mixins/mongoid/client.rb +44 -41
  24. data/lib/grape_oauth2/mixins/sequel/access_grant.rb +68 -68
  25. data/lib/grape_oauth2/mixins/sequel/access_token.rb +86 -86
  26. data/lib/grape_oauth2/mixins/sequel/client.rb +54 -46
  27. data/lib/grape_oauth2/responses/authorization.rb +11 -10
  28. data/lib/grape_oauth2/responses/base.rb +56 -56
  29. data/lib/grape_oauth2/responses/token.rb +10 -10
  30. data/lib/grape_oauth2/scopes.rb +74 -74
  31. data/lib/grape_oauth2/strategies/authorization_code.rb +38 -38
  32. data/lib/grape_oauth2/strategies/base.rb +47 -47
  33. data/lib/grape_oauth2/strategies/client_credentials.rb +20 -20
  34. data/lib/grape_oauth2/strategies/password.rb +22 -22
  35. data/lib/grape_oauth2/strategies/refresh_token.rb +47 -47
  36. data/lib/grape_oauth2/unique_token.rb +20 -20
  37. data/lib/grape_oauth2/version.rb +14 -14
  38. data/spec/configuration/config_spec.rb +231 -231
  39. data/spec/configuration/version_spec.rb +12 -12
  40. data/spec/dummy/endpoints/custom_authorization.rb +25 -25
  41. data/spec/dummy/endpoints/custom_token.rb +35 -35
  42. data/spec/dummy/endpoints/status.rb +25 -25
  43. data/spec/dummy/grape_oauth2_config.rb +11 -11
  44. data/spec/dummy/orm/active_record/app/config/db.rb +7 -7
  45. data/spec/dummy/orm/active_record/app/models/access_code.rb +3 -3
  46. data/spec/dummy/orm/active_record/app/models/access_token.rb +3 -3
  47. data/spec/dummy/orm/active_record/app/models/application.rb +3 -3
  48. data/spec/dummy/orm/active_record/app/models/application_record.rb +3 -3
  49. data/spec/dummy/orm/active_record/app/models/user.rb +10 -10
  50. data/spec/dummy/orm/active_record/app/twitter.rb +36 -36
  51. data/spec/dummy/orm/active_record/config.ru +7 -7
  52. data/spec/dummy/orm/active_record/db/schema.rb +53 -53
  53. data/spec/dummy/orm/mongoid/app/config/db.rb +6 -6
  54. data/spec/dummy/orm/mongoid/app/config/mongoid.yml +21 -21
  55. data/spec/dummy/orm/mongoid/app/models/access_code.rb +3 -3
  56. data/spec/dummy/orm/mongoid/app/models/access_token.rb +3 -3
  57. data/spec/dummy/orm/mongoid/app/models/application.rb +3 -3
  58. data/spec/dummy/orm/mongoid/app/models/user.rb +11 -11
  59. data/spec/dummy/orm/mongoid/app/twitter.rb +34 -34
  60. data/spec/dummy/orm/mongoid/config.ru +5 -5
  61. data/spec/dummy/orm/sequel/app/config/db.rb +1 -1
  62. data/spec/dummy/orm/sequel/app/models/access_code.rb +4 -4
  63. data/spec/dummy/orm/sequel/app/models/access_token.rb +4 -4
  64. data/spec/dummy/orm/sequel/app/models/application.rb +4 -4
  65. data/spec/dummy/orm/sequel/app/models/application_record.rb +2 -2
  66. data/spec/dummy/orm/sequel/app/models/user.rb +11 -11
  67. data/spec/dummy/orm/sequel/app/twitter.rb +47 -47
  68. data/spec/dummy/orm/sequel/config.ru +5 -5
  69. data/spec/dummy/orm/sequel/db/schema.rb +50 -50
  70. data/spec/lib/scopes_spec.rb +50 -50
  71. data/spec/mixins/active_record/access_token_spec.rb +185 -185
  72. data/spec/mixins/active_record/client_spec.rb +104 -95
  73. data/spec/mixins/mongoid/access_token_spec.rb +185 -185
  74. data/spec/mixins/mongoid/client_spec.rb +104 -95
  75. data/spec/mixins/sequel/access_token_spec.rb +185 -185
  76. data/spec/mixins/sequel/client_spec.rb +105 -96
  77. data/spec/requests/flows/authorization_code_spec.rb +67 -67
  78. data/spec/requests/flows/client_credentials_spec.rb +101 -101
  79. data/spec/requests/flows/password_spec.rb +210 -210
  80. data/spec/requests/flows/refresh_token_spec.rb +222 -222
  81. data/spec/requests/flows/revoke_token_spec.rb +103 -103
  82. data/spec/requests/protected_resources_spec.rb +64 -64
  83. data/spec/spec_helper.rb +60 -60
  84. data/spec/support/api_helper.rb +11 -11
  85. metadata +50 -52
  86. data/.rspec +0 -2
  87. data/.rubocop.yml +0 -18
  88. data/.travis.yml +0 -42
  89. data/README.md +0 -820
  90. data/gemfiles/active_record.rb +0 -25
  91. data/gemfiles/mongoid.rb +0 -14
  92. data/gemfiles/sequel.rb +0 -24
  93. data/grape_oauth2.png +0 -0
@@ -1,44 +1,44 @@
1
- module Grape
2
- module OAuth2
3
- module Generators
4
- # OAuth2 Authorization generator class.
5
- # Processes the request and builds the response.
6
- class Authorization < Base
7
- class << self
8
- # Generates Authorization Response based on the request.
9
- #
10
- # @return [Grape::OAuth2::Responses::Authorization] response
11
- #
12
- def generate_for(env, &_block)
13
- authorization = Rack::OAuth2::Server::Authorize.new do |request, response|
14
- if block_given?
15
- yield request, response
16
- else
17
- execute_default(request, response)
18
- end
19
- end
20
-
21
- Grape::OAuth2::Responses::Authorization.new(authorization.call(env))
22
- rescue Rack::OAuth2::Server::Authorize::BadRequest => error
23
- error_response(error)
24
- end
25
-
26
- private
27
-
28
- def error_response(error)
29
- response = Rack::Response.new
30
- response.status = error.status
31
- response.header['Content-Type'] = 'application/json'
32
- response.write(JSON.dump(Rack::OAuth2::Util.compact_hash(error.protocol_params)))
33
-
34
- Grape::OAuth2::Responses::Authorization.new(response.finish)
35
- end
36
-
37
- def execute_default(request, response)
38
- Grape::OAuth2::Strategies::AuthorizationCode.process(request, response)
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
1
+ module Grape
2
+ module OAuth2
3
+ module Generators
4
+ # OAuth2 Authorization generator class.
5
+ # Processes the request and builds the response.
6
+ class Authorization < Base
7
+ class << self
8
+ # Generates Authorization Response based on the request.
9
+ #
10
+ # @return [Grape::OAuth2::Responses::Authorization] response
11
+ #
12
+ def generate_for(env, &_block)
13
+ authorization = Rack::OAuth2::Server::Authorize.new do |request, response|
14
+ if block_given?
15
+ yield request, response
16
+ else
17
+ execute_default(request, response)
18
+ end
19
+ end
20
+
21
+ Grape::OAuth2::Responses::Authorization.new(authorization.call(env))
22
+ rescue Rack::OAuth2::Server::Authorize::BadRequest => error
23
+ error_response(error)
24
+ end
25
+
26
+ private
27
+
28
+ def error_response(error)
29
+ response = Rack::Response.new
30
+ response.status = error.status
31
+ response.header['Content-Type'] = 'application/json'
32
+ response.write(JSON.dump(Rack::OAuth2::Util.compact_hash(error.protocol_params)))
33
+
34
+ Grape::OAuth2::Responses::Authorization.new(response.finish)
35
+ end
36
+
37
+ def execute_default(request, response)
38
+ Grape::OAuth2::Strategies::AuthorizationCode.process(request, response)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,26 +1,26 @@
1
- module Grape
2
- module OAuth2
3
- module Generators
4
- # Base class for Grape::OAuth2 generators.
5
- # Grape::OAuth2 generators processes the requests and
6
- # generates responses with Access Token or Authorization Code.
7
- class Base
8
- class << self
9
- # Allowed grant types from the Grape::OAuth2 configuration.
10
- #
11
- # @return [Array]
12
- # allowed grant types
13
- #
14
- def allowed_grants
15
- config.allowed_grant_types
16
- end
17
-
18
- # Short getter for Grape::OAuth2 configuration.
19
- def config
20
- Grape::OAuth2.config
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
1
+ module Grape
2
+ module OAuth2
3
+ module Generators
4
+ # Base class for Grape::OAuth2 generators.
5
+ # Grape::OAuth2 generators processes the requests and
6
+ # generates responses with Access Token or Authorization Code.
7
+ class Base
8
+ class << self
9
+ # Allowed grant types from the Grape::OAuth2 configuration.
10
+ #
11
+ # @return [Array]
12
+ # allowed grant types
13
+ #
14
+ def allowed_grants
15
+ config.allowed_grant_types
16
+ end
17
+
18
+ # Short getter for Grape::OAuth2 configuration.
19
+ def config
20
+ Grape::OAuth2.config
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,62 +1,62 @@
1
- module Grape
2
- module OAuth2
3
- module Generators
4
- # OAuth2 Token generator class.
5
- # Processes the request by required Grant Type and builds the response.
6
- class Token < Base
7
- # Grant type => OAuth2 strategy class
8
- STRATEGY_CLASSES = {
9
- password: Grape::OAuth2::Strategies::Password,
10
- client_credentials: Grape::OAuth2::Strategies::ClientCredentials,
11
- refresh_token: Grape::OAuth2::Strategies::RefreshToken
12
- }.freeze
13
-
14
- class << self
15
- # Generates Token Response based on the request.
16
- #
17
- # @return [Grape::OAuth2::Responses::Token] response
18
- #
19
- def generate_for(env, &_block)
20
- token = Rack::OAuth2::Server::Token.new do |request, response|
21
- request.unsupported_grant_type! unless allowed_grants.include?(request.grant_type.to_s)
22
-
23
- if block_given?
24
- yield request, response
25
- else
26
- execute_default(request, response)
27
- end
28
- end
29
-
30
- Grape::OAuth2::Responses::Token.new(token.call(env))
31
- end
32
-
33
- protected
34
-
35
- # Runs default Grape::OAuth2 functionality for Token endpoint.
36
- # In common it authenticates client (or/and any other objects) and
37
- # grants the Access Token or Auth Code.
38
- #
39
- # @param request [Rack::Request] request object
40
- # @param response [Rack::Response] response object
41
- #
42
- def execute_default(request, response)
43
- strategy = find_strategy(request.grant_type) || request.invalid_grant!
44
- response.access_token = strategy.process(request)
45
- end
46
-
47
- # Returns Grape::OAuth2 strategy class by Grant Type.
48
- #
49
- # @param grant_type [Symbol]
50
- # grant type value
51
- #
52
- # @return [Password, ClientCredentials, RefreshToken]
53
- # strategy class
54
- #
55
- def find_strategy(grant_type)
56
- STRATEGY_CLASSES[grant_type]
57
- end
58
- end
59
- end
60
- end
61
- end
62
- end
1
+ module Grape
2
+ module OAuth2
3
+ module Generators
4
+ # OAuth2 Token generator class.
5
+ # Processes the request by required Grant Type and builds the response.
6
+ class Token < Base
7
+ # Grant type => OAuth2 strategy class
8
+ STRATEGY_CLASSES = {
9
+ password: Grape::OAuth2::Strategies::Password,
10
+ client_credentials: Grape::OAuth2::Strategies::ClientCredentials,
11
+ refresh_token: Grape::OAuth2::Strategies::RefreshToken
12
+ }.freeze
13
+
14
+ class << self
15
+ # Generates Token Response based on the request.
16
+ #
17
+ # @return [Grape::OAuth2::Responses::Token] response
18
+ #
19
+ def generate_for(env, &_block)
20
+ token = Rack::OAuth2::Server::Token.new do |request, response|
21
+ request.unsupported_grant_type! unless allowed_grants.include?(request.grant_type.to_s)
22
+
23
+ if block_given?
24
+ yield request, response
25
+ else
26
+ execute_default(request, response)
27
+ end
28
+ end
29
+
30
+ Grape::OAuth2::Responses::Token.new(token.call(env))
31
+ end
32
+
33
+ protected
34
+
35
+ # Runs default Grape::OAuth2 functionality for Token endpoint.
36
+ # In common it authenticates client (or/and any other objects) and
37
+ # grants the Access Token or Auth Code.
38
+ #
39
+ # @param request [Rack::Request] request object
40
+ # @param response [Rack::Response] response object
41
+ #
42
+ def execute_default(request, response)
43
+ strategy = find_strategy(request.grant_type) || request.invalid_grant!
44
+ response.access_token = strategy.process(request)
45
+ end
46
+
47
+ # Returns Grape::OAuth2 strategy class by Grant Type.
48
+ #
49
+ # @param grant_type [Symbol]
50
+ # grant type value
51
+ #
52
+ # @return [Password, ClientCredentials, RefreshToken]
53
+ # strategy class
54
+ #
55
+ def find_strategy(grant_type)
56
+ STRATEGY_CLASSES[grant_type]
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,54 +1,52 @@
1
- module Grape
2
- module OAuth2
3
- module Helpers
4
- # Set of Grape OAuth2 helpers.
5
- module AccessTokenHelpers
6
- extend ::Grape::API::Helpers
7
-
8
- # Adds OAuth2 Access Token protection for Grape routes.
9
- #
10
- # @param scopes [Array]
11
- # set of scopes required to access the endpoint
12
- #
13
- # @raise [Rack::OAuth2::Server::Resource::Bearer::Unauthorized]
14
- # invalid Access Token value
15
- # @raise [Rack::OAuth2::Server::Resource::Bearer::Forbidden]
16
- # Access Token expired, revoked or does't have required scopes
17
- #
18
- def access_token_required!(*scopes)
19
- endpoint_scopes = env['api.endpoint'].options[:route_options][:scopes]
20
- required_scopes = endpoint_scopes.presence || scopes
21
-
22
- raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized if current_access_token.nil?
23
- raise Rack::OAuth2::Server::Resource::Bearer::Forbidden unless valid_access_token?(required_scopes)
24
- end
25
-
26
- # Returns Resource Owner from the Access Token
27
- # found by access_token value passed with the request.
28
- def current_resource_owner
29
- @_current_resource_owner ||= current_access_token.resource_owner
30
- end
31
-
32
- # Returns Access Token instance found by
33
- # access_token value passed with the request.
34
- def current_access_token
35
- @_current_access_token ||= request.env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
36
- end
37
-
38
- private
39
-
40
- # Validate current access token not to be expired or revoked
41
- # and has all the requested scopes.
42
- #
43
- # @return [Boolean]
44
- # true if current Access Token not expired, not revoked and scopes match
45
- # false in other cases.
46
- #
47
- def valid_access_token?(scopes)
48
- !current_access_token.revoked? && !current_access_token.expired? &&
49
- Grape::OAuth2.config.scopes_validator.new(scopes).valid_for?(current_access_token)
50
- end
51
- end
52
- end
53
- end
54
- end
1
+ module Grape
2
+ module OAuth2
3
+ module Helpers
4
+ # Set of Grape OAuth2 helpers.
5
+ module AccessTokenHelpers
6
+ extend ::Grape::API::Helpers
7
+
8
+ # Adds OAuth2 Access Token protection for Grape routes.
9
+ #
10
+ # @param scopes [Array]
11
+ # set of scopes required to access the endpoint
12
+ #
13
+ # @raise [Rack::OAuth2::Server::Resource::Bearer::Unauthorized]
14
+ # invalid Access Token value
15
+ # @raise [Rack::OAuth2::Server::Resource::Bearer::Forbidden]
16
+ # Access Token expired, revoked or does't have required scopes
17
+ #
18
+ def access_token_required!(*scopes)
19
+ endpoint_scopes = env['api.endpoint'].options[:route_options][:scopes]
20
+ required_scopes = endpoint_scopes.presence || scopes
21
+
22
+ raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized if current_access_token.nil?
23
+ raise Rack::OAuth2::Server::Resource::Bearer::Forbidden unless valid_access_token?(required_scopes)
24
+ end
25
+
26
+ # Returns Resource Owner from the Access Token
27
+ # found by access_token value passed with the request.
28
+ def current_resource_owner
29
+ @_current_resource_owner ||= current_access_token.resource_owner
30
+ end
31
+
32
+ # Returns Access Token instance found by
33
+ # access_token value passed with the request.
34
+ def current_access_token
35
+ @_current_access_token ||= request.env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
36
+ end
37
+
38
+ # Validate current access token not to be expired or revoked
39
+ # and has all the requested scopes.
40
+ #
41
+ # @return [Boolean]
42
+ # true if current Access Token not expired, not revoked and scopes match
43
+ # false in other cases.
44
+ #
45
+ def valid_access_token?(scopes)
46
+ !current_access_token.revoked? && !current_access_token.expired? &&
47
+ Grape::OAuth2.config.scopes_validator.new(scopes).valid_for?(current_access_token)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,41 +1,41 @@
1
- module Grape
2
- module OAuth2
3
- module Helpers
4
- # Grape Helper object for OAuth2 requests params.
5
- # Used fin default Grape::OAuth2 gem endpoints and can be used
6
- # for custom one.
7
- module OAuthParams
8
- extend ::Grape::API::Helpers
9
-
10
- # Params are optional in order to process them correctly in accordance
11
- # with the RFC 6749 (invalid_client, unsupported_grant_type, etc.)
12
- params :oauth_token_params do
13
- optional :grant_type, type: String, desc: 'Grant type'
14
- optional :client_id, type: String, desc: 'Client ID'
15
- optional :client_secret, type: String, desc: 'Client secret'
16
- optional :refresh_token, type: String, desc: 'Refresh Token'
17
- end
18
-
19
- # Params for authorization request.
20
- # @see https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-4.1.1 Authorization Request
21
- params :oauth_authorization_params do
22
- optional :response_type, type: String, desc: 'Response type'
23
- optional :client_id, type: String, desc: 'Client ID'
24
- optional :redirect_uri, type: String, desc: 'Redirect URI'
25
- optional :scope, type: String, desc: 'Authorization scopes'
26
- optional :state, type: String, desc: 'State'
27
- end
28
-
29
- # Params for token revocation.
30
- # @see https://tools.ietf.org/html/rfc7009#section-2.1 OAuth 2.0 Token Revocation
31
- params :oauth_token_revocation_params do
32
- requires :token, type: String, desc: 'The token that the client wants to get revoked'
33
- optional :token_type_hint, type: String,
34
- values: %w(access_token refresh_token),
35
- default: 'access_token',
36
- desc: 'A hint about the type of the token submitted for revocation'
37
- end
38
- end
39
- end
40
- end
41
- end
1
+ module Grape
2
+ module OAuth2
3
+ module Helpers
4
+ # Grape Helper object for OAuth2 requests params.
5
+ # Used fin default Grape::OAuth2 gem endpoints and can be used
6
+ # for custom one.
7
+ module OAuthParams
8
+ extend ::Grape::API::Helpers
9
+
10
+ # Params are optional in order to process them correctly in accordance
11
+ # with the RFC 6749 (invalid_client, unsupported_grant_type, etc.)
12
+ params :oauth_token_params do
13
+ optional :grant_type, type: String, desc: 'Grant type'
14
+ optional :client_id, type: String, desc: 'Client ID'
15
+ optional :client_secret, type: String, desc: 'Client secret'
16
+ optional :refresh_token, type: String, desc: 'Refresh Token'
17
+ end
18
+
19
+ # Params for authorization request.
20
+ # @see https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-4.1.1 Authorization Request
21
+ params :oauth_authorization_params do
22
+ optional :response_type, type: String, desc: 'Response type'
23
+ optional :client_id, type: String, desc: 'Client ID'
24
+ optional :redirect_uri, type: String, desc: 'Redirect URI'
25
+ optional :scope, type: String, desc: 'Authorization scopes'
26
+ optional :state, type: String, desc: 'State'
27
+ end
28
+
29
+ # Params for token revocation.
30
+ # @see https://tools.ietf.org/html/rfc7009#section-2.1 OAuth 2.0 Token Revocation
31
+ params :oauth_token_revocation_params do
32
+ requires :token, type: String, desc: 'The token that the client wants to get revoked'
33
+ optional :token_type_hint, type: String,
34
+ values: %w[access_token refresh_token],
35
+ default: 'access_token',
36
+ desc: 'A hint about the type of the token submitted for revocation'
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end