doorkeeper-nobrainer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: beb7e759d1ddae184d48f5dc18733c9357b0e662
4
+ data.tar.gz: 95e4a5242ec601e67ec12e4fab2b6f2005420b26
5
+ SHA512:
6
+ metadata.gz: cf365287c793589ffc4be2b1cc040a3e9338d5aab3eb729d75275baec08445d952807eb9e496726aa76efe1df31e61bd943f8bcfb44d26e4e4b39347621a4e7f
7
+ data.tar.gz: c9ba649a5c5a689cb05db9c7345d24f93edd81e6103fadff964ae7d033c87b0a29647229dd75dd9a7c20c68c88480d5beb44fb47c014373bf7b3efdb2a091b0c
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Jasl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # doorkeeper-nobrainer extension
2
+
3
+ ## Installation
4
+
5
+ doorkeeper-nobrainer provides doorkeeper support to NoBrainer ORM
6
+ To start using it, add to your Gemfile:
7
+
8
+ ``` ruby
9
+ gem 'doorkeeper-nobrainer'
10
+ ```
11
+
12
+ Run [doorkeeper’s installation generator]:
13
+
14
+ rails generate doorkeeper:install
15
+
16
+ [doorkeeper’s installation generator]: https://github.com/doorkeeper-gem/doorkeeper#installation
17
+
18
+ This will install the doorkeeper initializer into
19
+ `config/initializers/doorkeeper.rb`.
20
+
21
+ Set the ORM configuration:
22
+
23
+ ``` ruby
24
+ Doorkeeper.configure do
25
+ orm :nobrainer
26
+ end
27
+ ```
28
+
29
+ ### NoBrainer indexes
30
+
31
+ `rake nobrainer:sync_indexes`
32
+
33
+ ---
@@ -0,0 +1,23 @@
1
+ require 'bundler/setup'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :load_doorkeeper do
5
+ `rm -rf spec/`
6
+ `git checkout spec`
7
+ `git submodule init`
8
+ `git submodule update`
9
+ `cp -r -n doorkeeper/spec .`
10
+ `bundle exec rspec`
11
+ end
12
+
13
+ desc 'Default: run specs.'
14
+ task default: :spec
15
+
16
+ desc 'Clone down doorkeeper specs'
17
+ task spec: :load_doorkeeper
18
+
19
+ RSpec::Core::RakeTask.new(:spec) do |config|
20
+ config.verbose = false
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,10 @@
1
+ require 'doorkeeper'
2
+ require 'doorkeeper/orm/nobrainer/concerns/scopes'
3
+ require 'doorkeeper/mixin/access_grant_mixin'
4
+ require 'doorkeeper/mixin/access_token_mixin'
5
+
6
+ require 'doorkeeper-nobrainer/version'
7
+
8
+ require 'doorkeeper/orm/nobrainer'
9
+
10
+ module DoorkeeperNobrainer;end
@@ -0,0 +1,3 @@
1
+ module DoorkeeperNobrainer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ module DoorkeeperNobrainer
2
+ module AccessGrantMixin
3
+ extend ActiveSupport::Concern
4
+
5
+ include Doorkeeper::OAuth::Helpers
6
+ include Doorkeeper::Models::Expirable
7
+ include Doorkeeper::Models::Revocable
8
+ include Doorkeeper::Models::Accessible
9
+ include Doorkeeper::Models::Scopes
10
+ include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes)
11
+
12
+ included do
13
+ belongs_to :application, class_name: 'Doorkeeper::Application'
14
+
15
+ if respond_to?(:attr_accessible)
16
+ attr_accessible :resource_owner_id, :application_id, :expires_in, :redirect_uri, :scopes
17
+ end
18
+
19
+ validates :resource_owner_id, :application_id, :token, :expires_in, :redirect_uri, presence: true
20
+ validates :token, uniqueness: true
21
+
22
+ before_validation :generate_token, on: :create
23
+ end
24
+
25
+ module ClassMethods
26
+ def by_token(token)
27
+ where(token: token.to_s).limit(1).to_a.first
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def generate_token
34
+ self.token = UniqueToken.generate
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,141 @@
1
+ module DoorkeeperNobrainer
2
+ module AccessTokenMixin
3
+ extend ActiveSupport::Concern
4
+
5
+ include Doorkeeper::OAuth::Helpers
6
+ include Doorkeeper::Models::Expirable
7
+ include Doorkeeper::Models::Revocable
8
+ include Doorkeeper::Models::Accessible
9
+ include Doorkeeper::Models::Scopes
10
+ include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes)
11
+
12
+ included do
13
+ belongs_to :application, class_name: 'Doorkeeper::Application'
14
+
15
+ validates :token, presence: true, uniqueness: true
16
+ validates :refresh_token, uniqueness: true, if: :use_refresh_token?
17
+
18
+ attr_writer :use_refresh_token
19
+
20
+ if respond_to?(:attr_accessible)
21
+ attr_accessible :application_id, :resource_owner_id, :expires_in,
22
+ :scopes, :use_refresh_token
23
+ end
24
+
25
+ before_validation :generate_token, on: :create
26
+ before_validation :generate_refresh_token,
27
+ on: :create,
28
+ if: :use_refresh_token?
29
+ end
30
+
31
+ class_methods do
32
+ def by_token(token)
33
+ where(token: token.to_s).limit(1).to_a.first
34
+ end
35
+
36
+ def by_refresh_token(refresh_token)
37
+ where(refresh_token: refresh_token.to_s).first
38
+ end
39
+
40
+ def revoke_all_for(application_id, resource_owner)
41
+ where(application_id: application_id,
42
+ resource_owner_id: resource_owner.id,
43
+ revoked_at: nil).
44
+ map(&:revoke)
45
+ end
46
+
47
+ def matching_token_for(application, resource_owner_or_id, scopes)
48
+ resource_owner_id = if resource_owner_or_id.respond_to?(:to_key)
49
+ resource_owner_or_id.id
50
+ else
51
+ resource_owner_or_id
52
+ end
53
+ token = last_authorized_token_for(application.try(:id), resource_owner_id)
54
+ if token && scopes_match?(token.scopes, scopes, application.try(:scopes))
55
+ token
56
+ end
57
+ end
58
+
59
+ def scopes_match?(token_scopes, param_scopes, app_scopes)
60
+ (!token_scopes.present? && !param_scopes.present?) ||
61
+ Doorkeeper::OAuth::Helpers::ScopeChecker.match?(
62
+ token_scopes.to_s,
63
+ param_scopes,
64
+ app_scopes
65
+ )
66
+ end
67
+
68
+ def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
69
+ if Doorkeeper.configuration.reuse_access_token
70
+ access_token = matching_token_for(application, resource_owner_id, scopes)
71
+ if access_token && !access_token.expired?
72
+ return access_token
73
+ end
74
+ end
75
+ create!(
76
+ application_id: application.try(:id),
77
+ resource_owner_id: resource_owner_id,
78
+ scopes: scopes.to_s,
79
+ expires_in: expires_in,
80
+ use_refresh_token: use_refresh_token
81
+ )
82
+ end
83
+
84
+ def last_authorized_token_for(application_id, resource_owner_id)
85
+ where(application_id: application_id,
86
+ resource_owner_id: resource_owner_id,
87
+ revoked_at: nil).
88
+ send(order_method, created_at_desc).
89
+ limit(1).
90
+ to_a.
91
+ first
92
+ end
93
+ end
94
+
95
+ def token_type
96
+ 'bearer'
97
+ end
98
+
99
+ def use_refresh_token?
100
+ !!@use_refresh_token
101
+ end
102
+
103
+ def as_json(_options = {})
104
+ {
105
+ resource_owner_id: resource_owner_id,
106
+ scopes: scopes,
107
+ expires_in_seconds: expires_in_seconds,
108
+ application: { uid: application.try(:uid) },
109
+ created_at: created_at.to_i,
110
+ }
111
+ end
112
+
113
+ # It indicates whether the tokens have the same credential
114
+ def same_credential?(access_token)
115
+ application_id == access_token.application_id &&
116
+ resource_owner_id == access_token.resource_owner_id
117
+ end
118
+
119
+ def acceptable?(scopes)
120
+ accessible? && includes_scope?(*scopes)
121
+ end
122
+
123
+ private
124
+
125
+ def generate_refresh_token
126
+ write_attribute :refresh_token, UniqueToken.generate
127
+ end
128
+
129
+ def generate_token
130
+ generator = Doorkeeper.configuration.access_token_generator.constantize
131
+ self.token = generator.generate(resource_owner_id: resource_owner_id,
132
+ scopes: scopes, application: application,
133
+ expires_in: expires_in)
134
+ rescue NoMethodError
135
+ raise Errors::UnableToGenerateToken, "{generator} does not respond to `.generate`."
136
+ rescue NameError
137
+ raise Errors::TokenGeneratorNotFound, "{generator} not found"
138
+ end
139
+ end
140
+
141
+ end
@@ -0,0 +1,19 @@
1
+ module Doorkeeper
2
+ module Orm
3
+ module Nobrainer
4
+ def self.initialize_models!
5
+ require 'doorkeeper/orm/nobrainer/application'
6
+ require 'doorkeeper/orm/nobrainer/access_grant'
7
+ require 'doorkeeper/orm/nobrainer/access_token'
8
+ end
9
+
10
+ def self.initialize_application_owner!
11
+ require 'doorkeeper/models/concerns/ownership'
12
+
13
+ Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership
14
+ end
15
+
16
+ def self.check_requirements!(_config); end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+
2
+ module Doorkeeper
3
+ class AccessGrant
4
+ include NoBrainer::Document
5
+ include NoBrainer::Document::Timestamps
6
+
7
+ include DoorkeeperNobrainer::AccessGrantMixin
8
+ include Doorkeeper::Models::Nobrainer::Scopes
9
+ store_in table: :oauth_access_grants
10
+
11
+ field :resource_owner_id, primary_key: true
12
+ field :token, type: String, index: true, unique: true
13
+ field :expires_in, type: Integer
14
+ field :redirect_uri, type: String
15
+ field :revoked_at, type: DateTime
16
+
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+
2
+ module Doorkeeper
3
+ class AccessToken
4
+ include NoBrainer::Document
5
+ include NoBrainer::Document::Timestamps
6
+
7
+ include DoorkeeperNobrainer::AccessTokenMixin
8
+ include Doorkeeper::Models::Nobrainer::Scopes
9
+
10
+ store_in table: :oauth_access_tokens
11
+
12
+ field :resource_owner_id, primary_key: true
13
+ field :token, type: String, index: true, unique: true
14
+ field :refresh_token, type: String, index: true
15
+ field :expires_in, type: Integer
16
+ field :revoked_at, type: DateTime
17
+
18
+
19
+ def self.delete_all_for(application_id, resource_owner)
20
+ where(application_id: application_id,
21
+ resource_owner_id: resource_owner.id).delete_all
22
+ end
23
+ private_class_method :delete_all_for
24
+
25
+ def self.order_method
26
+ :order_by
27
+ end
28
+
29
+ def self.created_at_desc
30
+ [:created_at, :desc]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ module Doorkeeper
2
+ class Application
3
+ include NoBrainer::Document
4
+ include NoBrainer::Document::Timestamps
5
+ include Models::Nobrainer::Scopes
6
+
7
+ include ApplicationMixin
8
+
9
+ store_in table: :oauth_applications
10
+
11
+ field :name, type: String
12
+ field :uid, type: String, index: true, unique: true
13
+ field :secret, type: String
14
+ field :redirect_uri, type: String
15
+
16
+ has_many :authorized_tokens, class_name: 'Doorkeeper::AccessToken'
17
+
18
+ def self.authorized_for(resource_owner)
19
+ ids = AccessToken.where(resource_owner_id: resource_owner.id, revoked_at: nil).map(&:application_id)
20
+ find(ids)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module Doorkeeper
2
+ module Models
3
+ module Nobrainer
4
+ module Scopes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ field :scopes, type: String
9
+ end
10
+
11
+ def scopes=(value)
12
+ write_attribute :scopes, value if value.present?
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class User
2
+ include Nobrainer::Document
3
+ include Nobrainer::Timestamps
4
+
5
+ field :name, type: String
6
+ field :password, type: String
7
+ end
8
+
9
+ class User
10
+ if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes)
11
+ attr_accessible :name, :password
12
+ end
13
+
14
+ def self.authenticate!(name, password)
15
+ User.where(name: name, password: password).first
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doorkeeper-nobrainer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Marín
8
+ - Gustavo Giménez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-06-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nobrainer
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: doorkeeper
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.2.1
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 2.2.1
42
+ description: Doorkeeper NoBrainer ORM
43
+ email:
44
+ - higher.vnf@gmail.com
45
+ - gimenezanderson@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - MIT-LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/doorkeeper-nobrainer.rb
54
+ - lib/doorkeeper-nobrainer/version.rb
55
+ - lib/doorkeeper/mixin/access_grant_mixin.rb
56
+ - lib/doorkeeper/mixin/access_token_mixin.rb
57
+ - lib/doorkeeper/orm/nobrainer.rb
58
+ - lib/doorkeeper/orm/nobrainer/access_grant.rb
59
+ - lib/doorkeeper/orm/nobrainer/access_token.rb
60
+ - lib/doorkeeper/orm/nobrainer/application.rb
61
+ - lib/doorkeeper/orm/nobrainer/concerns/scopes.rb
62
+ - spec/dummy/app/models/user.rb
63
+ homepage: http://github.com/escuelaweb/doorkeeper-nobrainer
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Doorkeeper NoBrainer ORM
87
+ test_files:
88
+ - spec/dummy/app/models/user.rb