2013-10-17 2 views
1

프로젝트에서 Devise로 놀고 있는데, 어떻게 작동하는지 더 잘 이해하려고합니다. 특히 세션 컨트롤러는 내가 이해할 수없는 몇 가지 일을하고 있습니다.Rails + Devise - 세션 컨트롤러 설명

class Devise::SessionsController < ApplicationController 
    def new 
    # What benefit is this providing over just "resource_class.new"? 
    self.resource = resource_class.new(sign_in_params) 
    clean_up_passwords(resource) 
    # What is "serialize_options" doing in the responder? 
    respond_with(resource, serialize_options(resource)) 
    end 

    def create 
    self.resource = warden.authenticate!(auth_options) 
    set_flash_message(:notice, :signed_in) if is_navigational_format? 
    sign_in(resource_name, resource) 
    respond_with resource, :location => after_sign_in_path_for(resource) 
    end 
    ... 

    protected 
    ... 

    def serialize_options(resource) 
    methods = resource_class.authentication_keys.dup 
    methods = methods.keys if methods.is_a?(Hash) 
    methods << :password if resource.respond_to?(:password) 
    { :methods => methods, :only => [:password] } 
    end 

    def sign_in_params 
    devise_parameter_sanitizer.sanitize(:sign_in) 
    end 
end 

이 방법들은 일종의 보안을 추가한다고 가정합니다. 나는 그들이 정확히 무엇으로부터 보호하고 있는지 정확히 알고 싶다.

+0

첫 번째 의견은'self.resource ='부분 또는'(sign_in_params)'부분에 대해 묻고 있습니까? 두 번째 주석의 경우, "serialize_options가하는 일"은 무엇을 의미합니까? 또는 "serialize_options가 응답자에게 전달되는 이유는 무엇입니까?" – carols10cents

+0

(sign_in_params) 부분에 대해 물어볼 것입니다. (왜 User.new를 사용하는 대신 새 User 객체를 삭제합니까?) "sanitize"는 무엇을 의미합니까?) 그런 다음 serialize_options가 수행하는 작업과 그 이유를 묻습니다. 기본적으로 두 가지 방법을 모두 건너 뛰어도 BDD 관점에서 동일한 기능을 복제 할 수 있습니다.이 값은 내가 이해하지 못하는 일종의 보안에 있다고 믿을 수 있습니다. – Bryce

답변

2

The implementation of devise_parameter_sanitizerParameterSanitizer 인스턴스를 생성합니다. 나는 종종 코드의 목적을 이해하는데 도움이되는 테스트를보고있다. 그리고 this test 나는 새로운 사용자를 만들고 있기 때문에 사용자가 원하는 어떤 값을 원하는 매개 변수에 할당 할 수 없기를 바란다. 따라서 sanitize은 " 우리는이 행동을 위해 필요합니다. " 여기에 없는데 role 특성이있는 경우 사용자는 사이트에 가입 할 때 관리자가되도록 특수하게 조작 된 POST 요청을 보낼 수 있습니다. 그래서 이것은 일반적으로 mass assignment vulnerability이라는 것을 보호합니다. Rails 3 and Rails 4 protect against this in different ways하지만 보호 장치는 여전히 꺼져있을 수 있습니다. Devise가 좋은 실무 기본값을 설정하려고합니다.

serialize_options 메서드는 XML 또는 JSON으로의 렌더링을 지원하는 옵션의 해시를 만듭니다. 나는 the implementation of responds_with을보고 이것을 발견했는데, 마지막 인자가 Hash 인 경우 마지막 인자를 옵션으로 사용하는 extract_options!을 호출합니다. responds_with에 대한 설명서에 "#respond_with에 지정된 모든 옵션이 기본 응답자에게 전송됩니다"라는 메시지가 표시되어 ActionController::Responder을 보았습니다.이 설명서에는 형식과 일치하는 템플릿을 찾는 데 필요한 프로세스가 설명되어 있으며 해당 형식이없는 경우 to_#{format}으로 전화 한 다음 to_format으로 전화하십시오. HTML보기가 있으며 to_xmlto_jsonActiveRecord 개체가 응답합니다. 그 방법 use those options :

The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the 
attributes included, and work similar to the +attributes+ method. 

To include the result of some method calls on the model use <tt>:methods</tt>. 

그래서 이것은 당신이 누군가가 XML 또는 JSON 형식으로 사용하는 경우에 할 수 있습니다보다 더 많은 정보를 노출에서 당신을 유지합니다.