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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5684304c79fc0d0110edf0f0cfccf74f965f120
4
- data.tar.gz: 20ed4ecda0c0406ade366594abd43d89b26f79fd
3
+ metadata.gz: f8be2fa7c44c5993c1517344c6ececa9057a1977
4
+ data.tar.gz: b84d3fd751304e035128c215da6f028092c3298f
5
5
  SHA512:
6
- metadata.gz: 8720ebaef81e37b3b9160e434f10823ebf10a1959718048cd3c80cbe125d0ddea1c5ef78632fa899317634bdd07ed15ac81fe67d32f9d58a2a963253e3ebb6c2
7
- data.tar.gz: 0ce5d8636c5c4b5920b879de0fd353c740d6bd2b2fc73cd5634b351ebc4eddb75a4b6b9d63295e18b8d34a037345ba43e7a053dc172fd866c6cad6cdca9b52e4
6
+ metadata.gz: 61b111e324a6e707c84ba24fcdf9d9a867b57ae88954b66fcb0950880ce69fc43606d0cf76a45c1160e386e73c8c78fadf126b7a130afb02f2697870cfeced24
7
+ data.tar.gz: 0063cecbf7445cb61fbb58bb84efb21fe7b9b05cb868916333c4b0e7ea442898ba1e2fd6161abaf770d28c366714f63305a20bd660675013441ce88a1c7daba1
data/.gitignore CHANGED
@@ -1,11 +1,11 @@
1
- .bundle/
2
- .rbx
3
- *.rbc
4
- log/*.log
5
- .rvmrc
6
- /.idea
7
- gemfiles/*.lock
8
- Gemfile.lock
9
- coverage/
10
- .yardoc/
11
- doc/
1
+ .bundle/
2
+ .rbx
3
+ *.rbc
4
+ log/*.log
5
+ .rvmrc
6
+ /.idea
7
+ gemfiles/*.lock
8
+ Gemfile.lock
9
+ coverage/
10
+ .yardoc/
11
+ doc/
data/Gemfile CHANGED
@@ -1,23 +1,23 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'grape', '~> 0.16'
6
- gem 'rack-oauth2'
7
-
8
- gem 'activerecord'
9
- gem 'bcrypt'
10
-
11
- group :test do
12
- platforms :ruby, :mswin, :mswin64, :mingw, :x64_mingw do
13
- gem 'sqlite3'
14
- end
15
-
16
- gem 'rspec-rails', '~> 3.5'
17
- gem 'coveralls', require: false
18
- gem 'database_cleaner'
19
- gem 'rack-test', require: 'rack/test'
20
- gem 'otr-activerecord'
21
- end
22
-
23
- gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'grape', '~> 1.0'
6
+ gem 'rack-oauth2'
7
+
8
+ gem 'activerecord'
9
+ gem 'bcrypt'
10
+
11
+ group :test do
12
+ platforms :ruby, :mswin, :mswin64, :mingw, :x64_mingw do
13
+ gem 'sqlite3'
14
+ end
15
+
16
+ gem 'coveralls', require: false
17
+ gem 'database_cleaner'
18
+ gem 'otr-activerecord'
19
+ gem 'rack-test', require: 'rack/test'
20
+ gem 'rspec-rails', '~> 3.5'
21
+ end
22
+
23
+ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
- require 'bundler/setup'
2
- require 'rspec/core/rake_task'
3
-
4
- desc 'Default: run specs.'
5
- task default: :spec
6
-
7
- RSpec::Core::RakeTask.new(:spec) do |config|
8
- config.verbose = false
9
- end
10
-
11
- Bundler::GemHelper.install_tasks
1
+ require 'bundler/setup'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run specs.'
5
+ task default: :spec
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |config|
8
+ config.verbose = false
9
+ end
10
+
11
+ Bundler::GemHelper.install_tasks
@@ -1,27 +1,26 @@
1
- $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
-
3
- require 'grape_oauth2/version'
4
-
5
- Gem::Specification.new do |gem|
6
- gem.name = 'grape_oauth2'
7
- gem.version = Grape::OAuth2.gem_version
8
- gem.authors = ['Nikita Bulai']
9
- gem.date = '2016-05-31'
10
- gem.email = ['bulajnikita@gmail.com']
11
- gem.homepage = 'http://github.com/nbulaj/grape-oauth2'
12
- gem.summary = 'Grape OAuth2 provider'
13
- gem.description = 'Provides flexible, ORM-agnostic, fully customizable and simple OAuth2 support for Grape APIs'
14
- gem.license = 'MIT'
15
-
16
- gem.require_paths = %w(lib)
17
- gem.files = `git ls-files`.split($RS)
18
- gem.test_files = Dir['spec/**/*']
19
-
20
- gem.required_ruby_version = '>= 2.2.2'
21
-
22
- gem.add_runtime_dependency 'grape', '~> 0.16'
23
- gem.add_runtime_dependency 'rack-oauth2', '~> 1.3.0', '>= 1.3.0'
24
-
25
- gem.add_development_dependency 'rspec-rails', '~> 3.4.0', '>= 3.4.0'
26
- gem.add_development_dependency 'database_cleaner', '~> 1.5.0', '>= 1.5.0'
27
- end
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'grape_oauth2/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'grape_oauth2'
7
+ gem.version = Grape::OAuth2.gem_version
8
+ gem.authors = ['Nikita Bulai']
9
+ gem.email = ['bulajnikita@gmail.com']
10
+ gem.homepage = 'http://github.com/nbulaj/grape-oauth2'
11
+ gem.summary = 'Grape OAuth2 provider'
12
+ gem.description = 'Flexible, ORM-agnostic, fully customizable and simple OAuth2 provider support for Grape APIs'
13
+ gem.license = 'MIT'
14
+
15
+ gem.require_paths = %w[lib]
16
+ gem.files = `git ls-files`.split($RS).reject { |f| f.include?('gemfiles') } - ['README.md', 'grape_oauth2.png', '.rspec', '.travis.yml', '.rubocop.yml']
17
+ gem.test_files = Dir['spec/**/*']
18
+
19
+ gem.required_ruby_version = '>= 2.2.2'
20
+
21
+ gem.add_runtime_dependency 'grape', '~> 1.0', '>= 1.0'
22
+ gem.add_runtime_dependency 'rack-oauth2', '~> 1.6.0', '>= 1.6.0'
23
+
24
+ gem.add_development_dependency 'rspec-rails', '~> 3.6.0', '>= 3.6.0'
25
+ gem.add_development_dependency 'database_cleaner', '~> 1.5.0', '>= 1.5.0'
26
+ end
@@ -1,129 +1,129 @@
1
- require 'grape'
2
- require 'rack/oauth2'
3
-
4
- require 'grape_oauth2/version'
5
- require 'grape_oauth2/configuration/validation'
6
- require 'grape_oauth2/configuration/class_accessors'
7
- require 'grape_oauth2/configuration'
8
- require 'grape_oauth2/scopes'
9
- require 'grape_oauth2/unique_token'
10
-
11
- # NOTE: Extract to separate gems!!!
12
- # This gem should contains only the core functionality and all mixins
13
- # need to be moved to their own repos with their own tests.
14
-
15
- # Mixins
16
- if defined?(ActiveRecord::Base)
17
- require 'grape_oauth2/mixins/active_record/access_token'
18
- require 'grape_oauth2/mixins/active_record/access_grant'
19
- require 'grape_oauth2/mixins/active_record/client'
20
- end
21
-
22
- if defined?(Sequel::Model)
23
- require 'grape_oauth2/mixins/sequel/access_token'
24
- require 'grape_oauth2/mixins/sequel/access_grant'
25
- require 'grape_oauth2/mixins/sequel/client'
26
- end
27
-
28
- if defined?(Mongoid::Document)
29
- require 'grape_oauth2/mixins/mongoid/access_token'
30
- require 'grape_oauth2/mixins/mongoid/access_grant'
31
- require 'grape_oauth2/mixins/mongoid/client'
32
- end
33
-
34
- # Authorization Grants aka Flows (Strategies)
35
- require 'grape_oauth2/strategies/base'
36
- require 'grape_oauth2/strategies/authorization_code'
37
- require 'grape_oauth2/strategies/password'
38
- require 'grape_oauth2/strategies/client_credentials'
39
- require 'grape_oauth2/strategies/refresh_token'
40
-
41
- # Generators
42
- require 'grape_oauth2/generators/base'
43
- require 'grape_oauth2/generators/token'
44
- require 'grape_oauth2/generators/authorization'
45
-
46
- # Grape Helpers
47
- require 'grape_oauth2/helpers/access_token_helpers'
48
- require 'grape_oauth2/helpers/oauth_params'
49
-
50
- # Responses
51
- require 'grape_oauth2/responses/base'
52
- require 'grape_oauth2/responses/authorization'
53
- require 'grape_oauth2/responses/token'
54
-
55
- # Grape Endpoints
56
- require 'grape_oauth2/endpoints/token'
57
- require 'grape_oauth2/endpoints/authorize'
58
-
59
- # Use Grape namespace for the gem.
60
- module Grape
61
- # Main Grape::OAuth2 module.
62
- module OAuth2
63
- class << self
64
- # Grape::OAuth2 configuration.
65
- #
66
- # @return [Grape::OAuth2::Configuration]
67
- # configuration object
68
- #
69
- def config
70
- @config ||= Grape::OAuth2::Configuration.new
71
- end
72
-
73
- # Configures Grape::OAuth2.
74
- # Yields Grape::OAuth2::Configuration instance to the block.
75
- def configure
76
- yield config
77
- end
78
-
79
- # Validates Grape::OAuth2 configuration to be set correctly.
80
- def check_configuration!
81
- config.check!
82
- end
83
-
84
- # Grape::OAuth2 default middleware.
85
- def middleware
86
- [Rack::OAuth2::Server::Resource::Bearer, config.realm, config.token_authenticator]
87
- end
88
-
89
- # Method for injecting Grape::OAuth2 endpoints and helpers
90
- # into Grape API class. Automatically set required middleware,
91
- # OAuth2 helpers and mounts all (or configured) endpoints.
92
- #
93
- # @param endpoints [Array<Symbol>, Array<String>] endpoints to add
94
- #
95
- def api(*endpoints)
96
- inject_to_api do |api|
97
- api.use(*Grape::OAuth2.middleware)
98
- api.helpers(Grape::OAuth2::Helpers::AccessTokenHelpers)
99
-
100
- (endpoints.presence || endpoints_mapping.keys).each do |name|
101
- endpoint = endpoints_mapping[name.to_sym]
102
- raise ArgumentError, "Unrecognized endpoint: #{endpoint}" if endpoint.nil?
103
-
104
- api.mount(endpoint)
105
- end
106
- end
107
- end
108
-
109
- private
110
-
111
- def endpoints_mapping
112
- {
113
- token: ::Grape::OAuth2::Endpoints::Token,
114
- authorize: ::Grape::OAuth2::Endpoints::Authorize
115
- }
116
- end
117
-
118
- def inject_to_api(&_block)
119
- raise ArgumentError, 'block must be specified!' unless block_given?
120
-
121
- Module.new do |mod|
122
- mod.define_singleton_method :included do |base|
123
- yield base
124
- end
125
- end
126
- end
127
- end
128
- end
129
- end
1
+ require 'grape'
2
+ require 'rack/oauth2'
3
+
4
+ require 'grape_oauth2/version'
5
+ require 'grape_oauth2/configuration/validation'
6
+ require 'grape_oauth2/configuration/class_accessors'
7
+ require 'grape_oauth2/configuration'
8
+ require 'grape_oauth2/scopes'
9
+ require 'grape_oauth2/unique_token'
10
+
11
+ # NOTE: Extract to separate gems!!!
12
+ # This gem should contains only the core functionality and all mixins
13
+ # need to be moved to their own repos with their own tests.
14
+
15
+ # Mixins
16
+ if defined?(ActiveRecord::Base)
17
+ require 'grape_oauth2/mixins/active_record/access_token'
18
+ require 'grape_oauth2/mixins/active_record/access_grant'
19
+ require 'grape_oauth2/mixins/active_record/client'
20
+ end
21
+
22
+ if defined?(Sequel::Model)
23
+ require 'grape_oauth2/mixins/sequel/access_token'
24
+ require 'grape_oauth2/mixins/sequel/access_grant'
25
+ require 'grape_oauth2/mixins/sequel/client'
26
+ end
27
+
28
+ if defined?(Mongoid::Document)
29
+ require 'grape_oauth2/mixins/mongoid/access_token'
30
+ require 'grape_oauth2/mixins/mongoid/access_grant'
31
+ require 'grape_oauth2/mixins/mongoid/client'
32
+ end
33
+
34
+ # Authorization Grants aka Flows (Strategies)
35
+ require 'grape_oauth2/strategies/base'
36
+ require 'grape_oauth2/strategies/authorization_code'
37
+ require 'grape_oauth2/strategies/password'
38
+ require 'grape_oauth2/strategies/client_credentials'
39
+ require 'grape_oauth2/strategies/refresh_token'
40
+
41
+ # Generators
42
+ require 'grape_oauth2/generators/base'
43
+ require 'grape_oauth2/generators/token'
44
+ require 'grape_oauth2/generators/authorization'
45
+
46
+ # Grape Helpers
47
+ require 'grape_oauth2/helpers/access_token_helpers'
48
+ require 'grape_oauth2/helpers/oauth_params'
49
+
50
+ # Responses
51
+ require 'grape_oauth2/responses/base'
52
+ require 'grape_oauth2/responses/authorization'
53
+ require 'grape_oauth2/responses/token'
54
+
55
+ # Grape Endpoints
56
+ require 'grape_oauth2/endpoints/token'
57
+ require 'grape_oauth2/endpoints/authorize'
58
+
59
+ # Use Grape namespace for the gem.
60
+ module Grape
61
+ # Main Grape::OAuth2 module.
62
+ module OAuth2
63
+ class << self
64
+ # Grape::OAuth2 configuration.
65
+ #
66
+ # @return [Grape::OAuth2::Configuration]
67
+ # configuration object
68
+ #
69
+ def config
70
+ @config ||= Grape::OAuth2::Configuration.new
71
+ end
72
+
73
+ # Configures Grape::OAuth2.
74
+ # Yields Grape::OAuth2::Configuration instance to the block.
75
+ def configure
76
+ yield config
77
+ end
78
+
79
+ # Validates Grape::OAuth2 configuration to be set correctly.
80
+ def check_configuration!
81
+ config.check!
82
+ end
83
+
84
+ # Grape::OAuth2 default middleware.
85
+ def middleware
86
+ [Rack::OAuth2::Server::Resource::Bearer, config.realm, config.token_authenticator]
87
+ end
88
+
89
+ # Method for injecting Grape::OAuth2 endpoints and helpers
90
+ # into Grape API class. Automatically set required middleware,
91
+ # OAuth2 helpers and mounts all (or configured) endpoints.
92
+ #
93
+ # @param endpoints [Array<Symbol>, Array<String>] endpoints to add
94
+ #
95
+ def api(*endpoints)
96
+ inject_to_api do |api|
97
+ api.use(*Grape::OAuth2.middleware)
98
+ api.helpers(Grape::OAuth2::Helpers::AccessTokenHelpers)
99
+
100
+ (endpoints.presence || endpoints_mapping.keys).each do |name|
101
+ endpoint = endpoints_mapping[name.to_sym]
102
+ raise ArgumentError, "Unrecognized endpoint: #{endpoint}" if endpoint.nil?
103
+
104
+ api.mount(endpoint)
105
+ end
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def endpoints_mapping
112
+ {
113
+ token: ::Grape::OAuth2::Endpoints::Token,
114
+ authorize: ::Grape::OAuth2::Endpoints::Authorize
115
+ }
116
+ end
117
+
118
+ def inject_to_api(&_block)
119
+ raise ArgumentError, 'block must be specified!' unless block_given?
120
+
121
+ Module.new do |mod|
122
+ mod.define_singleton_method :included do |base|
123
+ yield base
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -1,143 +1,143 @@
1
- module Grape
2
- module OAuth2
3
- # Grape::OAuth2 configuration class.
4
- # Contains default or customized options that would be used
5
- # in OAuth2 endpoints and helpers.
6
- class Configuration
7
- # Default Grape::OAuth2 configuration error class.
8
- Error = Class.new(StandardError)
9
- # Grape::OAuth2 configuration error for missing API required for OAuth2 classes.
10
- APIMissing = Class.new(Error)
11
-
12
- include Validation
13
- include ClassAccessors
14
-
15
- # Default Access Token TTL (in seconds)
16
- DEFAULT_TOKEN_LIFETIME = 7200
17
- # Default Authorization Code TTL ()in seconds)
18
- DEFAULT_CODE_LIFETIME = 1800
19
-
20
- # Default realm value
21
- DEFAULT_REALM = 'OAuth 2.0'.freeze
22
-
23
- # Currently supported (be the gem) OAuth2 grant types
24
- SUPPORTED_GRANT_TYPES = %w(password client_credentials refresh_token).freeze
25
-
26
- # The names of the classes that represents OAuth2 roles
27
- #
28
- # @return [String] class name
29
- #
30
- attr_accessor :access_token_class_name, :access_grant_class_name,
31
- :client_class_name, :resource_owner_class_name
32
-
33
- # Class name for the OAuth2 helper class that validates requested scopes against Access Token scopes
34
- #
35
- # @return [String] scopes validator class name
36
- #
37
- attr_accessor :scopes_validator_class_name
38
-
39
- # Class name for the OAuth2 helper class that generates unique token values
40
- #
41
- # @return [String] token generator class name
42
- #
43
- attr_accessor :token_generator_class_name
44
-
45
- # OAuth2 grant types (flows) allowed to be processed
46
- #
47
- # @return [Array<String>] grant types
48
- #
49
- attr_accessor :allowed_grant_types
50
-
51
- # Access Token and Authorization Code lifetime in seconds
52
- attr_accessor :authorization_code_lifetime, :access_token_lifetime
53
-
54
- # Specifies whether to generate a Refresh Token when creating an Access Token
55
- #
56
- # @return [Boolean] true if need to generate refresh token, false in other case
57
- #
58
- attr_accessor :issue_refresh_token
59
-
60
- # Realm value
61
- #
62
- # @return [String] realm
63
- #
64
- attr_accessor :realm
65
-
66
- # Access Token authenticator block option for customization
67
- attr_accessor :token_authenticator
68
-
69
- # Callback that would be invoked during processing of Refresh Token request for
70
- # the original Access Token found by token value
71
- attr_accessor :on_refresh
72
-
73
- def initialize
74
- reset!
75
- end
76
-
77
- # Default Access Token authenticator block.
78
- # Validates token value passed with the request params.
79
- def default_token_authenticator
80
- lambda do |request|
81
- access_token_class.authenticate(request.access_token) || request.invalid_token!
82
- end
83
- end
84
-
85
- # Accessor for Access Token authenticator block. Set it to proc
86
- # if called with block or returns current value of the accessor.
87
- def token_authenticator(&block)
88
- if block_given?
89
- instance_variable_set(:'@token_authenticator', block)
90
- else
91
- instance_variable_get(:'@token_authenticator')
92
- end
93
- end
94
-
95
- # Accessor for on_refresh callback. Set callback proc
96
- # if called with block or returns current value of the accessor.
97
- def on_refresh(&block)
98
- if block_given?
99
- instance_variable_set(:'@on_refresh', block)
100
- else
101
- instance_variable_get(:'@on_refresh')
102
- end
103
- end
104
-
105
- # Indicates if on_refresh callback can be invoked.
106
- #
107
- # @return [Boolean]
108
- # true if callback can be invoked and false in other cases
109
- #
110
- def on_refresh_runnable?
111
- !on_refresh.nil? && on_refresh != :nothing
112
- end
113
-
114
- # Reset configuration to default options values.
115
- def reset!
116
- initialize_classes
117
- initialize_authenticators
118
-
119
- self.access_token_lifetime = DEFAULT_TOKEN_LIFETIME
120
- self.authorization_code_lifetime = DEFAULT_CODE_LIFETIME
121
- self.allowed_grant_types = %w(password client_credentials)
122
-
123
- self.issue_refresh_token = false
124
- self.on_refresh = :nothing
125
-
126
- self.realm = DEFAULT_REALM
127
- end
128
-
129
- private
130
-
131
- # Sets OAuth2 helpers classes to gem defaults.
132
- def initialize_classes
133
- self.scopes_validator_class_name = Grape::OAuth2::Scopes.name
134
- self.token_generator_class_name = Grape::OAuth2::UniqueToken.name
135
- end
136
-
137
- # Sets authenticators to gem defaults.
138
- def initialize_authenticators
139
- self.token_authenticator = default_token_authenticator
140
- end
141
- end
142
- end
143
- end
1
+ module Grape
2
+ module OAuth2
3
+ # Grape::OAuth2 configuration class.
4
+ # Contains default or customized options that would be used
5
+ # in OAuth2 endpoints and helpers.
6
+ class Configuration
7
+ # Default Grape::OAuth2 configuration error class.
8
+ Error = Class.new(StandardError)
9
+ # Grape::OAuth2 configuration error for missing API required for OAuth2 classes.
10
+ APIMissing = Class.new(Error)
11
+
12
+ include Validation
13
+ include ClassAccessors
14
+
15
+ # Default Access Token TTL (in seconds)
16
+ DEFAULT_TOKEN_LIFETIME = 7200
17
+ # Default Authorization Code TTL ()in seconds)
18
+ DEFAULT_CODE_LIFETIME = 1800
19
+
20
+ # Default realm value
21
+ DEFAULT_REALM = 'OAuth 2.0'.freeze
22
+
23
+ # Currently supported (by the gem) OAuth2 grant types
24
+ SUPPORTED_GRANT_TYPES = %w[password client_credentials refresh_token].freeze
25
+
26
+ # The names of the classes that represents OAuth2 roles
27
+ #
28
+ # @return [String] class name
29
+ #
30
+ attr_accessor :access_token_class_name, :access_grant_class_name,
31
+ :client_class_name, :resource_owner_class_name
32
+
33
+ # Class name for the OAuth2 helper class that validates requested scopes against Access Token scopes
34
+ #
35
+ # @return [String] scopes validator class name
36
+ #
37
+ attr_accessor :scopes_validator_class_name
38
+
39
+ # Class name for the OAuth2 helper class that generates unique token values
40
+ #
41
+ # @return [String] token generator class name
42
+ #
43
+ attr_accessor :token_generator_class_name
44
+
45
+ # OAuth2 grant types (flows) allowed to be processed
46
+ #
47
+ # @return [Array<String>] grant types
48
+ #
49
+ attr_accessor :allowed_grant_types
50
+
51
+ # Access Token and Authorization Code lifetime in seconds
52
+ attr_accessor :authorization_code_lifetime, :access_token_lifetime
53
+
54
+ # Specifies whether to generate a Refresh Token when creating an Access Token
55
+ #
56
+ # @return [Boolean] true if need to generate refresh token, false in other case
57
+ #
58
+ attr_accessor :issue_refresh_token
59
+
60
+ # Realm value
61
+ #
62
+ # @return [String] realm
63
+ #
64
+ attr_accessor :realm
65
+
66
+ # Access Token authenticator block option for customization
67
+ attr_accessor :token_authenticator
68
+
69
+ # Callback that would be invoked during processing of Refresh Token request for
70
+ # the original Access Token found by token value
71
+ attr_accessor :on_refresh
72
+
73
+ def initialize
74
+ reset!
75
+ end
76
+
77
+ # Default Access Token authenticator block.
78
+ # Validates token value passed with the request params.
79
+ def default_token_authenticator
80
+ lambda do |request|
81
+ access_token_class.authenticate(request.access_token) || request.invalid_token!
82
+ end
83
+ end
84
+
85
+ # Accessor for Access Token authenticator block. Set it to proc
86
+ # if called with block or returns current value of the accessor.
87
+ def token_authenticator(&block)
88
+ if block_given?
89
+ instance_variable_set(:'@token_authenticator', block)
90
+ else
91
+ instance_variable_get(:'@token_authenticator')
92
+ end
93
+ end
94
+
95
+ # Accessor for on_refresh callback. Set callback proc
96
+ # if called with block or returns current value of the accessor.
97
+ def on_refresh(&block)
98
+ if block_given?
99
+ instance_variable_set(:'@on_refresh', block)
100
+ else
101
+ instance_variable_get(:'@on_refresh')
102
+ end
103
+ end
104
+
105
+ # Indicates if on_refresh callback can be invoked.
106
+ #
107
+ # @return [Boolean]
108
+ # true if callback can be invoked and false in other cases
109
+ #
110
+ def on_refresh_runnable?
111
+ !on_refresh.nil? && on_refresh != :nothing
112
+ end
113
+
114
+ # Reset configuration to default options values.
115
+ def reset!
116
+ initialize_classes
117
+ initialize_authenticators
118
+
119
+ self.access_token_lifetime = DEFAULT_TOKEN_LIFETIME
120
+ self.authorization_code_lifetime = DEFAULT_CODE_LIFETIME
121
+ self.allowed_grant_types = %w[password client_credentials]
122
+
123
+ self.issue_refresh_token = false
124
+ self.on_refresh = :nothing
125
+
126
+ self.realm = DEFAULT_REALM
127
+ end
128
+
129
+ private
130
+
131
+ # Sets OAuth2 helpers classes to gem defaults.
132
+ def initialize_classes
133
+ self.scopes_validator_class_name = Grape::OAuth2::Scopes.name
134
+ self.token_generator_class_name = Grape::OAuth2::UniqueToken.name
135
+ end
136
+
137
+ # Sets authenticators to gem defaults.
138
+ def initialize_authenticators
139
+ self.token_authenticator = default_token_authenticator
140
+ end
141
+ end
142
+ end
143
+ end