devise ソースコードリーディング

何年か前にも devise 関連の gem の中身見て、再び中身を見る機会があったけどすっかり挙動を忘れてたので大まかな流れをメモっておく

完全に自分用のメモ

古の盟約により devise-token_authenticatable を使用している

- application_controller
  - authenticate_user!
    - devise helper_method authenticate_user!
      - warden authenticate!
        - warden proxy _perform_authentication
          - warden proxy _retrieve_scope_and_opts
          - warden proxy user
            - warden session_serializer.fetch return nil
            - warden hooks after_failed_fetch
            - warden proxy set_user
          - warden proxy _run_stragegies_for
            - warden proxy _fetch_stragegy strategies.each
            - warden proxy stragety.performed?
            - devise-token_authenticatable stragety valid? (stragety.valid?)
              - devise strategies authenticatable valid?
                - devise authenticatable valid_for_params_auth?
                  - devise authenticatable stragety params_authenticatable?
                    - devise authenticatable model params_authenticatable?
                  - devise authenticatable stragety valid_params_request?
                  - devise authenticatable stragety valid_params?
                  - devise authenticatable stragety with_authentication_hash
            - warden base _run!(strategy._run!)
              - devise-token_authenticatable stragety authenticate!
                - devise-token_authencatable model fird_for_token_authentication
                  - devise-token_authencatable model find_for_authentication
                    - devise-token_authencatable model find_first_by_auth_conditions
                - devise strategies authenticatable validate
                  - devise model authenticatable valid_for_authentication?
                - devise-token_authencatable model after_token_authentication (only definition)
                - warden base success!
          - warden proxy set_user

基本的な流れは

strategies.each do |strategy|
  strategy._run!
end

をやってる

stragegy は mail & pass 認証だったり token 認証だったりしてるやつ

gem の中で params とかが使われているけど、application_controller とかで使う params ではなく warden の params メソッドを使ってる

devise-token_authenticatable-1.0.2/lib/devise/token_authenticatable/strategy.rb

def params_auth_hash
  if params[scope].kind_of?(Hash) && params[scope].has_key?(authentication_keys.first)
    params[scope]
  else
    params
  end
end

warden-1.2.7/lib/warden/mixins/common.rb

def params
  request.params                          
end