1

当社では、Java コードを含む jar ファイルを使用して、内部 Web サイトにアクセスするユーザーを認証しています。これまでのところ、そのjarファイルを、ユーザーがサインインできるようにする工夫戦略に統合することができました。ただし、コードを実行すると、ログイン/リダイレクトが成功し、その後に無許可/リダイレクトが続きます。

Started POST "/login" for 0:0:0:0:0:0:0:1 at 2017-01-09 11:05:40 -0500
(7.0ms)  SELECT name FROM sqlite_master WHERE type = 'table' AND name =  "schema_migrations"
ActiveRecord::SchemaMigration Load (2.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WCrhgdRgZsQng2rgHdhnCBB7me5lAfhvDxwYrMpMUxfUdMN0fN/PjBHJPIUxMYiNjoJlaLsCIlQdN4WmbPlclg==", "user"=>{"email"=>"<valid email>", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
Custom Authenticate
Authenticated=true
{:username=>"<valid un>", :firstname=>"", :lastname=>"", :fullname=>"", :email=>""}
(0.0ms)  SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'
Redirected to http://localhost:3000/ideas
Completed 302 Found in 825ms (ActiveRecord: 2.0ms)


Started GET "/ideas" for 0:0:0:0:0:0:0:1 at 2017-01-09 11:05:42 -0500
Processing by IdeasController#index as HTML
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL ORDER BY "users"."id" ASC LIMIT 1
Completed 401 Unauthorized in 34ms (ActiveRecord: 1.0ms)


Started GET "/login" for 0:0:0:0:0:0:0:1 at 2017-01-09 11:05:42 -0500
Processing by Devise::SessionsController#new as HTML
  Rendered /Users/un/.rbenv/versions/jruby-9.1.2.0/lib/ruby/gems/shared/gems/devise-4.2.0/app/views/devise/shared/_links.html.erb (6.0ms)
  Rendered /Users/un/.rbenv/versions/jruby-9.1.2.0/lib/ruby/gems/shared/gems/devise-4.2.0/app/views/devise/sessions/new.html.erb within layouts/application (60.0ms)
Completed 200 OK in 1724ms (Views: 1684.7ms | ActiveRecord: 0.0ms)

これは、user.rb の「:database_authenticatable」行の一部であると想定しています。

ユーザー.rb:

class User < ActiveRecord::Base
  #include ActiveModel::Model
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable #, :registerable,
         #:recoverable, :rememberable, :trackable, :validatable

  attr_accessor :username
  attr_accessor :firstname
  attr_accessor :lastname
  attr_accessor :fullname
  attr_accessor :email

end

Custom_authenticated 戦略

module Devise
  module Strategies
    class CustomAuthenticatable < Authenticatable

      def authenticate!
        puts "Custom Authenticate"
        if password.present? && has_valid_credentials?
          #puts "Username and password:" + password
          puts authentication_hash[:email]

          authorized = jar.authenticate(authentication_hash[:email], password);
          puts "Authenticated=" + authorized
          if authorized == false
            return fail!
          else
            personInfo = jar.getPerson(username);
            puts personInfo
            personHash = {:username => personInfo.userName, 
                          :firstname => personInfo.firstName, 
                          :lastname => personInfo.lastName,
                          :fullname => personInfo.longName,
                          :email => personInfo.emailAddress}
            puts personHash
            return success! User.new(personHash)
          end
        else
          fail(:unable_to_authenticate)
        end
      end

      def has_valid_credentials?
        true
      end

    end
  end
end

ただし、:database_authenticatable から :custom_authenticatable に変更すると、users/sign_in ルートがなくなります。

:database_authenticatable としてルーティング

              Prefix Verb   URI Pattern               Controller#Action
               ideas GET    /ideas(.:format)          ideas#index
                     POST   /ideas(.:format)          ideas#create
            new_idea GET    /ideas/new(.:format)      ideas#new
           edit_idea GET    /ideas/:id/edit(.:format) ideas#edit
                idea GET    /ideas/:id(.:format)      ideas#show
                     PATCH  /ideas/:id(.:format)      ideas#update
                     PUT    /ideas/:id(.:format)      ideas#update
                     DELETE /ideas/:id(.:format)      ideas#destroy
    new_user_session GET    /login(.:format)          devise/sessions#new
        user_session POST   /login(.:format)          devise/sessions#create
destroy_user_session DELETE /logout(.:format)         devise/sessions#destroy
                root GET    /                         redirect(301, /ideas)

:custom_authenticatable としてルーティング

   Prefix Verb   URI Pattern               Controller#Action
    ideas GET    /ideas(.:format)          ideas#index
          POST   /ideas(.:format)          ideas#create
 new_idea GET    /ideas/new(.:format)      ideas#new
edit_idea GET    /ideas/:id/edit(.:format) ideas#edit
     idea GET    /ideas/:id(.:format)      ideas#show
          PATCH  /ideas/:id(.:format)      ideas#update
          PUT    /ideas/:id(.:format)      ideas#update
          DELETE /ideas/:id(.:format)      ideas#destroy
     root GET    /                         redirect(301, /ideas)

続きをどうしようか、ちょっと迷っています。また、コールバック フェーズをどのようにセットアップするかはわかりませんが、OmniAuth でおそらく同じことができることもわかりました。サインインを許可しようとして Devise 戦略パスを続行する価値はありますか?それとも、今行っていることをやめて OmniAuth を使用する必要がありますか?

考案ルートを継続するのに時間をかける価値がある場合、次のステップは何ですか?

過去の私のRails経験の多くは、過去に既存のRailsアプリケーションを変更していたため、Deviseをセットアップしたのはこれが初めてです。前もって感謝します。

参考文献: (すべての参考文献へのリンクを含めますが、それ以上のリンクを投稿するには十分な評価ポイントがありません)

認証戦略を考案する

devise と warden を使用してカスタム認証戦略を作成する方法 https://www.ldstudios.co/blog/2016/06/15/how-to-create-custom-authentication-strategies-with-devise-and-warden.html

OmniAuth の非ジェム化戦略の作成 http://www.polyglotprogramminginc.com/writing-a-non-gemified-strategy-for-omniauth/

編集私は実際に答えに非常に近かった。私は Devise を正しくセットアップしておらず、Warden に直接アクセスしていました。つまり、「サインイン」できた後、すぐにアプリケーションからサインアウトされました。

config/initializers/devise.rb (間違った方法)

config.warden do |manager|  
  manager.strategies.add(:custom_authenticatable, Devise::Strategies::CustomAuthenticatable)
  manager.default_strategies(:scope => :user).unshift :custom_authenticatable
end

これにより、custom_authenticatatable コードが呼び出されましたが、まだユーザーのローカル データベースを使用しようとしていました。(ldstudios ブログで説明されているように) devise.rb に次の変更を加えたところ、期待どおりに動作し始めました。

config/initializers/devise.rb (正しい方法)

Devise.add_module(:custom_authenticatable, {
  strategy: true,
  controller: :sessions,
  model: 'custom_auth',
  route: :session
})
4

1 に答える 1

0

omn​​iauth ルートはそれほど悪くありません。omn​​iauth-identityでプロバイダーをサブクラス化し、カスタム サインイン ロジックを追加できます。

于 2017-01-11T00:00:41.350 に答える