diff --git a/.gitignore b/.gitignore index f6ee337..43625e8 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ spec/generators/tmp .rvmrc *.swp .idea +spec diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6bfd7ba --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "doorkeeper"] + path = doorkeeper + url = https://github.com/doorkeeper-gem/doorkeeper.git diff --git a/.hound.yml b/.hound.yml index b4126ad..27ef9c1 100644 --- a/.hound.yml +++ b/.hound.yml @@ -4,3 +4,6 @@ LineLength: StringLiterals: Enabled: false + +TrailingBlankLines: + Enabled: true diff --git a/.travis.yml b/.travis.yml index 77ce1df..3b5082f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,41 @@ language: ruby +sudo: false +cache: bundler + rvm: - - 1.9.3 - 2.0 - 2.1 + - 2.2 + env: - - orm=mongoid2 - - orm=mongoid3 - - orm=mongoid4 - - orm=mongo_mapper + - rails=3.2.0 + - rails=4.1.0 + - rails=4.2.0 + +gemfile: + - Gemfile + - gemfiles/Gemfile.mongoid2.rb + - gemfiles/Gemfile.mongoid3.rb + - gemfiles/Gemfile.mongoid4.rb + - gemfiles/Gemfile.mongo_mapper.rb + services: - mongodb + +matrix: + exclude: + - env: rails=3.2.0 + rvm: 2.2 + + - gemfile: gemfiles/Gemfile.mongoid2.rb + env: rails=4.1.0 + - gemfile: gemfiles/Gemfile.mongoid2.rb + env: rails=4.2.0 + + - gemfile: gemfiles/Gemfile.mongoid3.rb + env: rails=4.1.0 + - gemfile: gemfiles/Gemfile.mongoid3.rb + env: rails=4.2.0 + + - gemfile: gemfiles/Gemfile.mongoid4.rb + env: rails=3.2.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..22d4311 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,45 @@ +# Contributing + +We love pull requests from everyone. By participating in this project, you agree +to abide by the thoughtbot [code of conduct]. + +[code of conduct]: https://thoughtbot.com/open-source-code-of-conduct + +Fork, then clone the repo: + + git clone git@github.com:your-username/doorkeeper-orms.git + +Set up Ruby dependencies via Bundler + + bundle install + +Make sure the tests pass: + + rake + +Make your change. +Write tests. +Follow our [style guide][style]. +Make the tests pass: + +[style]: https://github.com/thoughtbot/guides/tree/master/style + + rake + +Write a [good commit message][commit]. +Push to your fork. +[Submit a pull request][pr]. + +[commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html +[pr]: https://github.com/doorkeeper-gem/doorkeeper-orms/compare/ + +If [Hound] catches style violations, +fix them. + +[hound]: https://houndci.com + +Wait for us. +We try to at least comment on pull requests within one business day. +We may suggest changes. + +Thank you for your contribution! diff --git a/Gemfile b/Gemfile index e72821f..00bdf63 100644 --- a/Gemfile +++ b/Gemfile @@ -1,28 +1,8 @@ -source "https://rubygems.org" +ENV['rails'] ||= '4.2.0' -gem 'doorkeeper', github: 'jasl/doorkeeper', branch: 'extract-orm-specifics' +source 'https://rubygems.org' -# Defaults. For supported versions check .travis.yml -ENV['orm'] ||= 'mongoid4' -ENV['rails'] ||= ENV['orm'] == 'mongoid4' ? '~> 4.1.2' : '~> 3.2.13' - -gem 'rails', ENV['rails'] - -case ENV['orm'] -when 'active_record' - gem 'activerecord' - -when 'mongoid2' - gem 'mongoid', '~> 2' - -when 'mongoid3' - gem 'mongoid', '~> 3' - -when 'mongoid4' - gem 'mongoid', '~> 4' - -when 'mongo_mapper' - gem 'mongo_mapper', '~> 0' -end +gem 'rails', "~> #{ENV['rails']}" +gem "doorkeeper" gemspec diff --git a/README.md b/README.md index cedec0a..fde6a96 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,54 @@ -# Doorkeeper bundle +# doorkeeper-orms extension -==== +## Installation -Doorkeeper's extracted ORM specifics. +doorkeeper-orms provides doorkeeper support to MongoMapper, Mongoid 2, 3 and 4. +To start using it, add to your Gemfile: -including: +``` ruby +gem 'doorkeeper-orms' +``` -- Mongoid2 -- Mongoid3 -- Mongoid4 -- Mongo mapper +Run [doorkeeper’s installation generator]: -### License + rails generate doorkeeper:install -MIT. +[doorkeeper’s installation generator]: https://github.com/doorkeeper-gem/doorkeeper#installation + +This will install the doorkeeper initializer into +`config/initializers/doorkeeper.rb`. + +Set the ORM configuration: + +``` ruby +Doorkeeper.configure do + orm :mongoid2 # or :mongoid3, :mongoid4, :mongo_mapper +end +``` + +### Mongoid indexes + +Make sure you create indexes for doorkeeper models. You can do this either by +running `rake db:mongoid:create_indexes` or (if you're using Mongoid 2) by +adding `autocreate_indexes: true` to your `config/mongoid.yml` + + +### MongoMapper indexes + +Generate the `db/indexes.rb` file and create indexes for the doorkeeper models: + + rails generate doorkeeper:mongo_mapper:indexes + rake db:index + + +## Tests + +To run tests, clone this repository and run `rake`. It will copy and run +doorkeeper’s original test suite, after configuring the ORM according to the +variables defined in `.travis.yml` file. + + +--- + +Please refer to https://github.com/doorkeeper-gem/doorkeeper for instructions on +doorkeeper’s project. diff --git a/Rakefile b/Rakefile index 78c14c4..db23caf 100644 --- a/Rakefile +++ b/Rakefile @@ -1,24 +1,28 @@ -begin - require 'bundler/setup' -rescue LoadError - puts 'You must `gem install bundler` and `bundle install` to run rake tasks' -end - +require 'bundler/setup' require 'rspec/core/rake_task' -require 'rdoc/task' +desc 'Default: run specs.' +task :default => :spec -RDoc::Task.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'DoorkeeperOrms' - rdoc.options << '--line-numbers' - rdoc.rdoc_files.include('lib/**/*.rb') +task :load_doorkeeper do + `git submodule init` + `git submodule update` + `cp -r doorkeeper/spec .` + `bundle exec rspec` end -Bundler::GemHelper.install_tasks +RSpec::Core::RakeTask.new(:spec) do |config| + config.verbose = false +end -desc "Run all specs" -RSpec::Core::RakeTask.new(:spec) +Rake::Task["spec"].enhance [:load_doorkeeper] -desc 'Default: run specs.' -task :default => :spec \ No newline at end of file +namespace :doorkeeper do + desc "Install doorkeeper in dummy app" + task :install do + cd 'spec/dummy' + system 'bundle exec rails g doorkeeper:install --force' + end +end + +Bundler::GemHelper.install_tasks diff --git a/doorkeeper b/doorkeeper new file mode 160000 index 0000000..365c10e --- /dev/null +++ b/doorkeeper @@ -0,0 +1 @@ +Subproject commit 365c10ea51bc451d4b075cc0a005cf05fb900e17 diff --git a/doorkeeper-orms.gemspec b/doorkeeper-orms.gemspec index f75b31d..ae08635 100644 --- a/doorkeeper-orms.gemspec +++ b/doorkeeper-orms.gemspec @@ -10,17 +10,23 @@ Gem::Specification.new do |s| s.authors = ["jasl"] s.email = ["jasl9187@hotmail.com"] s.homepage = "http://github.com/doorkeeper-gem/doorkeeper-orms" - s.summary = "Doorkeeper with extracted ORM specifics, including mongoid 2-4 and mongo_mapper." - s.description = "Doorkeeper with extracted ORM specifics, including mongoid 2-4 and mongo_mapper" + s.summary = "Doorkeeper mongoid 2, 3, 4 and mongo_mapper ORMs" + s.description = "Doorkeeper mongoid 2, 3, 4 and mongo_mapper ORMs" s.license = "MIT" s.files = Dir["lib/**/*", "MIT-LICENSE", "Rakefile", "README.md"] s.test_files = Dir["spec/**/*"] - s.add_dependency "rails", ">= 3.1" + s.add_dependency "doorkeeper", "~> 2.2.0" - s.add_development_dependency "rspec-rails", "~> 2.99.0" - s.add_development_dependency "factory_girl", "~> 4.4.0" + s.add_development_dependency "sqlite3", "~> 1.3.5" + s.add_development_dependency "rspec-rails", "~> 3.2.0" + s.add_development_dependency "capybara", "~> 2.3.0" + s.add_development_dependency "generator_spec", "~> 0.9.0" + s.add_development_dependency "factory_girl", "~> 4.5.0" s.add_development_dependency "timecop", "~> 0.7.0" s.add_development_dependency "database_cleaner", "~> 1.3.0" + s.add_development_dependency "rspec-activemodel-mocks", "~> 1.0.0" + s.add_development_dependency "bcrypt-ruby", "~> 3.0.1" + s.add_development_dependency "pry", "~> 0.10.0" end diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb new file mode 100644 index 0000000..c011553 --- /dev/null +++ b/gemfiles/Gemfile.common.rb @@ -0,0 +1,8 @@ +ENV['rails'] ||= '4.2.0' + +source 'https://rubygems.org' + +gem 'rails', "~> #{ENV['rails']}" +gem "doorkeeper" + +gemspec path: '../' diff --git a/gemfiles/Gemfile.mongo_mapper.rb b/gemfiles/Gemfile.mongo_mapper.rb new file mode 100644 index 0000000..223fc39 --- /dev/null +++ b/gemfiles/Gemfile.mongo_mapper.rb @@ -0,0 +1,5 @@ +gemfile = 'gemfiles/Gemfile.common.rb' +instance_eval IO.read(gemfile), gemfile + +gem 'mongo_mapper' +gem 'bson_ext' diff --git a/gemfiles/Gemfile.mongoid2.rb b/gemfiles/Gemfile.mongoid2.rb new file mode 100644 index 0000000..b766725 --- /dev/null +++ b/gemfiles/Gemfile.mongoid2.rb @@ -0,0 +1,5 @@ +gemfile = 'gemfiles/Gemfile.common.rb' +instance_eval IO.read(gemfile), gemfile + +gem 'mongoid', '~> 2' +gem 'bson_ext' diff --git a/gemfiles/Gemfile.mongoid3.rb b/gemfiles/Gemfile.mongoid3.rb new file mode 100644 index 0000000..c418b20 --- /dev/null +++ b/gemfiles/Gemfile.mongoid3.rb @@ -0,0 +1,4 @@ +gemfile = 'gemfiles/Gemfile.common.rb' +instance_eval IO.read(gemfile), gemfile + +gem 'mongoid', '~> 3' diff --git a/gemfiles/Gemfile.mongoid4.rb b/gemfiles/Gemfile.mongoid4.rb new file mode 100644 index 0000000..5088363 --- /dev/null +++ b/gemfiles/Gemfile.mongoid4.rb @@ -0,0 +1,4 @@ +gemfile = 'gemfiles/Gemfile.common.rb' +instance_eval IO.read(gemfile), gemfile + +gem 'mongoid', '~> 4' diff --git a/lib/doorkeeper-orms/version.rb b/lib/doorkeeper-orms/version.rb index 214f38f..1426607 100644 --- a/lib/doorkeeper-orms/version.rb +++ b/lib/doorkeeper-orms/version.rb @@ -1,3 +1,3 @@ module DoorkeeperOrms - VERSION = "0.0.1" + VERSION = "3.0.0" end diff --git a/lib/doorkeeper/orm/mongo_mapper.rb b/lib/doorkeeper/orm/mongo_mapper.rb index dba80c0..7522110 100644 --- a/lib/doorkeeper/orm/mongo_mapper.rb +++ b/lib/doorkeeper/orm/mongo_mapper.rb @@ -6,6 +6,14 @@ def self.initialize_models! require 'doorkeeper/orm/mongo_mapper/access_token' require 'doorkeeper/orm/mongo_mapper/application' end + + def self.initialize_application_owner! + require 'doorkeeper/models/concerns/ownership' + + Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership + end + + def self.check_requirements!(_config); end end end end diff --git a/lib/doorkeeper/orm/mongo_mapper/access_grant.rb b/lib/doorkeeper/orm/mongo_mapper/access_grant.rb index 81f0bfe..91e7800 100644 --- a/lib/doorkeeper/orm/mongo_mapper/access_grant.rb +++ b/lib/doorkeeper/orm/mongo_mapper/access_grant.rb @@ -1,6 +1,9 @@ module Doorkeeper class AccessGrant include MongoMapper::Document + + include AccessGrantMixin + safe timestamps! @@ -9,14 +12,10 @@ class AccessGrant key :resource_owner_id, ObjectId key :application_id, ObjectId key :token, String + key :scopes, String key :expires_in, Integer key :redirect_uri, String key :revoked_at, DateTime - key :scopes, String - - def scopes=(value) - write_attribute :scopes, value if value.present? - end def self.create_indexes ensure_index :token, unique: true diff --git a/lib/doorkeeper/orm/mongo_mapper/access_token.rb b/lib/doorkeeper/orm/mongo_mapper/access_token.rb index 481e335..603cec5 100644 --- a/lib/doorkeeper/orm/mongo_mapper/access_token.rb +++ b/lib/doorkeeper/orm/mongo_mapper/access_token.rb @@ -1,21 +1,22 @@ module Doorkeeper class AccessToken include MongoMapper::Document + + include AccessTokenMixin + safe timestamps! set_collection_name 'oauth_access_tokens' key :resource_owner_id, ObjectId + key :application_id, ObjectId key :token, String + key :refresh_token, String key :expires_in, Integer key :revoked_at, DateTime key :scopes, String - def scopes=(value) - write_attribute :scopes, value if value.present? - end - def self.last self.sort(:created_at).last end @@ -26,23 +27,17 @@ def self.delete_all_for(application_id, resource_owner) end private_class_method :delete_all_for - def self.last_authorized_token_for(application_id, resource_owner_id) - where(application_id: application_id, - resource_owner_id: resource_owner_id, - revoked_at: nil). - sort(:created_at.desc). - limit(1). - first + def self.create_indexes + ensure_index :token, unique: true + ensure_index [[:refresh_token, 1]], unique: true, sparse: true end - private_class_method :last_authorized_token_for - def refresh_token - self[:refresh_token] + def self.order_method + :sort end - def self.create_indexes - ensure_index :token, unique: true - ensure_index [[:refresh_token, 1]], unique: true, sparse: true + def self.created_at_desc + :created_at.desc end end end diff --git a/lib/doorkeeper/orm/mongo_mapper/application.rb b/lib/doorkeeper/orm/mongo_mapper/application.rb index 4e97e99..96cb9ef 100644 --- a/lib/doorkeeper/orm/mongo_mapper/application.rb +++ b/lib/doorkeeper/orm/mongo_mapper/application.rb @@ -1,6 +1,9 @@ module Doorkeeper class Application include MongoMapper::Document + + include ApplicationMixin + safe timestamps! @@ -14,12 +17,11 @@ class Application key :redirect_uri, String key :scopes, String - def scopes=(value) - write_attribute :scopes, value if value.present? - end - def self.authorized_for(resource_owner) - ids = AccessToken.where(resource_owner_id: resource_owner.id, revoked_at: nil).map(&:application_id) + ids = AccessToken.where( + resource_owner_id: resource_owner.id, + revoked_at: nil + ).map(&:application_id) find(ids) end diff --git a/lib/doorkeeper/orm/mongoid2.rb b/lib/doorkeeper/orm/mongoid2.rb index 43638d7..94d2b4b 100644 --- a/lib/doorkeeper/orm/mongoid2.rb +++ b/lib/doorkeeper/orm/mongoid2.rb @@ -6,6 +6,14 @@ def self.initialize_models! require 'doorkeeper/orm/mongoid2/access_token' require 'doorkeeper/orm/mongoid2/application' end + + def self.initialize_application_owner! + require 'doorkeeper/models/concerns/ownership' + + Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership + end + + def self.check_requirements!(_config); end end end end diff --git a/lib/doorkeeper/orm/mongoid2/access_grant.rb b/lib/doorkeeper/orm/mongoid2/access_grant.rb index b787417..2ac255e 100644 --- a/lib/doorkeeper/orm/mongoid2/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid2/access_grant.rb @@ -4,12 +4,14 @@ module Doorkeeper class AccessGrant include Mongoid::Document include Mongoid::Timestamps + + include AccessGrantMixin include Models::Mongoid2::Scopes self.store_in :oauth_access_grants field :resource_owner_id, type: Integer - field :application_id, type: Hash + field :application_id, type: BSON::ObjectId field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid2/access_token.rb b/lib/doorkeeper/orm/mongoid2/access_token.rb index 9d7300b..ee53cf3 100644 --- a/lib/doorkeeper/orm/mongoid2/access_token.rb +++ b/lib/doorkeeper/orm/mongoid2/access_token.rb @@ -4,12 +4,16 @@ module Doorkeeper class AccessToken include Mongoid::Document include Mongoid::Timestamps + + include AccessTokenMixin include Models::Mongoid2::Scopes self.store_in :oauth_access_tokens field :resource_owner_id, type: Integer + field :application_id, type: BSON::ObjectId field :token, type: String + field :refresh_token, type: String field :expires_in, type: Integer field :revoked_at, type: DateTime @@ -22,18 +26,12 @@ def self.delete_all_for(application_id, resource_owner) end private_class_method :delete_all_for - def self.last_authorized_token_for(application_id, resource_owner_id) - where(application_id: application_id, - resource_owner_id: resource_owner_id, - revoked_at: nil). - order_by([:created_at, :desc]). - limit(1). - first + def self.order_method + :order_by end - private_class_method :last_authorized_token_for - def refresh_token - self[:refresh_token] + def self.created_at_desc + [:created_at, :desc] end end end diff --git a/lib/doorkeeper/orm/mongoid2/application.rb b/lib/doorkeeper/orm/mongoid2/application.rb index 2dfa773..02cf0fc 100644 --- a/lib/doorkeeper/orm/mongoid2/application.rb +++ b/lib/doorkeeper/orm/mongoid2/application.rb @@ -2,6 +2,9 @@ module Doorkeeper class Application include Mongoid::Document include Mongoid::Timestamps + include Models::Mongoid2::Scopes + + include ApplicationMixin self.store_in :oauth_applications diff --git a/lib/doorkeeper/orm/mongoid2/concerns/scopes.rb b/lib/doorkeeper/orm/mongoid2/concerns/scopes.rb index c0fbf12..d98a7b2 100644 --- a/lib/doorkeeper/orm/mongoid2/concerns/scopes.rb +++ b/lib/doorkeeper/orm/mongoid2/concerns/scopes.rb @@ -4,8 +4,9 @@ module Mongoid2 module Scopes extend ActiveSupport::Concern - # It's strange that if not define these after included will raise error in Mongoid 2 and 3, but 4 works well - # see: https://travis-ci.org/jasl/doorkeeper/builds/31586902 + # It's strange that if not define these after included will raise error + # in Mongoid 2 and 3, but 4 works well see: + # https://travis-ci.org/jasl/doorkeeper/builds/31586902 included do def scopes OAuth::Scopes.from_string(self[:scopes]) diff --git a/lib/doorkeeper/orm/mongoid3.rb b/lib/doorkeeper/orm/mongoid3.rb index fc1c1bf..79a4735 100644 --- a/lib/doorkeeper/orm/mongoid3.rb +++ b/lib/doorkeeper/orm/mongoid3.rb @@ -6,6 +6,14 @@ def self.initialize_models! require 'doorkeeper/orm/mongoid3/access_token' require 'doorkeeper/orm/mongoid3/application' end + + def self.initialize_application_owner! + require 'doorkeeper/models/concerns/ownership' + + Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership + end + + def self.check_requirements!(_config); end end end end diff --git a/lib/doorkeeper/orm/mongoid3/access_grant.rb b/lib/doorkeeper/orm/mongoid3/access_grant.rb index 0add2d0..f587bb1 100644 --- a/lib/doorkeeper/orm/mongoid3/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid3/access_grant.rb @@ -4,12 +4,14 @@ module Doorkeeper class AccessGrant include Mongoid::Document include Mongoid::Timestamps + + include AccessGrantMixin include Models::Mongoid3::Scopes self.store_in collection: :oauth_access_grants field :resource_owner_id, type: Moped::BSON::ObjectId - field :application_id, type: Hash + field :application_id, type: Moped::BSON::ObjectId field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid3/access_token.rb b/lib/doorkeeper/orm/mongoid3/access_token.rb index 9c54182..7ef6ff1 100644 --- a/lib/doorkeeper/orm/mongoid3/access_token.rb +++ b/lib/doorkeeper/orm/mongoid3/access_token.rb @@ -4,12 +4,16 @@ module Doorkeeper class AccessToken include Mongoid::Document include Mongoid::Timestamps + + include AccessTokenMixin include Models::Mongoid3::Scopes self.store_in collection: :oauth_access_tokens field :resource_owner_id, type: Moped::BSON::ObjectId + field :application_id, type: Moped::BSON::ObjectId field :token, type: String + field :refresh_token, type: String field :expires_in, type: Integer field :revoked_at, type: DateTime @@ -22,18 +26,12 @@ def self.delete_all_for(application_id, resource_owner) end private_class_method :delete_all_for - def self.last_authorized_token_for(application_id, resource_owner_id) - where(application_id: application_id, - resource_owner_id: resource_owner_id, - revoked_at: nil). - order_by([:created_at, :desc]). - limit(1). - first + def self.order_method + :order_by end - private_class_method :last_authorized_token_for - def refresh_token - self[:refresh_token] + def self.created_at_desc + [:created_at, :desc] end end end diff --git a/lib/doorkeeper/orm/mongoid3/application.rb b/lib/doorkeeper/orm/mongoid3/application.rb index 2441278..d5ffaef 100644 --- a/lib/doorkeeper/orm/mongoid3/application.rb +++ b/lib/doorkeeper/orm/mongoid3/application.rb @@ -2,6 +2,9 @@ module Doorkeeper class Application include Mongoid::Document include Mongoid::Timestamps + include Models::Mongoid3::Scopes + + include ApplicationMixin self.store_in collection: :oauth_applications diff --git a/lib/doorkeeper/orm/mongoid3/concerns/scopes.rb b/lib/doorkeeper/orm/mongoid3/concerns/scopes.rb index 2cad094..269d998 100644 --- a/lib/doorkeeper/orm/mongoid3/concerns/scopes.rb +++ b/lib/doorkeeper/orm/mongoid3/concerns/scopes.rb @@ -4,6 +4,9 @@ module Mongoid3 module Scopes extend ActiveSupport::Concern + # It's strange that if not define these after included will raise error + # in Mongoid 2 and 3, but 4 works well see: + # https://travis-ci.org/jasl/doorkeeper/builds/31586902 included do def scopes OAuth::Scopes.from_string(self[:scopes]) @@ -16,7 +19,7 @@ def scopes_string def includes_scope?(*required_scopes) required_scopes.blank? || required_scopes.any? { |s| scopes.exists?(s.to_s) } end - + def scopes=(value) write_attribute :scopes, value if value.present? end diff --git a/lib/doorkeeper/orm/mongoid4.rb b/lib/doorkeeper/orm/mongoid4.rb index 1c9d7fd..27d7e28 100644 --- a/lib/doorkeeper/orm/mongoid4.rb +++ b/lib/doorkeeper/orm/mongoid4.rb @@ -6,6 +6,14 @@ def self.initialize_models! require 'doorkeeper/orm/mongoid4/access_token' require 'doorkeeper/orm/mongoid4/application' end + + def self.initialize_application_owner! + require 'doorkeeper/models/concerns/ownership' + + Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership + end + + def self.check_requirements!(_config); end end end end diff --git a/lib/doorkeeper/orm/mongoid4/access_grant.rb b/lib/doorkeeper/orm/mongoid4/access_grant.rb index 01b9d1e..a2014d2 100644 --- a/lib/doorkeeper/orm/mongoid4/access_grant.rb +++ b/lib/doorkeeper/orm/mongoid4/access_grant.rb @@ -4,12 +4,14 @@ module Doorkeeper class AccessGrant include Mongoid::Document include Mongoid::Timestamps + + include AccessGrantMixin include Models::Mongoid4::Scopes self.store_in collection: :oauth_access_grants field :resource_owner_id, type: BSON::ObjectId - field :application_id, type: Hash + field :application_id, type: BSON::ObjectId field :token, type: String field :expires_in, type: Integer field :redirect_uri, type: String diff --git a/lib/doorkeeper/orm/mongoid4/access_token.rb b/lib/doorkeeper/orm/mongoid4/access_token.rb index 6906061..6d9c385 100644 --- a/lib/doorkeeper/orm/mongoid4/access_token.rb +++ b/lib/doorkeeper/orm/mongoid4/access_token.rb @@ -4,12 +4,16 @@ module Doorkeeper class AccessToken include Mongoid::Document include Mongoid::Timestamps + + include AccessTokenMixin include Models::Mongoid4::Scopes self.store_in collection: :oauth_access_tokens field :resource_owner_id, type: BSON::ObjectId + field :application_id, type: BSON::ObjectId field :token, type: String + field :refresh_token, type: String field :expires_in, type: Integer field :revoked_at, type: DateTime @@ -22,18 +26,12 @@ def self.delete_all_for(application_id, resource_owner) end private_class_method :delete_all_for - def self.last_authorized_token_for(application_id, resource_owner_id) - where(application_id: application_id, - resource_owner_id: resource_owner_id, - revoked_at: nil). - order_by([:created_at, :desc]). - limit(1). - first + def self.order_method + :order_by end - private_class_method :last_authorized_token_for - def refresh_token - self[:refresh_token] + def self.created_at_desc + [:created_at, :desc] end end end diff --git a/lib/doorkeeper/orm/mongoid4/application.rb b/lib/doorkeeper/orm/mongoid4/application.rb index 2441278..18b1cff 100644 --- a/lib/doorkeeper/orm/mongoid4/application.rb +++ b/lib/doorkeeper/orm/mongoid4/application.rb @@ -2,6 +2,9 @@ module Doorkeeper class Application include Mongoid::Document include Mongoid::Timestamps + include Models::Mongoid4::Scopes + + include ApplicationMixin self.store_in collection: :oauth_applications diff --git a/spec/dummy/Rakefile b/spec/dummy/Rakefile deleted file mode 100644 index 3645852..0000000 --- a/spec/dummy/Rakefile +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env rake -# Add your own tasks in files placed in lib/tasks ending in .rake, -# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. - -require File.expand_path('../config/application', __FILE__) - -Dummy::Application.load_tasks diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb deleted file mode 100644 index e8065d9..0000000 --- a/spec/dummy/app/controllers/application_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ApplicationController < ActionController::Base - protect_from_forgery -end diff --git a/spec/dummy/app/controllers/custom_authorizations_controller.rb b/spec/dummy/app/controllers/custom_authorizations_controller.rb deleted file mode 100644 index b490d27..0000000 --- a/spec/dummy/app/controllers/custom_authorizations_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class CustomAuthorizationsController < ::ApplicationController - %w(index show new create edit update destroy).each do |action| - define_method action do - render nothing: true - end - end -end diff --git a/spec/dummy/app/controllers/full_protected_resources_controller.rb b/spec/dummy/app/controllers/full_protected_resources_controller.rb deleted file mode 100644 index 1e23568..0000000 --- a/spec/dummy/app/controllers/full_protected_resources_controller.rb +++ /dev/null @@ -1,12 +0,0 @@ -class FullProtectedResourcesController < ApplicationController - before_filter -> { doorkeeper_authorize! :admin }, only: :show - before_filter :doorkeeper_authorize!, only: :index - - def index - render text: 'index' - end - - def show - render text: 'show' - end -end diff --git a/spec/dummy/app/controllers/home_controller.rb b/spec/dummy/app/controllers/home_controller.rb deleted file mode 100644 index 6b05eb8..0000000 --- a/spec/dummy/app/controllers/home_controller.rb +++ /dev/null @@ -1,17 +0,0 @@ -class HomeController < ApplicationController - def index - end - - def sign_in - session[:user_id] = if Rails.env.development? - User.first || User.create!(name: 'Joe', password: 'sekret') - else - User.first - end - redirect_to '/' - end - - def callback - render text: 'ok' - end -end diff --git a/spec/dummy/app/controllers/metal_controller.rb b/spec/dummy/app/controllers/metal_controller.rb deleted file mode 100644 index f52a512..0000000 --- a/spec/dummy/app/controllers/metal_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -class MetalController < ActionController::Metal - include AbstractController::Callbacks - include ActionController::Head - include Doorkeeper::Rails::Helpers - - before_filter :doorkeeper_authorize! - - def index - self.response_body = { ok: true }.to_json - end -end diff --git a/spec/dummy/app/controllers/semi_protected_resources_controller.rb b/spec/dummy/app/controllers/semi_protected_resources_controller.rb deleted file mode 100644 index 5886799..0000000 --- a/spec/dummy/app/controllers/semi_protected_resources_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -class SemiProtectedResourcesController < ApplicationController - before_filter :doorkeeper_authorize!, only: :index - - def index - render text: 'protected index' - end - - def show - render text: 'non protected show' - end -end diff --git a/spec/dummy/app/helpers/application_helper.rb b/spec/dummy/app/helpers/application_helper.rb deleted file mode 100644 index 2dbb236..0000000 --- a/spec/dummy/app/helpers/application_helper.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ApplicationHelper - def current_user - @current_user ||= User.find_by_id(session[:user_id]) - end -end diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb deleted file mode 100644 index c206725..0000000 --- a/spec/dummy/app/models/user.rb +++ /dev/null @@ -1,28 +0,0 @@ -case DOORKEEPER_ORM -when :mongoid2, :mongoid3, :mongoid4 - class User - include Mongoid::Document - include Mongoid::Timestamps - - field :name, type: String - field :password, type: String - end -when :mongo_mapper - class User - include MongoMapper::Document - timestamps! - - key :name, String - key :password, String - end -end - -class User - if ::Rails.version.to_i < 4 - attr_accessible :name, :password - end - - def self.authenticate!(name, password) - User.where(name: name, password: password).first - end -end diff --git a/spec/dummy/app/views/home/index.html.erb b/spec/dummy/app/views/home/index.html.erb deleted file mode 100644 index e69de29..0000000 diff --git a/spec/dummy/app/views/layouts/application.html.erb b/spec/dummy/app/views/layouts/application.html.erb deleted file mode 100644 index 8988b5d..0000000 --- a/spec/dummy/app/views/layouts/application.html.erb +++ /dev/null @@ -1,14 +0,0 @@ - - - - Dummy - <%= csrf_meta_tags %> - - - -<%= link_to "Sign in", '/sign_in' %> - -<%= yield %> - - - diff --git a/spec/dummy/config.ru b/spec/dummy/config.ru deleted file mode 100644 index 1989ed8..0000000 --- a/spec/dummy/config.ru +++ /dev/null @@ -1,4 +0,0 @@ -# This file is used by Rack-based servers to start the application. - -require ::File.expand_path('../config/environment', __FILE__) -run Dummy::Application diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb deleted file mode 100644 index 91578f8..0000000 --- a/spec/dummy/config/application.rb +++ /dev/null @@ -1,54 +0,0 @@ -require File.expand_path('../boot', __FILE__) - -require 'action_controller/railtie' -require 'sprockets/railtie' - -Bundler.require :default - -orm = if [:mongoid2, :mongoid3, :mongoid4].include?(DOORKEEPER_ORM) - Mongoid.load!(File.join(File.dirname(File.expand_path(__FILE__)), "#{DOORKEEPER_ORM}.yml")) - :mongoid - else - DOORKEEPER_ORM - end - -require "#{orm}/railtie" - -module Dummy - class Application < Rails::Application - # Settings in config/environments/* take precedence over those specified here. - # Application configuration should go into files in config/initializers - # -- all .rb files in that directory are automatically loaded. - - # Only load the plugins named here, in the order given (default is alphabetical). - # :all can be used as a placeholder for all plugins not explicitly named. - # config.plugins = [ :exception_notification, :ssl_requirement, :all ] - - # Activate observers that should always be running. - # config.active_record.observers = :cacher, :garbage_collector, :forum_observer - - if defined?(ActiveRecord) && Rails.version.to_i < 4 - config.active_record.whitelist_attributes = true - end - - # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. - # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. - # config.time_zone = 'Central Time (US & Canada)' - - # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. - config.i18n.load_path += Dir[Rails.root.join('../../', 'config/locales', '*.{rb,yml}').to_s] - # config.i18n.default_locale = :en - - # Configure the default encoding used in templates for Ruby 1.9. - config.encoding = 'utf-8' - - # Configure sensitive parameters which will be filtered from the log file. - config.filter_parameters += [:password] - - # Enable the asset pipeline - config.assets.enabled = true - - # Version of your assets, change this if you want to expire all your assets - config.assets.version = '1.0' - end -end diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb deleted file mode 100644 index d0d2cdd..0000000 --- a/spec/dummy/config/boot.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'rubygems' -require 'bundler/setup' - -DOORKEEPER_ORM = (ENV['orm'] || 'mongoid').to_sym unless defined?(DOORKEEPER_ORM) - -$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/spec/dummy/config/environment.rb b/spec/dummy/config/environment.rb deleted file mode 100644 index 3da5eb9..0000000 --- a/spec/dummy/config/environment.rb +++ /dev/null @@ -1,5 +0,0 @@ -# Load the rails application -require File.expand_path('../application', __FILE__) - -# Initialize the rails application -Dummy::Application.initialize! diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb deleted file mode 100644 index df00f8a..0000000 --- a/spec/dummy/config/environments/development.rb +++ /dev/null @@ -1,29 +0,0 @@ -Dummy::Application.configure do - # Settings specified here will take precedence over those in config/application.rb - - # In the development environment your application's code is reloaded on - # every request. This slows down response time but is perfect for development - # since you don't have to restart the web server when you make code changes. - config.cache_classes = false - - # Show full error reports and disable caching - config.consider_all_requests_local = true - config.action_controller.perform_caching = false - - # Don't care if the mailer can't send - # config.action_mailer.raise_delivery_errors = false - - # Print deprecation notices to the Rails logger - config.active_support.deprecation = :log - - # Only use best-standards-support built into browsers - config.action_dispatch.best_standards_support = :builtin - - # Do not compress assets - config.assets.compress = false - - # Expands the lines which load the assets - config.assets.debug = true - - config.eager_load = false -end diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb deleted file mode 100644 index 5c18740..0000000 --- a/spec/dummy/config/environments/production.rb +++ /dev/null @@ -1,62 +0,0 @@ -Dummy::Application.configure do - # Settings specified here will take precedence over those in config/application.rb - - # Code is not reloaded between requests - config.cache_classes = true - - # Full error reports are disabled and caching is turned on - config.consider_all_requests_local = false - config.action_controller.perform_caching = true - - # Disable Rails's static asset server (Apache or nginx will already do this) - config.serve_static_assets = false - - # Compress JavaScripts and CSS - config.assets.compress = true - - # Don't fallback to assets pipeline if a precompiled asset is missed - config.assets.compile = false - - # Generate digests for assets URLs - config.assets.digest = true - - # Defaults to Rails.root.join("public/assets") - # config.assets.manifest = YOUR_PATH - - # Specifies the header that your server uses for sending files - # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache - # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx - - # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. - # config.force_ssl = true - - # See everything in the log (default is :info) - # config.log_level = :debug - - # Use a different logger for distributed setups - # config.logger = SyslogLogger.new - - # Use a different cache store in production - # config.cache_store = :mem_cache_store - - # Enable serving of images, stylesheets, and JavaScripts from an asset server - # config.action_controller.asset_host = "http://assets.example.com" - - # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) - # config.assets.precompile += %w( search.js ) - - # Disable delivery errors, bad email addresses will be ignored - # config.action_mailer.raise_delivery_errors = false - - # Enable threaded mode - # config.threadsafe! - - # Enable locale fallbacks for I18n (makes lookups for any locale fall back to - # the I18n.default_locale when a translation can not be found) - config.i18n.fallbacks = true - - # Send deprecation notices to registered listeners - config.active_support.deprecation = :notify - - config.eager_load = true -end diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb deleted file mode 100644 index b8890bd..0000000 --- a/spec/dummy/config/environments/test.rb +++ /dev/null @@ -1,51 +0,0 @@ -Dummy::Application.configure do - # Settings specified here will take precedence over those in config/application.rb - - # The test environment is used exclusively to run your application's - # test suite. You never need to work with it otherwise. Remember that - # your test database is "scratch space" for the test suite and is wiped - # and recreated between test runs. Don't rely on the data there! - config.cache_classes = true - - # Configure static asset server for tests with Cache-Control for performance - config.serve_static_assets = true - config.static_cache_control = 'public, max-age=3600' - - if Rails.version.to_i < 4 - # Log error messages when you accidentally call methods on nil - config.whiny_nils = true - end - - if Rails.version.to_i >= 4 - # Do not eager load code on boot. This avoids loading your whole application - # just for the purpose of running a single test. If you are using a tool that - # preloads Rails for running tests, you may have to set it to true. - config.eager_load = false - config.i18n.enforce_available_locales = true - end - - # Show full error reports and disable caching - config.consider_all_requests_local = true - config.action_controller.perform_caching = false - - # Raise exceptions instead of rendering exception templates - config.action_dispatch.show_exceptions = false - - # Disable request forgery protection in test environment - config.action_controller.allow_forgery_protection = false - - # Tell Action Mailer not to deliver emails to the real world. - # The :test delivery method accumulates sent emails in the - # ActionMailer::Base.deliveries array. - # config.action_mailer.delivery_method = :test - - # Use SQL instead of Active Record's schema dumper when creating the test database. - # This is necessary if your schema can't be completely dumped by the schema dumper, - # like if you have constraints or database-specific column types - # config.active_record.schema_format = :sql - - # Print deprecation notices to the stderr - config.active_support.deprecation = :stderr - - config.eager_load = true -end diff --git a/spec/dummy/config/initializers/backtrace_silencers.rb b/spec/dummy/config/initializers/backtrace_silencers.rb deleted file mode 100644 index 59385cd..0000000 --- a/spec/dummy/config/initializers/backtrace_silencers.rb +++ /dev/null @@ -1,7 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. -# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } - -# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. -# Rails.backtrace_cleaner.remove_silencers! diff --git a/spec/dummy/config/initializers/doorkeeper.rb b/spec/dummy/config/initializers/doorkeeper.rb deleted file mode 100644 index cc93511..0000000 --- a/spec/dummy/config/initializers/doorkeeper.rb +++ /dev/null @@ -1,58 +0,0 @@ -Doorkeeper.configure do - # Change the ORM that doorkeeper will use - orm DOORKEEPER_ORM - - # This block will be called to check whether the - # resource owner is authenticated or not - resource_owner_authenticator do - # Put your resource owner authentication logic here. - # e.g. User.find_by_id(session[:user_id]) || redirect_to(new_user_session_url) - User.where(id: session[:user_id]).first || redirect_to(root_url, alert: 'Needs sign in.') - end - - # If you want to restrict the access to the web interface for - # adding oauth authorized applications you need to declare the - # block below - # admin_authenticator do - # # Put your admin authentication logic here. - # Admin.find_by_id(session[:admin_id]) || redirect_to(new_admin_session_url) - # end - - # Authorization Code expiration time (default 10 minutes). - # access_token_expires_in 10.minutes - - # Access token expiration time (default 2 hours) - # If you want to disable expiration, set this to nil. - # access_token_expires_in 2.hours - - # Issue access tokens with refresh token (disabled by default) - use_refresh_token - - # Define access token scopes for your provider - # For more information go to - # https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes - default_scopes :public - optional_scopes :write, :update - - # Change the way client credentials are retrieved from the request object. - # By default it retrieves first from `HTTP_AUTHORIZATION` header and - # fallsback to `:client_id` and `:client_secret` from `params` object - # Check out the wiki for mor information on customization - # client_credentials :from_basic, :from_params - - # Change the way access token is authenticated from the request object. - # By default it retrieves first from `HTTP_AUTHORIZATION` header and - # fallsback to `:access_token` or `:bearer_token` from `params` object - # Check out the wiki for mor information on customization - # access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param - - # Change the native redirect uri for client apps - # When clients register with the following redirect uri, they won't be redirected to any server and the authorization code will be displayed within the provider - # The value can be any string. Use nil to disable this feature. When disabled, clients must provide a valid URL - # (Similar behaviour: https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi) - # - # native_redirect_uri 'urn:ietf:wg:oauth:2.0:oob' - - # WWW-Authenticate Realm (default 'Doorkeeper'). - realm 'Doorkeeper' -end diff --git a/spec/dummy/config/initializers/secret_token.rb b/spec/dummy/config/initializers/secret_token.rb deleted file mode 100644 index 3d2daab..0000000 --- a/spec/dummy/config/initializers/secret_token.rb +++ /dev/null @@ -1,9 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Your secret key for verifying the integrity of signed cookies. -# If you change this key, all old signed cookies will become invalid! -# Make sure the secret is at least 30 characters and all random, -# no regular words or you'll be exposed to dictionary attacks. -Dummy::Application.config.secret_key_base = - Dummy::Application.config.secret_token = - 'c00157b5a1bb6181792f0f4a8a080485de7bab9987e6cf159dc74c4f0573345c1bfa713b5d756e1491fc0b098567e8a619e2f8d268eda86a20a720d05d633780' diff --git a/spec/dummy/config/initializers/session_store.rb b/spec/dummy/config/initializers/session_store.rb deleted file mode 100644 index 952473f..0000000 --- a/spec/dummy/config/initializers/session_store.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Be sure to restart your server when you modify this file. - -Dummy::Application.config.session_store :cookie_store, key: '_dummy_session' - -# Use the database for sessions instead of the cookie-based default, -# which shouldn't be used to store highly confidential information -# (create the session table with "rails generate session_migration") -# Dummy::Application.config.session_store :active_record_store diff --git a/spec/dummy/config/initializers/wrap_parameters.rb b/spec/dummy/config/initializers/wrap_parameters.rb deleted file mode 100644 index 999df20..0000000 --- a/spec/dummy/config/initializers/wrap_parameters.rb +++ /dev/null @@ -1,14 +0,0 @@ -# Be sure to restart your server when you modify this file. -# -# This file contains settings for ActionController::ParamsWrapper which -# is enabled by default. - -# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. -ActiveSupport.on_load(:action_controller) do - wrap_parameters format: [:json] -end - -# Disable root element in JSON by default. -ActiveSupport.on_load(:active_record) do - self.include_root_in_json = false -end diff --git a/spec/dummy/config/locales/doorkeeper.en.yml b/spec/dummy/config/locales/doorkeeper.en.yml deleted file mode 100644 index 60527e6..0000000 --- a/spec/dummy/config/locales/doorkeeper.en.yml +++ /dev/null @@ -1,5 +0,0 @@ -en: - doorkeeper: - scopes: - public: "Access your public data" - write: "Update your data" diff --git a/spec/dummy/config/mongo.yml b/spec/dummy/config/mongo.yml deleted file mode 100644 index 4fa128b..0000000 --- a/spec/dummy/config/mongo.yml +++ /dev/null @@ -1,11 +0,0 @@ -defaults: &defaults - host: 127.0.0.1 - port: 27017 - -development: - <<: *defaults - database: doorkeeper-mongomapper-development - -test: - <<: *defaults - database: doorkeeper-mongomapper-test-suite diff --git a/spec/dummy/config/mongoid2.yml b/spec/dummy/config/mongoid2.yml deleted file mode 100644 index 93c7c05..0000000 --- a/spec/dummy/config/mongoid2.yml +++ /dev/null @@ -1,9 +0,0 @@ -development: - database: doorkeeper-development - persist_in_safe_mode: true - autocreate_indexes: true - -test: - database: doorkeeper-mongoid2-test - persist_in_safe_mode: true - autocreate_indexes: true diff --git a/spec/dummy/config/mongoid3.yml b/spec/dummy/config/mongoid3.yml deleted file mode 100644 index 6f8304c..0000000 --- a/spec/dummy/config/mongoid3.yml +++ /dev/null @@ -1,18 +0,0 @@ -development: - sessions: - default: - database: doorkeeper-mongoid3-development - hosts: - - localhost:27017 - options: - consistency: :strong - safe: true -test: - sessions: - default: - database: doorkeeper-mongoid3-test - hosts: - - localhost:27017 - options: - consistency: :strong - safe: true diff --git a/spec/dummy/config/mongoid4.yml b/spec/dummy/config/mongoid4.yml deleted file mode 100644 index dd06d0d..0000000 --- a/spec/dummy/config/mongoid4.yml +++ /dev/null @@ -1,19 +0,0 @@ -development: - sessions: - default: - database: doorkeeper-mongoid4-development - hosts: - - localhost:27017 - options: - write: - w: 1 - -test: - sessions: - default: - database: doorkeeper-mongoid4-test - hosts: - - localhost:27017 - options: - write: - w: 1 diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb deleted file mode 100644 index ada394a..0000000 --- a/spec/dummy/config/routes.rb +++ /dev/null @@ -1,52 +0,0 @@ -Rails.application.routes.draw do - use_doorkeeper - use_doorkeeper scope: 'scope' - - scope 'inner_space' do - use_doorkeeper scope: 'scope' do - controllers authorizations: 'custom_authorizations', - tokens: 'custom_authorizations', - applications: 'custom_authorizations', - token_info: 'custom_authorizations' - - as authorizations: 'custom_auth', - tokens: 'custom_token', - token_info: 'custom_token_info' - end - end - - scope 'space' do - use_doorkeeper do - controllers authorizations: 'custom_authorizations', - tokens: 'custom_authorizations', - applications: 'custom_authorizations', - token_info: 'custom_authorizations' - - as authorizations: 'custom_auth', - tokens: 'custom_token', - token_info: 'custom_token_info' - end - end - - scope 'outer_space' do - use_doorkeeper do - controllers authorizations: 'custom_authorizations', - tokens: 'custom_authorizations', - token_info: 'custom_authorizations' - - as authorizations: 'custom_auth', - tokens: 'custom_token', - token_info: 'custom_token_info' - - skip_controllers :tokens, :applications, :token_info - end - end - - get 'metal.json' => 'metal#index' - - get '/callback', to: 'home#callback' - get '/sign_in', to: 'home#sign_in' - resources :semi_protected_resources - resources :full_protected_resources - root to: 'home#index' -end diff --git a/spec/dummy/public/404.html b/spec/dummy/public/404.html deleted file mode 100644 index 9a48320..0000000 --- a/spec/dummy/public/404.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - The page you were looking for doesn't exist (404) - - - - - -
-

