doorkeeper 1.4.2 → 2.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of doorkeeper might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.hound.yml +3 -0
- data/CHANGELOG.md +18 -11
- data/Gemfile +7 -7
- data/README.md +20 -41
- data/app/controllers/doorkeeper/application_controller.rb +1 -7
- data/app/controllers/doorkeeper/applications_controller.rb +4 -5
- data/app/controllers/doorkeeper/tokens_controller.rb +2 -2
- data/app/helpers/doorkeeper/{form_errors_helper.rb → dashboard_helper.rb} +5 -1
- data/app/views/doorkeeper/applications/_delete_form.html.erb +1 -1
- data/app/views/doorkeeper/applications/_form.html.erb +1 -1
- data/app/views/doorkeeper/applications/index.html.erb +1 -1
- data/config/locales/en.yml +3 -3
- data/lib/doorkeeper.rb +15 -6
- data/lib/doorkeeper/config.rb +18 -24
- data/lib/doorkeeper/engine.rb +1 -6
- data/lib/doorkeeper/generators/doorkeeper/mongo_mapper/indexes_generator.rb +12 -0
- data/lib/{generators/doorkeeper → doorkeeper/generators/doorkeeper/mongo_mapper}/templates/indexes.rb +0 -0
- data/lib/doorkeeper/models/access_grant_mixin.rb +36 -0
- data/lib/doorkeeper/models/access_token_mixin.rb +121 -0
- data/lib/doorkeeper/models/application_mixin.rb +42 -0
- data/lib/doorkeeper/models/{accessible.rb → concerns/accessible.rb} +0 -0
- data/lib/doorkeeper/models/{expirable.rb → concerns/expirable.rb} +6 -5
- data/lib/doorkeeper/models/{ownership.rb → concerns/ownership.rb} +7 -7
- data/lib/doorkeeper/models/{revocable.rb → concerns/revocable.rb} +1 -1
- data/lib/doorkeeper/models/concerns/scopes.rb +17 -0
- data/lib/doorkeeper/oauth/authorization/token.rb +6 -6
- data/lib/doorkeeper/oauth/client.rb +1 -1
- data/lib/doorkeeper/oauth/password_access_token_request.rb +3 -3
- data/lib/doorkeeper/oauth/refresh_token_request.rb +6 -6
- data/lib/doorkeeper/oauth/token.rb +3 -2
- data/lib/doorkeeper/orm/active_record.rb +17 -0
- data/lib/doorkeeper/orm/active_record/access_grant.rb +7 -0
- data/lib/doorkeeper/orm/active_record/access_token.rb +20 -0
- data/lib/doorkeeper/{models → orm}/active_record/application.rb +1 -3
- data/lib/doorkeeper/orm/mongo_mapper.rb +11 -0
- data/lib/doorkeeper/{models → orm}/mongo_mapper/access_grant.rb +4 -5
- data/lib/doorkeeper/{models → orm}/mongo_mapper/access_token.rb +5 -8
- data/lib/doorkeeper/{models → orm}/mongo_mapper/application.rb +3 -4
- data/lib/doorkeeper/orm/mongoid2.rb +11 -0
- data/lib/doorkeeper/{models → orm}/mongoid2/access_grant.rb +5 -3
- data/lib/doorkeeper/{models → orm}/mongoid2/access_token.rb +10 -13
- data/lib/doorkeeper/{models → orm}/mongoid2/application.rb +2 -0
- data/lib/doorkeeper/orm/mongoid2/concerns/scopes.rb +30 -0
- data/lib/doorkeeper/orm/mongoid3.rb +11 -0
- data/lib/doorkeeper/orm/mongoid3/access_grant.rb +22 -0
- data/lib/doorkeeper/orm/mongoid3/access_token.rb +36 -0
- data/lib/doorkeeper/{models/mongoid3_4 → orm/mongoid3}/application.rb +2 -0
- data/lib/doorkeeper/orm/mongoid3/concerns/scopes.rb +30 -0
- data/lib/doorkeeper/orm/mongoid4.rb +11 -0
- data/lib/doorkeeper/orm/mongoid4/access_grant.rb +22 -0
- data/lib/doorkeeper/orm/mongoid4/access_token.rb +36 -0
- data/lib/doorkeeper/orm/mongoid4/application.rb +24 -0
- data/lib/doorkeeper/orm/mongoid4/concerns/scopes.rb +17 -0
- data/lib/doorkeeper/rails/helpers.rb +63 -0
- data/lib/doorkeeper/rails/routes.rb +1 -12
- data/lib/doorkeeper/request/code.rb +0 -1
- data/lib/doorkeeper/request/token.rb +0 -1
- data/lib/doorkeeper/server.rb +1 -1
- data/lib/doorkeeper/version.rb +1 -1
- data/lib/generators/doorkeeper/templates/initializer.rb +2 -1
- data/spec/controllers/applications_controller_spec.rb +4 -4
- data/spec/controllers/protected_resources_controller_spec.rb +25 -175
- data/spec/dummy/app/controllers/full_protected_resources_controller.rb +2 -2
- data/spec/dummy/app/controllers/metal_controller.rb +2 -2
- data/spec/dummy/app/controllers/semi_protected_resources_controller.rb +2 -2
- data/spec/dummy/config/application.rb +2 -0
- data/spec/lib/config_spec.rb +16 -10
- data/spec/lib/models/expirable_spec.rb +1 -1
- data/spec/lib/models/revocable_spec.rb +8 -3
- data/spec/lib/models/scopes_spec.rb +3 -3
- data/spec/lib/oauth/password_access_token_request_spec.rb +1 -1
- data/spec/lib/oauth/token_request_spec.rb +28 -1
- data/spec/lib/oauth/token_spec.rb +1 -1
- data/spec/models/doorkeeper/application_spec.rb +1 -1
- data/spec/requests/applications/applications_request_spec.rb +4 -4
- data/spec/requests/endpoints/authorization_spec.rb +0 -23
- data/spec/requests/flows/implicit_grant_spec.rb +32 -0
- data/spec/support/shared/controllers_shared_context.rb +2 -2
- metadata +40 -35
- data/lib/doorkeeper/doorkeeper_for.rb +0 -69
- data/lib/doorkeeper/helpers/filter.rb +0 -64
- data/lib/doorkeeper/models/access_grant.rb +0 -30
- data/lib/doorkeeper/models/access_token.rb +0 -106
- data/lib/doorkeeper/models/active_record/access_grant.rb +0 -9
- data/lib/doorkeeper/models/active_record/access_token.rb +0 -25
- data/lib/doorkeeper/models/application.rb +0 -40
- data/lib/doorkeeper/models/mongoid/scopes.rb +0 -15
- data/lib/doorkeeper/models/mongoid/version.rb +0 -15
- data/lib/doorkeeper/models/mongoid3_4/access_grant.rb +0 -27
- data/lib/doorkeeper/models/mongoid3_4/access_token.rb +0 -46
- data/lib/doorkeeper/models/scopes.rb +0 -21
- data/lib/generators/doorkeeper/mongo_mapper/indexes_generator.rb +0 -12
- data/script/rails +0 -5
- data/script/run_all +0 -14
@@ -0,0 +1,12 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
module MongoMapper
|
3
|
+
class IndexesGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path('templates', __FILE__)
|
5
|
+
desc "'Creates an indexes file for use with MongoMapper's rake db:index'"
|
6
|
+
|
7
|
+
def install
|
8
|
+
template 'indexes.rb', 'db/indexes.rb'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
File without changes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
module AccessGrantMixin
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
include OAuth::Helpers
|
6
|
+
include Models::Expirable
|
7
|
+
include Models::Revocable
|
8
|
+
include Models::Accessible
|
9
|
+
include Models::Scopes
|
10
|
+
|
11
|
+
included do
|
12
|
+
belongs_to :application, class_name: 'Doorkeeper::Application', inverse_of: :access_grants
|
13
|
+
|
14
|
+
if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes)
|
15
|
+
attr_accessible :resource_owner_id, :application_id, :expires_in, :redirect_uri, :scopes
|
16
|
+
end
|
17
|
+
|
18
|
+
validates :resource_owner_id, :application_id, :token, :expires_in, :redirect_uri, presence: true
|
19
|
+
validates :token, uniqueness: true
|
20
|
+
|
21
|
+
before_validation :generate_token, on: :create
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def by_token(token)
|
26
|
+
where(token: token).first
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def generate_token
|
33
|
+
self.token = UniqueToken.generate
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
module AccessTokenMixin
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
include OAuth::Helpers
|
6
|
+
include Models::Expirable
|
7
|
+
include Models::Revocable
|
8
|
+
include Models::Accessible
|
9
|
+
include Models::Scopes
|
10
|
+
|
11
|
+
included do
|
12
|
+
belongs_to :application,
|
13
|
+
class_name: 'Doorkeeper::Application',
|
14
|
+
inverse_of: :access_tokens
|
15
|
+
|
16
|
+
validates :token, presence: true, uniqueness: true
|
17
|
+
validates :refresh_token, uniqueness: true, if: :use_refresh_token?
|
18
|
+
|
19
|
+
attr_writer :use_refresh_token
|
20
|
+
|
21
|
+
if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes)
|
22
|
+
attr_accessible :application_id, :resource_owner_id, :expires_in,
|
23
|
+
:scopes, :use_refresh_token
|
24
|
+
end
|
25
|
+
|
26
|
+
before_validation :generate_token, on: :create
|
27
|
+
before_validation :generate_refresh_token,
|
28
|
+
on: :create,
|
29
|
+
if: :use_refresh_token?
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
def by_token(token)
|
34
|
+
where(token: token).first
|
35
|
+
end
|
36
|
+
|
37
|
+
def by_refresh_token(refresh_token)
|
38
|
+
where(refresh_token: refresh_token).first
|
39
|
+
end
|
40
|
+
|
41
|
+
def revoke_all_for(application_id, resource_owner)
|
42
|
+
where(application_id: application_id,
|
43
|
+
resource_owner_id: resource_owner.id,
|
44
|
+
revoked_at: nil).
|
45
|
+
map(&:revoke)
|
46
|
+
end
|
47
|
+
|
48
|
+
def matching_token_for(application, resource_owner_or_id, scopes)
|
49
|
+
resource_owner_id = if resource_owner_or_id.respond_to?(:to_key)
|
50
|
+
resource_owner_or_id.id
|
51
|
+
else
|
52
|
+
resource_owner_or_id
|
53
|
+
end
|
54
|
+
token = last_authorized_token_for(application.try(:id), resource_owner_id)
|
55
|
+
token if token && Doorkeeper::OAuth::Helpers::ScopeChecker.matches?(token.scopes, scopes)
|
56
|
+
end
|
57
|
+
|
58
|
+
def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
|
59
|
+
if Doorkeeper.configuration.reuse_access_token
|
60
|
+
access_token = matching_token_for(application, resource_owner_id, scopes)
|
61
|
+
if access_token && !access_token.expired?
|
62
|
+
return access_token
|
63
|
+
end
|
64
|
+
end
|
65
|
+
create!(
|
66
|
+
application_id: application.try(:id),
|
67
|
+
resource_owner_id: resource_owner_id,
|
68
|
+
scopes: scopes.to_s,
|
69
|
+
expires_in: expires_in,
|
70
|
+
use_refresh_token: use_refresh_token
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def last_authorized_token_for(application_id, resource_owner_id)
|
75
|
+
where(application_id: application_id,
|
76
|
+
resource_owner_id: resource_owner_id,
|
77
|
+
revoked_at: nil).
|
78
|
+
send(order_method, created_at_desc).
|
79
|
+
limit(1).
|
80
|
+
first
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def token_type
|
85
|
+
'bearer'
|
86
|
+
end
|
87
|
+
|
88
|
+
def use_refresh_token?
|
89
|
+
!!@use_refresh_token
|
90
|
+
end
|
91
|
+
|
92
|
+
def as_json(_options = {})
|
93
|
+
{
|
94
|
+
resource_owner_id: resource_owner_id,
|
95
|
+
scopes: scopes,
|
96
|
+
expires_in_seconds: expires_in_seconds,
|
97
|
+
application: { uid: application.try(:uid) }
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
# It indicates whether the tokens have the same credential
|
102
|
+
def same_credential?(access_token)
|
103
|
+
application_id == access_token.application_id &&
|
104
|
+
resource_owner_id == access_token.resource_owner_id
|
105
|
+
end
|
106
|
+
|
107
|
+
def acceptable?(scopes)
|
108
|
+
accessible? && includes_scope?(*scopes)
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def generate_refresh_token
|
114
|
+
write_attribute :refresh_token, UniqueToken.generate
|
115
|
+
end
|
116
|
+
|
117
|
+
def generate_token
|
118
|
+
self.token = UniqueToken.generate
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
module ApplicationMixin
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
include OAuth::Helpers
|
6
|
+
|
7
|
+
included do
|
8
|
+
has_many :access_grants, dependent: :destroy, class_name: 'Doorkeeper::AccessGrant'
|
9
|
+
has_many :access_tokens, dependent: :destroy, class_name: 'Doorkeeper::AccessToken'
|
10
|
+
|
11
|
+
validates :name, :secret, :uid, presence: true
|
12
|
+
validates :uid, uniqueness: true
|
13
|
+
validates :redirect_uri, redirect_uri: true
|
14
|
+
|
15
|
+
before_validation :generate_uid, :generate_secret, on: :create
|
16
|
+
|
17
|
+
if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes)
|
18
|
+
attr_accessible :name, :redirect_uri
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def by_uid_and_secret(uid, secret)
|
24
|
+
where(uid: uid, secret: secret).first
|
25
|
+
end
|
26
|
+
|
27
|
+
def by_uid(uid)
|
28
|
+
where(uid: uid).first
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def generate_uid
|
35
|
+
self.uid ||= UniqueToken.generate
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_secret
|
39
|
+
self.secret ||= UniqueToken.generate
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
File without changes
|
@@ -5,17 +5,18 @@ module Doorkeeper
|
|
5
5
|
expires_in && Time.now > expired_time
|
6
6
|
end
|
7
7
|
|
8
|
-
def expired_time
|
9
|
-
created_at + expires_in.seconds
|
10
|
-
end
|
11
|
-
|
12
8
|
def expires_in_seconds
|
13
9
|
return nil if expires_in.nil?
|
14
10
|
expires = (created_at + expires_in.seconds) - Time.now
|
15
11
|
expires_sec = expires.seconds.round(0)
|
16
12
|
expires_sec > 0 ? expires_sec : 0
|
17
13
|
end
|
18
|
-
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def expired_time
|
18
|
+
created_at + expires_in.seconds
|
19
|
+
end
|
19
20
|
end
|
20
21
|
end
|
21
22
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Doorkeeper
|
2
2
|
module Models
|
3
3
|
module Ownership
|
4
|
-
|
5
|
-
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
belongs_to :owner, polymorphic: true
|
8
|
+
validates :owner, presence: true, if: :validate_owner?
|
6
9
|
end
|
7
10
|
|
8
|
-
def
|
9
|
-
|
10
|
-
belongs_to :owner, polymorphic: true
|
11
|
-
validates :owner, presence: true, if: :validate_owner?
|
12
|
-
end
|
11
|
+
def validate_owner?
|
12
|
+
Doorkeeper.configuration.confirm_application_owner?
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
module Models
|
3
|
+
module Scopes
|
4
|
+
def scopes
|
5
|
+
OAuth::Scopes.from_string(self[:scopes])
|
6
|
+
end
|
7
|
+
|
8
|
+
def scopes_string
|
9
|
+
self[:scopes]
|
10
|
+
end
|
11
|
+
|
12
|
+
def includes_scope?(*required_scopes)
|
13
|
+
required_scopes.blank? || required_scopes.any? { |s| scopes.exists?(s.to_s) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -10,12 +10,12 @@ module Doorkeeper
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def issue_token
|
13
|
-
@token ||= AccessToken.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
@token ||= AccessToken.find_or_create_for(
|
14
|
+
pre_auth.client,
|
15
|
+
resource_owner.id,
|
16
|
+
pre_auth.scopes,
|
17
|
+
configuration.access_token_expires_in,
|
18
|
+
false
|
19
19
|
)
|
20
20
|
end
|
21
21
|
|
@@ -10,7 +10,7 @@ module Doorkeeper
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def self.authenticate(credentials, method = Application.method(:
|
13
|
+
def self.authenticate(credentials, method = Application.method(:by_uid_and_secret))
|
14
14
|
return false if credentials.blank?
|
15
15
|
if application = method.call(credentials.uid, credentials.secret)
|
16
16
|
new(application)
|
@@ -6,7 +6,7 @@ module Doorkeeper
|
|
6
6
|
include OAuth::Helpers
|
7
7
|
|
8
8
|
validate :client, error: :invalid_client
|
9
|
-
validate :resource_owner, error: :
|
9
|
+
validate :resource_owner, error: :invalid_grant
|
10
10
|
validate :scopes, error: :invalid_scope
|
11
11
|
|
12
12
|
attr_accessor :server, :resource_owner, :credentials, :access_token
|
@@ -19,8 +19,8 @@ module Doorkeeper
|
|
19
19
|
@original_scopes = parameters[:scope]
|
20
20
|
|
21
21
|
if credentials
|
22
|
-
@client = Application.
|
23
|
-
|
22
|
+
@client = Application.by_uid_and_secret credentials.uid,
|
23
|
+
credentials.secret
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -14,14 +14,14 @@ module Doorkeeper
|
|
14
14
|
attr_accessor :client
|
15
15
|
|
16
16
|
def initialize(server, refresh_token, credentials, parameters = {})
|
17
|
-
@server
|
18
|
-
@refresh_token
|
19
|
-
@credentials
|
20
|
-
@original_scopes
|
17
|
+
@server = server
|
18
|
+
@refresh_token = refresh_token
|
19
|
+
@credentials = credentials
|
20
|
+
@original_scopes = parameters[:scopes]
|
21
21
|
|
22
22
|
if credentials
|
23
|
-
@client = Application.
|
24
|
-
|
23
|
+
@client = Application.by_uid_and_secret credentials.uid,
|
24
|
+
credentials.secret
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -54,8 +54,9 @@ module Doorkeeper
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def self.authenticate(request, *methods)
|
57
|
-
token = from_request
|
58
|
-
|
57
|
+
if token = from_request(request, *methods)
|
58
|
+
AccessToken.by_token(token)
|
59
|
+
end
|
59
60
|
end
|
60
61
|
end
|
61
62
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
module Orm
|
3
|
+
module ActiveRecord
|
4
|
+
def self.initialize_models!
|
5
|
+
require 'doorkeeper/orm/active_record/access_grant'
|
6
|
+
require 'doorkeeper/orm/active_record/access_token'
|
7
|
+
require 'doorkeeper/orm/active_record/application'
|
8
|
+
|
9
|
+
if Doorkeeper.configuration.active_record_options[:establish_connection]
|
10
|
+
[Doorkeeper::AccessGrant, Doorkeeper::Application, Doorkeeper::AccessGrant].each do |c|
|
11
|
+
c.send :establish_connection, Doorkeeper.configuration.active_record_options[:establish_connection]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Doorkeeper
|
2
|
+
class AccessToken < ActiveRecord::Base
|
3
|
+
include AccessTokenMixin
|
4
|
+
|
5
|
+
self.table_name = "#{table_name_prefix}oauth_access_tokens#{table_name_suffix}".to_sym
|
6
|
+
|
7
|
+
def self.delete_all_for(application_id, resource_owner)
|
8
|
+
where(application_id: application_id,
|
9
|
+
resource_owner_id: resource_owner.id).delete_all
|
10
|
+
end
|
11
|
+
private_class_method :delete_all_for
|
12
|
+
|
13
|
+
def self.order_method
|
14
|
+
:order
|
15
|
+
end
|
16
|
+
def self.created_at_desc
|
17
|
+
'created_at desc'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
module Doorkeeper
|
2
2
|
class Application < ActiveRecord::Base
|
3
|
-
|
4
|
-
establish_connection Doorkeeper.configuration.active_record_options[:establish_connection]
|
5
|
-
end
|
3
|
+
include ApplicationMixin
|
6
4
|
|
7
5
|
self.table_name = "#{table_name_prefix}oauth_applications#{table_name_suffix}".to_sym
|
8
6
|
|