1

iPhoneアプリに接続するアプリがあり、それがhttp_digestを介してユーザーを認証します。

私は authlogic を使用しています。私のスキーマでは、Web サイトのユーザーは「ユーザー」であり、電話アプリのユーザーは「人」です。だから、私は user_sessions と people_sessions を持っています。http_digest 認証を処理するために、次のような authenticate_or_request_with_http_digest メソッドを使用しています。

def digest_authenticate_person
  authenticate_or_request_with_http_digest do |email, password|
    #ldb is just a logging method i have
    ldb "email = #{email.inspect}, password = #{password.inspect}"
    person = Person.find_by_email(email)
    if person
      ldb "Authentication successful: Got person with id #{person.id}"
      @current_person_session = PersonSession.create(person)        
    else
      ldb "Authentication failed"
      @current_person_session = nil
    end
    return @current_person_session
  end
end

パスワードが nil であることをログで確認できます。メールのみが authenticate_or_request_with_http_digest ブロックの内部に渡されます。

次のようなcurl呼び出しでこれをテストしています:

curl --digest --user fakename@madeup.xyz:apass "http://localhost:3000/reports.xml"

「fakename@madeup.xyz」と「apass」がブロックの内部に渡されることを期待しています。パスワードを取得したら、電子メールとパスワードの組み合わせを使用して、通常の方法でユーザーを検索 (または検索しない) できます。パスワードにアクセスする方法を知っている人はいますか?

アドバイスに感謝します - マックス

編集 - さらにグーグルで調べてみると、この方法を間違って使用していると思います。パスワードまたは暗号化されたパスワードを返すだけです。しかし、それを http_digest ユーザー名の一部として渡されたパスワードと比較するにはどうすればよいでしょうか?

4

1 に答える 1

1

答えが見つかりました: 私は、authenticate_or_request_with_http_digest がどのように機能するかについて根本的な誤解がありました: ドキュメント (gem のソース コード内) を読んだ後、このメソッドの目的は認証を行うことではなく、「電子メールを提供すること」であることに気付きました。 :realm:password" 文字列をブラウザに送信し、ブラウザに暗号化させて、独自に計算された (またはキャッシュされた) これのバージョンに対して結果をチェックします。

設定方法は次のとおりです。

def current_person
  if @current_person
    @current_person
  else
    load_current_person
  end
end 

#use in before_filter for methods that require an authenticated person (mobile app user)
def require_person
  unless current_person
    redirect_to root_path
  end   
end

def load_current_person
  #check user agent to see if we're getting the request from the mobile app
  if request.env['HTTP_USER_AGENT'] =~ /MobileAppName/
    result = digest_authenticate_person
    if result == 401
      return 401
    elsif result == true
      #make authlogic session for person
      @current_person_session = PersonSession.new(@person_from_digest_auth)
      @current_person = @person_from_digest_auth
    end
  end
end  

#this method returns either true or 401
def digest_authenticate_person
  authenticate_or_request_with_http_digest(Person::DIGEST_REALM) do |email|
    person = Person.find_by_email(email)
    @result = nil
    if person
      #need to send back ha1_password for digest_auth, but also hang on to the person in case we *do* auth them successfully
      @person_from_digest_auth = person
      @result = person.ha1_password  
    else
      @person_from_digest_auth = nil
      @result = false
    end
    @result
  end
end
于 2011-06-17T09:16:30.510 に答える