From bbd7eb3d0f6aa372ddffd32a17c150133c2861cb Mon Sep 17 00:00:00 2001 From: Robert Dingwell Date: Fri, 24 Feb 2012 16:12:29 -0500 Subject: [PATCH] adding ouath2 client support via devise-oauth2-providable fork that works with mongoid. Adding mitre openid server param to the UI --- Gemfile | 2 ++ Gemfile.lock | 20 ++++++++++++++ app/models/user.rb | 4 ++- app/views/devise/sessions/new.html.erb | 2 +- config/routes.rb | 6 ++++- lib/section_registry.rb | 1 + test/factories.rb | 7 +++++ .../devise_oauth2_provider_test.rb | 27 +++++++++++++++++++ 8 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 test/integration/devise_oauth2_provider_test.rb diff --git a/Gemfile b/Gemfile index 7d2caae..efd506a 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,8 @@ gem "capistrano" gem 'heroku' gem "nokogiri" gem 'devise' +gem 'devise_oauth2_providable', :git => 'https://github.com/rdingwell/devise_oauth2_providable.git',:branch => "master" + gem 'omniauth' gem 'omniauth-openid' gem 'kaminari' diff --git a/Gemfile.lock b/Gemfile.lock index fa92360..75826da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,6 +10,16 @@ GIT nokogiri (~> 1.4.7) uuid (~> 2.3.4) +GIT + remote: https://github.com/rdingwell/devise_oauth2_providable.git + revision: 8ede1009ef9edbc8994eb6521293a68aee7e9b71 + branch: master + specs: + devise_oauth2_providable (1.0.5) + devise (>= 1.4.3) + rack-oauth2 (~> 0.11.0) + rails (>= 3.1.0) + GIT remote: https://github.com/rdingwell/ruby-openid.git revision: cc6fa401242190344935fb12a824fa39043cffc7 @@ -50,6 +60,7 @@ GEM addressable (2.2.6) ansi (1.4.2) arel (3.0.0) + attr_required (0.0.5) bcrypt-ruby (3.0.1) bson (1.5.2) bson_ext (1.5.2) @@ -108,6 +119,7 @@ GEM term-ansicolor (~> 1.0.5) highline (1.6.11) hike (1.2.1) + httpclient (2.2.4) i18n (0.6.0) journey (1.0.1) jquery-rails (2.0.0) @@ -164,6 +176,13 @@ GEM rack (1.4.1) rack-cache (1.1) rack (>= 0.4) + rack-oauth2 (0.11.0) + activesupport (>= 2.3) + attr_required (>= 0.0.3) + httpclient (>= 2.2.0.2) + i18n + json (>= 1.4.3) + rack (>= 1.1) rack-openid (1.3.1) rack (>= 1.1.0) ruby-openid (>= 2.1.8) @@ -238,6 +257,7 @@ DEPENDENCIES coffee-rails (~> 3.2.1) cover_me devise + devise_oauth2_providable! factory_girl_rails feedzirra health-data-standards! diff --git a/app/models/user.rb b/app/models/user.rb index f4f5444..6493594 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,7 +3,9 @@ class User # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, - :recoverable, :rememberable, :trackable, :validatable + :recoverable, :rememberable, :trackable, :validatable, + :oauth2_providable, :oauth2_password_grantable, :oauth2_refresh_token_grantable, + :oauth2_authorization_code_grantable field :name # validates_presence_of :name diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 1ce6fbb..ffaebc9 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -46,7 +46,7 @@ - + <%= image_tag "mitre_open_id.png", :class => "tag", :alt => "MITRE Open ID" %> diff --git a/config/routes.rb b/config/routes.rb index dc15b5b..423275b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ match '/auth/:provider/callback' => 'authentications#create' devise_for :users, :controllers => {:registrations => 'registrations'} - + mount Devise::Oauth2Providable::Engine => '/oauth2' resources :authentications # The priority is based upon order of creation: @@ -39,6 +39,10 @@ root :to => "records#index" + #mount the oauth2 devise provider + + + # Sample resource route with options: # resources :products do # member do diff --git a/lib/section_registry.rb b/lib/section_registry.rb index 8e01260..0b6212f 100644 --- a/lib/section_registry.rb +++ b/lib/section_registry.rb @@ -7,6 +7,7 @@ class SectionRegistry include Singleton + attr_reader :extensions def initialize @extensions = [] end diff --git a/test/factories.rb b/test/factories.rb index 277dde0..b42353d 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -40,5 +40,12 @@ u.password_confirmation 'password' u.sequence(:name) { |n| "testuser#{n}"} end + + + factory :client, :class => 'Devise::Oauth2Providable::Client' do |f| + f.name 'test' + f.website 'http://localhost' + f.redirect_uri 'http://localhost:3000' + end end diff --git a/test/integration/devise_oauth2_provider_test.rb b/test/integration/devise_oauth2_provider_test.rb new file mode 100644 index 0000000..f0afe96 --- /dev/null +++ b/test/integration/devise_oauth2_provider_test.rb @@ -0,0 +1,27 @@ +require 'test_helper' + +# This is just some simple testing to see that devise is configured with the OAUTH2 provider +# We are not testing the entirty of the provider, we are counting on the tests for that to cover that +# it works. We are only testing here that it is configured +class DeviseOauth2ProviderTest < ActionController::TestCase + include Devise::TestHelpers + setup do + Devise::Oauth2Providable::Client.destroy_all + @controller = Devise::Oauth2Providable::AuthorizationsController.new + @record = FactoryGirl.create(:record, :with_lab_results) + @user = FactoryGirl.create(:user) + @client = FactoryGirl.create(:client) + sign_in @user + end + + test "Should Redirect to client URL without an auth token " do + + get :new, :client_id => @client.cidentifier, :redirect_uri => @client.redirect_uri, :response_type => 'code', :use_route => 'devise_oauth2_providable' + assert_response :success + end + + test "Should return error with bad redirect utl " do + get :new, :client_id => @client.cidentifier, :redirect_uri => "#{@client.redirect_uri}/t", :response_type => 'code', :use_route => 'devise_oauth2_providable' + assert_response 400, "Should respond with error message if redirect does not match uri" + end +end