1

サインインによって提供されるセキュリティを失うことなく、サインインをバイパスしてリモートで json に繰り返されるレコードを作成する方法を理解しようと、しばらく時間を費やしました。私は HTTParty でこれを行うことができましたが、rest-client も同様に行うと思います。

私は今、私の解決策を共有していますが、これを行うためのより良い、より安全な方法がおそらくあると思います。これについてのあなたの考えを聞きたいです.

だから、まず最初に。これが私のコントローラーです:

things_controller.rb

...
before_filter :signed_in_user, only: [:show, :destroy]
before_filter :signed_in_user, only: [:create], unless: :is_it_json?
...
def create
    if is_it_json?
      user = User.find_by_email(params[:session][:email].downcase)
      if !(user && user.authenticate(params[:session][:password]))
        format.json { render json: @user.errors, status: :unprocessable_entity }
      else 
        @thing = user.things.build(params[:thing])
      end
    else
      @thing = current_user.things.build(params[:thing])
    end  
    respond_to do |format|
      if @thing.save
        format.html { redirect_to root_path, :flash => { :success =>  'thing was successfully created.'} }
        format.json { render json: @thing, status: :created, location: @thing}
      else
        @feed_items = []
        format.html { render action: "static_pages/home" }
        format.json { render json: @thing.errors, status: :unprocessable_entity }
      end
    end
end

application_controller.rb

...
def is_it_json?
  request.format.json?
end
...

私の Ruby スクリプト: wsConsumption_httparty.rb

require 'HTTParty'
def ask(prompt)
    print prompt, ' '
    $stdout.flush
    s = gets.chomp 
end

class thingCreate
    include HTTParty
    base_uri 'localhost:3000'
    format :json
    headers "Accept" => "application/json"

  def initialize
  end

  def post(description, weight,typeid,email,password)
    options = { body: 
                {thing:
                    {description:description, 
                     weight:weight, 
                     type_id:typeid 
                     },
                session:
                    {email:email,
                    password:password
                    }
                }
            }
    self.class.post('/things', options)
  end
end

username = ask("What is the user email?")
password = ask("What's the password?")
description = ask("thing description: ")
weight = ask("Weight: ")
typeid = ask("Type Id: ")

thing = thingCreate.new()
print thing.post(description, weight,typeid,username,password)

これはうまく機能しています...

  • ものを作成するこのメカニズムをより安全にするにはどうすればよいでしょうか (https を使用するだけで十分ですか?
  • サービスにある種のフェイルセーフ メカニズムを適用する必要がありますか? 助言がありますか?
  • 間違ったユーザー/パスが挿入された場合、「無効なユーザー名/パスワード」というテキスト メッセージのみを返したいのですが、これを実現する方法について何かアイデアはありますか?
  • あなたが持っているかもしれない他の提案。

乾杯

4

0 に答える 0