grape_oauth2 0.1.1 → 0.2.0

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.
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,10 +1,11 @@
1
- module Grape
2
- module OAuth2
3
- # Grape::OAuth2 responses namespace.
4
- module Responses
5
- # Authorization response.
6
- class Authorization < Base
7
- end
8
- end
9
- end
10
- end
1
+ module Grape
2
+ module OAuth2
3
+ # Grape::OAuth2 responses namespace.
4
+ module Responses
5
+ # Authorization response.
6
+ class Authorization < Base
7
+ # [IMPORTANT]: need to be implemented!
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,56 +1,56 @@
1
- module Grape
2
- module OAuth2
3
- # Grape::OAuth2 responses namespace.
4
- module Responses
5
- # Base class for Grape::OAuth2 endpoints responses.
6
- # Processes raw Rack Responses and contains helper methods.
7
- class Base
8
- # Raw Rack::Response to process
9
- #
10
- # @return [Array] Rack response
11
- #
12
- # @example
13
- # response = Grape::OAuth2::Responses::Base.new([200, {}, Rack::BodyProxy.new('Test')])
14
- # response.rack_response
15
- #
16
- # #=> [200, {}, Rack::BodyProxy.new('Test')]
17
- #
18
- attr_reader :rack_response
19
-
20
- # OAuth2 response class.
21
- #
22
- # @param rack_response [Array]
23
- # raw Rack::Response object
24
- #
25
- def initialize(rack_response)
26
- # Rack Body:
27
- # [Status Code, Headers, Body]
28
- @rack_response = rack_response
29
- end
30
-
31
- # Response status
32
- def status
33
- @rack_response[0]
34
- end
35
-
36
- # Response headers
37
- def headers
38
- @rack_response[1]
39
- end
40
-
41
- # Raw Rack body
42
- def raw_body
43
- @rack_response[2].body
44
- end
45
-
46
- # JSON-parsed body
47
- def body
48
- response_body = raw_body.first
49
- return {} if response_body.nil? || response_body.empty?
50
-
51
- JSON.parse(response_body)
52
- end
53
- end
54
- end
55
- end
56
- end
1
+ module Grape
2
+ module OAuth2
3
+ # Grape::OAuth2 responses namespace.
4
+ module Responses
5
+ # Base class for Grape::OAuth2 endpoints responses.
6
+ # Processes raw Rack Responses and contains helper methods.
7
+ class Base
8
+ # Raw Rack::Response to process
9
+ #
10
+ # @return [Array] Rack response
11
+ #
12
+ # @example
13
+ # response = Grape::OAuth2::Responses::Base.new([200, {}, Rack::BodyProxy.new('Test')])
14
+ # response.rack_response
15
+ #
16
+ # #=> [200, {}, Rack::BodyProxy.new('Test')]
17
+ #
18
+ attr_reader :rack_response
19
+
20
+ # OAuth2 response class.
21
+ #
22
+ # @param rack_response [Array]
23
+ # raw Rack::Response object
24
+ #
25
+ def initialize(rack_response)
26
+ # Rack Body:
27
+ # [Status Code, Headers, Body]
28
+ @rack_response = rack_response
29
+ end
30
+
31
+ # Response status
32
+ def status
33
+ @rack_response[0]
34
+ end
35
+
36
+ # Response headers
37
+ def headers
38
+ @rack_response[1]
39
+ end
40
+
41
+ # Raw Rack body
42
+ def raw_body
43
+ @rack_response[2].body
44
+ end
45
+
46
+ # JSON-parsed body
47
+ def body
48
+ response_body = raw_body.first
49
+ return {} if response_body.nil? || response_body.empty?
50
+
51
+ JSON.parse(response_body)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,10 +1,10 @@
1
- module Grape
2
- module OAuth2
3
- # Grape::OAuth2 responses namespace.
4
- module Responses
5
- # Token response.
6
- class Token < Base
7
- end
8
- end
9
- end
10
- end
1
+ module Grape
2
+ module OAuth2
3
+ # Grape::OAuth2 responses namespace.
4
+ module Responses
5
+ # Token response.
6
+ class Token < Base
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,74 +1,74 @@
1
- module Grape
2
- module OAuth2
3
- # OAuth2 helper for scopes validation
4
- # (between requested and presented in Access Token).
5
- class Scopes
6
- # Array of requested scopes
7
- #
8
- # @return [Array<String>] scopes
9
- #
10
- attr_reader :scopes
11
-
12
- # Helper class initializer.
13
- #
14
- # @param scopes [Array, String, #to_a]
15
- # array, string of any object that responds to `to_a`
16
- #
17
- def initialize(scopes)
18
- @scopes = to_array(scopes || [])
19
- end
20
-
21
- # Checks if requested scopes (passed and processed on initialization)
22
- # are presented in the Access Token.
23
- #
24
- # @param access_token [Object]
25
- # instance of the Access Token class that responds to `scopes`
26
- #
27
- # @return [Boolean]
28
- # true if requested scopes are empty or present in access token scopes
29
- # and false in other cases
30
- #
31
- def valid_for?(access_token)
32
- scopes.empty? || present_in?(access_token.scopes)
33
- end
34
-
35
- private
36
-
37
- # Checks if scopes present in Access Token scopes.
38
- #
39
- # @param token_scopes [Array, String, #to_a]
40
- # array, string of any object that responds to `to_a`
41
- #
42
- # @return [Boolean]
43
- # true if requested scopes present in Access Token and false in other cases
44
- #
45
- def present_in?(token_scopes)
46
- required_scopes = Set.new(to_array(scopes))
47
- authorized_scopes = Set.new(to_array(token_scopes))
48
-
49
- authorized_scopes >= required_scopes
50
- end
51
-
52
- # Converts scopes set to the array.
53
- #
54
- # @param scopes [Array, String, #to_a]
55
- # string, array or object that responds to `to_a`
56
- # @return [Array<String>]
57
- # array of scopes
58
- #
59
- def to_array(scopes)
60
- return [] if scopes.nil?
61
-
62
- collection = if scopes.is_a?(Array) || scopes.respond_to?(:to_a)
63
- scopes.to_a
64
- elsif scopes.is_a?(String)
65
- scopes.split
66
- else
67
- raise ArgumentError, 'scopes class is not supported!'
68
- end
69
-
70
- collection.map(&:to_s)
71
- end
72
- end
73
- end
74
- end
1
+ module Grape
2
+ module OAuth2
3
+ # OAuth2 helper for scopes validation
4
+ # (between requested and presented in Access Token).
5
+ class Scopes
6
+ # Array of requested scopes
7
+ #
8
+ # @return [Array<String>] scopes
9
+ #
10
+ attr_reader :scopes
11
+
12
+ # Helper class initializer.
13
+ #
14
+ # @param scopes [Array, String, #to_a]
15
+ # array, string of any object that responds to `to_a`
16
+ #
17
+ def initialize(scopes)
18
+ @scopes = to_array(scopes || [])
19
+ end
20
+
21
+ # Checks if requested scopes (passed and processed on initialization)
22
+ # are presented in the Access Token.
23
+ #
24
+ # @param access_token [Object]
25
+ # instance of the Access Token class that responds to `scopes`
26
+ #
27
+ # @return [Boolean]
28
+ # true if requested scopes are empty or present in access token scopes
29
+ # and false in other cases
30
+ #
31
+ def valid_for?(access_token)
32
+ scopes.empty? || present_in?(access_token.scopes)
33
+ end
34
+
35
+ private
36
+
37
+ # Checks if scopes present in Access Token scopes.
38
+ #
39
+ # @param token_scopes [Array, String, #to_a]
40
+ # array, string of any object that responds to `to_a`
41
+ #
42
+ # @return [Boolean]
43
+ # true if requested scopes present in Access Token and false in other cases
44
+ #
45
+ def present_in?(token_scopes)
46
+ required_scopes = Set.new(to_array(scopes))
47
+ authorized_scopes = Set.new(to_array(token_scopes))
48
+
49
+ authorized_scopes >= required_scopes
50
+ end
51
+
52
+ # Converts scopes set to the array.
53
+ #
54
+ # @param scopes [Array, String, #to_a]
55
+ # string, array or object that responds to `to_a`
56
+ # @return [Array<String>]
57
+ # array of scopes
58
+ #
59
+ def to_array(scopes)
60
+ return [] if scopes.nil?
61
+
62
+ collection = if scopes.is_a?(Array) || scopes.respond_to?(:to_a)
63
+ scopes.to_a
64
+ elsif scopes.is_a?(String)
65
+ scopes.split
66
+ else
67
+ raise ArgumentError, 'scopes class is not supported!'
68
+ end
69
+
70
+ collection.map(&:to_s)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,38 +1,38 @@
1
- module Grape
2
- module OAuth2
3
- module Strategies
4
- # Auth Code strategy class.
5
- # Processes request and responds with Token or Code
6
- # (depend on requested response type).
7
- class AuthorizationCode < Base
8
- class << self
9
- # Processes Authorization request.
10
- def process(request, response)
11
- client = authenticate_client(request)
12
- request.bad_request! if client.nil?
13
-
14
- response.redirect_uri = request.verify_redirect_uri!(client.redirect_uri)
15
-
16
- # TODO: verify scopes if they valid
17
- # scopes = request.scope
18
- # request.invalid_scope! "Unknown scope: #{scope}"
19
-
20
- case request.response_type
21
- when :code
22
- # resource owner can't be nil!
23
- authorization_code = config.access_grant_class.create_for(client, nil, response.redirect_uri)
24
- response.code = authorization_code.token
25
- when :token
26
- # resource owner can't be nil!
27
- access_token = config.access_token_class.create_for(client, nil, scopes_from(request))
28
- response.access_token = expose_to_bearer_token(access_token)
29
- end
30
-
31
- response.approve!
32
- response
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
1
+ module Grape
2
+ module OAuth2
3
+ module Strategies
4
+ # Auth Code strategy class.
5
+ # Processes request and responds with Token or Code
6
+ # (depend on requested response type).
7
+ class AuthorizationCode < Base
8
+ class << self
9
+ # Processes Authorization request.
10
+ def process(request, response)
11
+ client = authenticate_client(request)
12
+ request.bad_request! if client.nil?
13
+
14
+ response.redirect_uri = request.verify_redirect_uri!(client.redirect_uri)
15
+
16
+ # TODO: verify scopes if they valid
17
+ # scopes = request.scope
18
+ # request.invalid_scope! "Unknown scope: #{scope}"
19
+
20
+ case request.response_type
21
+ when :code
22
+ # resource owner can't be nil!
23
+ authorization_code = config.access_grant_class.create_for(client, nil, response.redirect_uri)
24
+ response.code = authorization_code.token
25
+ when :token
26
+ # resource owner can't be nil!
27
+ access_token = config.access_token_class.create_for(client, nil, scopes_from(request))
28
+ response.access_token = expose_to_bearer_token(access_token)
29
+ end
30
+
31
+ response.approve!
32
+ response
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,47 +1,47 @@
1
- module Grape
2
- module OAuth2
3
- # Grape::OAuth2 strategies namespace
4
- module Strategies
5
- # Base Grape::OAuth2 Strategies class .
6
- # Contains common functionality for all the descendants.
7
- class Base
8
- class << self
9
- # Authenticates Client from the request.
10
- def authenticate_client(request)
11
- config.client_class.authenticate(request.client_id, request.try(:client_secret))
12
- end
13
-
14
- # Authenticates Resource Owner from the request.
15
- def authenticate_resource_owner(client, request)
16
- config.resource_owner_class.oauth_authenticate(client, request.username, request.password)
17
- end
18
-
19
- # Short getter for Grape::OAuth2 configuration
20
- def config
21
- Grape::OAuth2.config
22
- end
23
-
24
- # Converts scopes from the request string. Separate them by the whitespace.
25
- # @return [String] scopes string
26
- #
27
- def scopes_from(request)
28
- return nil if request.scope.nil?
29
-
30
- Array(request.scope).join(' ')
31
- end
32
-
33
- # Exposes token object to Bearer token.
34
- #
35
- # @param token [#to_bearer_token]
36
- # any object that responds to `to_bearer_token`
37
- # @return [Rack::OAuth2::AccessToken::Bearer]
38
- # bearer token instance
39
- #
40
- def expose_to_bearer_token(token)
41
- Rack::OAuth2::AccessToken::Bearer.new(token.to_bearer_token)
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
1
+ module Grape
2
+ module OAuth2
3
+ # Grape::OAuth2 strategies namespace
4
+ module Strategies
5
+ # Base Grape::OAuth2 Strategies class .
6
+ # Contains common functionality for all the descendants.
7
+ class Base
8
+ class << self
9
+ # Authenticates Client from the request.
10
+ def authenticate_client(request)
11
+ config.client_class.authenticate(request.client_id, request.try(:client_secret))
12
+ end
13
+
14
+ # Authenticates Resource Owner from the request.
15
+ def authenticate_resource_owner(client, request)
16
+ config.resource_owner_class.oauth_authenticate(client, request.username, request.password)
17
+ end
18
+
19
+ # Short getter for Grape::OAuth2 configuration
20
+ def config
21
+ Grape::OAuth2.config
22
+ end
23
+
24
+ # Converts scopes from the request string. Separate them by the whitespace.
25
+ # @return [String] scopes string
26
+ #
27
+ def scopes_from(request)
28
+ return nil if request.scope.nil?
29
+
30
+ Array(request.scope).join(' ')
31
+ end
32
+
33
+ # Exposes token object to Bearer token.
34
+ #
35
+ # @param token [#to_bearer_token]
36
+ # any object that responds to `to_bearer_token`
37
+ # @return [Rack::OAuth2::AccessToken::Bearer]
38
+ # bearer token instance
39
+ #
40
+ def expose_to_bearer_token(token)
41
+ Rack::OAuth2::AccessToken::Bearer.new(token.to_bearer_token)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end