The page you were looking for doesn't exist.

-

You may have mistyped the address or the page may have moved.

-
- - diff --git a/spec/dummy/public/422.html b/spec/dummy/public/422.html deleted file mode 100644 index 83660ab..0000000 --- a/spec/dummy/public/422.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - The change you wanted was rejected (422) - - - - - -
-

The change you wanted was rejected.

-

Maybe you tried to change something you didn't have access to.

-
- - diff --git a/spec/dummy/public/500.html b/spec/dummy/public/500.html deleted file mode 100644 index b80307f..0000000 --- a/spec/dummy/public/500.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - We're sorry, but something went wrong (500) - - - - - -
-

We're sorry, but something went wrong.

-

We've been notified about this issue and we'll take a look at it shortly.

-
- - diff --git a/spec/dummy/public/favicon.ico b/spec/dummy/public/favicon.ico deleted file mode 100644 index e69de29..0000000 diff --git a/spec/dummy/script/rails b/spec/dummy/script/rails deleted file mode 100755 index f8da2cf..0000000 --- a/spec/dummy/script/rails +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby -# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. - -APP_PATH = File.expand_path('../../config/application', __FILE__) -require File.expand_path('../../config/boot', __FILE__) -require 'rails/commands' diff --git a/spec/factories/access_grant.rb b/spec/factories/access_grant.rb deleted file mode 100644 index 2a5c9e6..0000000 --- a/spec/factories/access_grant.rb +++ /dev/null @@ -1,9 +0,0 @@ -FactoryGirl.define do - factory :access_grant, class: Doorkeeper::AccessGrant do - sequence(:resource_owner_id) { |n| n } - application - redirect_uri 'https://app.com/callback' - expires_in 100 - scopes 'public write' - end -end diff --git a/spec/factories/access_token.rb b/spec/factories/access_token.rb deleted file mode 100644 index 48cd6e5..0000000 --- a/spec/factories/access_token.rb +++ /dev/null @@ -1,11 +0,0 @@ -FactoryGirl.define do - factory :access_token, class: Doorkeeper::AccessToken do - sequence(:resource_owner_id) { |n| n } - application - expires_in 2.hours - - factory :clientless_access_token do - application nil - end - end -end diff --git a/spec/factories/application.rb b/spec/factories/application.rb deleted file mode 100644 index 749f2c0..0000000 --- a/spec/factories/application.rb +++ /dev/null @@ -1,6 +0,0 @@ -FactoryGirl.define do - factory :application, class: Doorkeeper::Application do - sequence(:name) { |n| "Application #{n}" } - redirect_uri 'https://app.com/callback' - end -end diff --git a/spec/models/doorkeeper/access_grant_spec.rb b/spec/models/doorkeeper/access_grant_spec.rb deleted file mode 100644 index 37b3351..0000000 --- a/spec/models/doorkeeper/access_grant_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'spec_helper_integration' - -describe Doorkeeper::AccessGrant do - subject { FactoryGirl.build(:access_grant) } - - it { should be_valid } - - it_behaves_like 'an accessible token' - it_behaves_like 'a revocable token' - it_behaves_like 'a unique token' do - let(:factory_name) { :access_grant } - end - - describe 'validations' do - it 'is invalid without resource_owner_id' do - subject.resource_owner_id = nil - should_not be_valid - end - - it 'is invalid without application_id' do - subject.application_id = nil - should_not be_valid - end - - it 'is invalid without token' do - subject.save - subject.token = nil - should_not be_valid - end - - it 'is invalid without expires_in' do - subject.expires_in = nil - should_not be_valid - end - end -end diff --git a/spec/models/doorkeeper/access_token_spec.rb b/spec/models/doorkeeper/access_token_spec.rb deleted file mode 100644 index 261b874..0000000 --- a/spec/models/doorkeeper/access_token_spec.rb +++ /dev/null @@ -1,226 +0,0 @@ -require 'spec_helper_integration' - -module Doorkeeper - describe AccessToken do - subject { FactoryGirl.build(:access_token) } - - it { should be_valid } - - it_behaves_like 'an accessible token' - it_behaves_like 'a revocable token' - it_behaves_like 'a unique token' do - let(:factory_name) { :access_token } - end - - describe :refresh_token do - it 'has empty refresh token if it was not required' do - token = FactoryGirl.create :access_token - expect(token.refresh_token).to be_nil - end - - it 'generates a refresh token if it was requested' do - token = FactoryGirl.create :access_token, use_refresh_token: true - expect(token.refresh_token).not_to be_nil - end - - it 'is not valid if token exists' do - token1 = FactoryGirl.create :access_token, use_refresh_token: true - token2 = FactoryGirl.create :access_token, use_refresh_token: true - token2.send :write_attribute, :refresh_token, token1.refresh_token - expect(token2).not_to be_valid - end - - it 'expects database to raise an error if refresh tokens are the same' do - token1 = FactoryGirl.create :access_token, use_refresh_token: true - token2 = FactoryGirl.create :access_token, use_refresh_token: true - expect do - token2.write_attribute :refresh_token, token1.refresh_token - token2.save(validate: false) - end.to raise_error - end - end - - describe 'validations' do - it 'is valid without resource_owner_id' do - # For client credentials flow - subject.resource_owner_id = nil - should be_valid - end - end - - describe '#same_credential?' do - - context 'with default parameters' do - - let(:resource_owner_id) { 100 } - let(:application) { FactoryGirl.create :application } - let(:default_attributes) do - { application: application, resource_owner_id: resource_owner_id } - end - let(:access_token1) { FactoryGirl.create :access_token, default_attributes } - - context 'the second token has the same owner and same app' do - let(:access_token2) { FactoryGirl.create :access_token, default_attributes } - it 'success' do - expect(access_token1.same_credential?(access_token2)).to be_truthy - end - end - - context 'the second token has same owner and different app' do - let(:other_application) { FactoryGirl.create :application } - let(:access_token2) { FactoryGirl.create :access_token, application: other_application, resource_owner_id: resource_owner_id } - - it 'fail' do - expect(access_token1.same_credential?(access_token2)).to be_falsey - end - end - - context 'the second token has different owner and different app' do - - let(:other_application) { FactoryGirl.create :application } - let(:access_token2) { FactoryGirl.create :access_token, application: other_application, resource_owner_id: 42 } - - it 'fail' do - expect(access_token1.same_credential?(access_token2)).to be_falsey - end - end - - context 'the second token has different owner and same app' do - let(:access_token2) { FactoryGirl.create :access_token, application: application, resource_owner_id: 42 } - - it 'fail' do - expect(access_token1.same_credential?(access_token2)).to be_falsey - end - end - end - end - - describe '#acceptable?' do - context 'a token that is not accessible' do - let(:token) { FactoryGirl.create(:access_token, created_at: 6.hours.ago) } - - it 'should return false' do - expect(token.acceptable?(nil)).to be false - end - end - - context 'a token that has the incorrect scopes' do - let(:token) { FactoryGirl.create(:access_token) } - - it 'should return false' do - expect(token.acceptable?(['public'])).to be false - end - end - - context 'a token is acceptable with the correct scopes' do - let(:token) do - token = FactoryGirl.create(:access_token) - token[:scopes] = 'public' - token - end - - it 'should return true' do - expect(token.acceptable?(['public'])).to be true - end - end - end - - describe '.revoke_all_for' do - let(:resource_owner) { double(id: 100) } - let(:application) { FactoryGirl.create :application } - let(:default_attributes) do - { application: application, resource_owner_id: resource_owner.id } - end - - it 'revokes all tokens for given application and resource owner' do - FactoryGirl.create :access_token, default_attributes - AccessToken.revoke_all_for application.id, resource_owner - AccessToken.all.each do |token| - expect(token).to be_revoked - end - end - - it 'matches application' do - FactoryGirl.create :access_token, default_attributes.merge(application: FactoryGirl.create(:application)) - AccessToken.revoke_all_for application.id, resource_owner - expect(AccessToken.all).not_to be_empty - end - - it 'matches resource owner' do - FactoryGirl.create :access_token, default_attributes.merge(resource_owner_id: 90) - AccessToken.revoke_all_for application.id, resource_owner - expect(AccessToken.all).not_to be_empty - end - end - - describe '.matching_token_for' do - let(:resource_owner_id) { 100 } - let(:application) { FactoryGirl.create :application } - let(:scopes) { Doorkeeper::OAuth::Scopes.from_string('public write') } - let(:default_attributes) do - { application: application, resource_owner_id: resource_owner_id, scopes: scopes.to_s } - end - - it 'returns only one token' do - token = FactoryGirl.create :access_token, default_attributes - last_token = AccessToken.matching_token_for(application, resource_owner_id, scopes) - expect(last_token).to eq(token) - end - - it 'accepts resource owner as object' do - resource_owner = double(to_key: true, id: 100) - token = FactoryGirl.create :access_token, default_attributes - last_token = AccessToken.matching_token_for(application, resource_owner, scopes) - expect(last_token).to eq(token) - end - - it 'accepts nil as resource owner' do - token = FactoryGirl.create :access_token, default_attributes.merge(resource_owner_id: nil) - last_token = AccessToken.matching_token_for(application, nil, scopes) - expect(last_token).to eq(token) - end - - it 'excludes revoked tokens' do - FactoryGirl.create :access_token, default_attributes.merge(revoked_at: 1.day.ago) - last_token = AccessToken.matching_token_for(application, resource_owner_id, scopes) - expect(last_token).to be_nil - end - - it 'matches the application' do - token = FactoryGirl.create :access_token, default_attributes.merge(application: FactoryGirl.create(:application)) - last_token = AccessToken.matching_token_for(application, resource_owner_id, scopes) - expect(last_token).to be_nil - end - - it 'matches the resource owner' do - FactoryGirl.create :access_token, default_attributes.merge(resource_owner_id: 2) - last_token = AccessToken.matching_token_for(application, resource_owner_id, scopes) - expect(last_token).to be_nil - end - - it 'matches the scopes' do - FactoryGirl.create :access_token, default_attributes.merge(scopes: 'public email') - last_token = AccessToken.matching_token_for(application, resource_owner_id, scopes) - expect(last_token).to be_nil - end - - it 'returns the last created token' do - FactoryGirl.create :access_token, default_attributes.merge(created_at: 1.day.ago) - token = FactoryGirl.create :access_token, default_attributes - last_token = AccessToken.matching_token_for(application, resource_owner_id, scopes) - expect(last_token).to eq(token) - end - - it 'returns as_json hash' do - token = FactoryGirl.create :access_token, default_attributes - token_hash = { resource_owner_id: token.resource_owner_id, - scopes: token.scopes, - expires_in_seconds: token.expires_in_seconds, - application: { uid: token.application.uid } - } - expect(token.as_json).to eq token_hash - end - end - - end -end diff --git a/spec/models/doorkeeper/application_spec.rb b/spec/models/doorkeeper/application_spec.rb deleted file mode 100644 index c4def9e..0000000 --- a/spec/models/doorkeeper/application_spec.rb +++ /dev/null @@ -1,177 +0,0 @@ -require 'spec_helper_integration' - -module Doorkeeper - describe Application do - include OrmHelper - - let(:require_owner) { Doorkeeper.configuration.instance_variable_set('@confirm_application_owner', true) } - let(:unset_require_owner) { Doorkeeper.configuration.instance_variable_set('@confirm_application_owner', false) } - let(:new_application) { FactoryGirl.build(:application) } - - let(:uid) { SecureRandom.hex(8) } - let(:secret) { SecureRandom.hex(8) } - - context 'application_owner is enabled' do - before do - Doorkeeper.configure do - orm DOORKEEPER_ORM - enable_application_owner - end - end - - context 'application owner is not required' do - before(:each) do - unset_require_owner - end - - it 'is valid given valid attributes' do - expect(new_application).to be_valid - end - end - - context 'application owner is required' do - before(:each) do - require_owner - @owner = mock_application_owner - end - - it 'is invalid without an owner' do - expect(new_application).not_to be_valid - end - - it 'is valid with an owner' do - new_application.owner = @owner - expect(new_application).to be_valid - end - end - end - - it 'is invalid without a name' do - new_application.name = nil - expect(new_application).not_to be_valid - end - - it 'generates uid on create' do - expect(new_application.uid).to be_nil - new_application.save - expect(new_application.uid).not_to be_nil - end - - it 'generates uid on create unless one is set' do - new_application.uid = uid - new_application.save - expect(new_application.uid).to eq(uid) - end - - it 'is invalid without uid' do - new_application.save - new_application.uid = nil - expect(new_application).not_to be_valid - end - - it 'is invalid without redirect_uri' do - new_application.save - new_application.redirect_uri = nil - expect(new_application).not_to be_valid - end - - it 'checks uniqueness of uid' do - app1 = FactoryGirl.create(:application) - app2 = FactoryGirl.create(:application) - app2.uid = app1.uid - expect(app2).not_to be_valid - end - - it 'expects database to throw an error when uids are the same' do - app1 = FactoryGirl.create(:application) - app2 = FactoryGirl.create(:application) - app2.uid = app1.uid - expect { app2.save!(validate: false) }.to raise_error - end - - it 'generate secret on create' do - expect(new_application.secret).to be_nil - new_application.save - expect(new_application.secret).not_to be_nil - end - - it 'generate secret on create unless one is set' do - new_application.secret = secret - new_application.save - expect(new_application.secret).to eq(secret) - end - - it 'is invalid without secret' do - new_application.save - new_application.secret = nil - expect(new_application).not_to be_valid - end - - describe 'destroy related models on cascade' do - before(:each) do - new_application.save - end - - it 'should destroy its access grants' do - FactoryGirl.create(:access_grant, application: new_application) - expect { new_application.destroy }.to change { Doorkeeper::AccessGrant.count }.by(-1) - end - - it 'should destroy its access tokens' do - FactoryGirl.create(:access_token, application: new_application) - FactoryGirl.create(:access_token, application: new_application, revoked_at: Time.now) - expect do - new_application.destroy - end.to change { Doorkeeper::AccessToken.count }.by(-2) - end - end - - describe :authorized_for do - let(:resource_owner) { double(:resource_owner, id: 10) } - - it 'is empty if the application is not authorized for anyone' do - expect(Application.authorized_for(resource_owner)).to be_empty - end - - it 'returns only application for a specific resource owner' do - FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id + 1) - token = FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id) - expect(Application.authorized_for(resource_owner)).to eq([token.application]) - end - - it 'excludes revoked tokens' do - FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id, revoked_at: 2.days.ago) - expect(Application.authorized_for(resource_owner)).to be_empty - end - - it 'returns all applications that have been authorized' do - token1 = FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id) - token2 = FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id) - expect(Application.authorized_for(resource_owner)).to eq([token1.application, token2.application]) - end - - it 'returns only one application even if it has been authorized twice' do - application = FactoryGirl.create(:application) - FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id, application: application) - FactoryGirl.create(:access_token, resource_owner_id: resource_owner.id, application: application) - expect(Application.authorized_for(resource_owner)).to eq([application]) - end - - it 'should fail to mass assign a new application', if: ::Rails::VERSION::MAJOR < 4 do - mass_assign = { name: 'Something', - redirect_uri: 'http://somewhere.com/something', - uid: 123, - secret: 'something' } - expect(Application.create(mass_assign).uid).not_to eq(123) - end - end - - describe :authenticate do - it 'finds the application via uid/secret' do - app = FactoryGirl.create :application - authenticated = Application.by_uid_and_secret(app.uid, app.secret) - expect(authenticated).to eq(app) - end - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb deleted file mode 100644 index 717cc18..0000000 --- a/spec/spec_helper.rb +++ /dev/null @@ -1 +0,0 @@ -$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '../lib')) \ No newline at end of file diff --git a/spec/spec_helper_integration.rb b/spec/spec_helper_integration.rb deleted file mode 100644 index 82e785d..0000000 --- a/spec/spec_helper_integration.rb +++ /dev/null @@ -1,39 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -DOORKEEPER_ORM = (ENV['orm'] || 'mongoid4').to_sym - -$LOAD_PATH.unshift File.dirname(__FILE__) - -require 'dummy/config/environment' -require 'rspec/rails' -require 'rspec/autorun' -require 'timecop' -require 'database_cleaner' - -Rails.logger.info "====> Doorkeeper.orm = #{Doorkeeper.configuration.orm.inspect}" -Rails.logger.info "====> Rails version: #{Rails.version}" -Rails.logger.info "====> Ruby version: #{RUBY_VERSION}" - -orm_name = Doorkeeper.configuration.orm.to_s.include?('mongoid') ? :mongoid : Doorkeeper.configuration.orm -require "support/orm/#{orm_name}" - -ENGINE_RAILS_ROOT = File.join(File.dirname(__FILE__), '../') - -Dir["#{File.dirname(__FILE__)}/support/{dependencies,helpers,shared}/*.rb"].each { |f| require f } - -RSpec.configure do |config| - config.infer_spec_type_from_file_location! - config.mock_with :rspec - - config.infer_base_class_for_anonymous_controllers = false - - config.before do - DatabaseCleaner.start - Doorkeeper.configure { orm DOORKEEPER_ORM } - end - - config.after do - DatabaseCleaner.clean - end - - config.order = 'random' -end diff --git a/spec/support/dependencies/factory_girl.rb b/spec/support/dependencies/factory_girl.rb deleted file mode 100644 index eb4d27b..0000000 --- a/spec/support/dependencies/factory_girl.rb +++ /dev/null @@ -1,2 +0,0 @@ -require 'factory_girl' -FactoryGirl.find_definitions diff --git a/spec/support/helpers/model_helper.rb b/spec/support/helpers/model_helper.rb deleted file mode 100644 index a955440..0000000 --- a/spec/support/helpers/model_helper.rb +++ /dev/null @@ -1,45 +0,0 @@ -module ModelHelper - def client_exists(client_attributes = {}) - @client = FactoryGirl.create(:application, client_attributes) - end - - def create_resource_owner - @resource_owner = User.create!(name: 'Joe', password: 'sekret') - end - - def authorization_code_exists(options = {}) - @authorization = FactoryGirl.create(:access_grant, options) - end - - def access_grant_should_exist_for(client, resource_owner) - grant = Doorkeeper::AccessGrant.first - expect(grant.application).to eq(client) - grant.resource_owner_id == resource_owner.id - end - - def access_token_should_exist_for(client, resource_owner) - grant = Doorkeeper::AccessToken.first - expect(grant.application).to eq(client) - grant.resource_owner_id == resource_owner.id - end - - def access_grant_should_not_exist - expect(Doorkeeper::AccessGrant.all).to be_empty - end - - def access_token_should_not_exist - expect(Doorkeeper::AccessToken.all).to be_empty - end - - def access_grant_should_have_scopes(*args) - grant = Doorkeeper::AccessGrant.first - expect(grant.scopes).to eq(Doorkeeper::OAuth::Scopes.from_array(args)) - end - - def access_token_should_have_scopes(*args) - grant = Doorkeeper::AccessToken.first - expect(grant.scopes).to eq(Doorkeeper::OAuth::Scopes.from_array(args)) - end -end - -RSpec.configuration.send :include, ModelHelper, type: :request diff --git a/spec/support/orm/mongo_mapper.rb b/spec/support/orm/mongo_mapper.rb deleted file mode 100644 index 4a56b52..0000000 --- a/spec/support/orm/mongo_mapper.rb +++ /dev/null @@ -1,25 +0,0 @@ -DatabaseCleaner[:mongo_mapper].strategy = :truncation -DatabaseCleaner[:mongo_mapper].clean_with :truncation - -RSpec.configure do |config| - config.before :suite do - Doorkeeper::Application.create_indexes - Doorkeeper::AccessGrant.create_indexes - Doorkeeper::AccessToken.create_indexes - end -end - -module Doorkeeper - class PlaceholderApplicationOwner - include ::MongoMapper::Document - - set_collection_name 'placeholder_application_owners' - many :applications, class: Doorkeeper::Application - end - - module OrmHelper - def mock_application_owner - PlaceholderApplicationOwner.new - end - end -end diff --git a/spec/support/orm/mongoid.rb b/spec/support/orm/mongoid.rb deleted file mode 100644 index 7b18018..0000000 --- a/spec/support/orm/mongoid.rb +++ /dev/null @@ -1,30 +0,0 @@ -DatabaseCleaner[:mongoid].strategy = :truncation -DatabaseCleaner[:mongoid].clean_with :truncation - -RSpec.configure do |config| - config.before do - Doorkeeper::Application.create_indexes - Doorkeeper::AccessGrant.create_indexes - Doorkeeper::AccessToken.create_indexes - end -end - -module Doorkeeper - class PlaceholderApplicationOwner - include Mongoid::Document - - if ::Mongoid::VERSION >= '3' - self.store_in collection: :placeholder_application_owners - else - self.store_in :placeholder_application_owners - end - - has_many :applications - end - - module OrmHelper - def mock_application_owner - PlaceholderApplicationOwner.new - end - end -end diff --git a/spec/support/shared/models_shared_examples.rb b/spec/support/shared/models_shared_examples.rb deleted file mode 100644 index 9489a3e..0000000 --- a/spec/support/shared/models_shared_examples.rb +++ /dev/null @@ -1,52 +0,0 @@ -shared_examples 'an accessible token' do - describe :accessible? do - it 'is accessible if token is not expired' do - allow(subject).to receive(:expired?).and_return(false) - should be_accessible - end - - it 'is not accessible if token is expired' do - allow(subject).to receive(:expired?).and_return(true) - should_not be_accessible - end - end -end - -shared_examples 'a revocable token' do - describe :accessible? do - before { subject.save! } - - it 'is accessible if token is not revoked' do - expect(subject).to be_accessible - end - - it 'is not accessible if token is revoked' do - subject.revoke - expect(subject).not_to be_accessible - end - end -end - -shared_examples 'a unique token' do - describe :token do - it 'is generated before validation' do - expect { subject.valid? }.to change { subject.token }.from(nil) - end - - it 'is not valid if token exists' do - token1 = FactoryGirl.create factory_name - token2 = FactoryGirl.create factory_name - token2.token = token1.token - expect(token2).not_to be_valid - end - - it 'expects database to throw an error when tokens are the same' do - token1 = FactoryGirl.create factory_name - token2 = FactoryGirl.create factory_name - token2.token = token1.token - expect do - token2.save!(validate: false) - end.to raise_error - end - end -end