Using authlogic_api for Rails REST API access

前端 未结 3 2212
误落风尘
误落风尘 2021-02-09 07:44

I am writing a Rails back-end API for a Steam game that is only accessed via REST calls, so no user-specific authentication is required. I am trying to implement the authlogic_a

3条回答
  •  面向向阳花
    2021-02-09 08:45

    Actually, it's much simpler. Using all that code from the Authlogic example is somewhat overkill - it mainly manages storing session details, which you don't need to do for the Application (also known as Client) session. The Client session is re-confirmed at every request.

    All you need is:

    models\client.rb

    class Client < ActiveRecord::Base
      acts_as_authentic do |config|
      end
    end
    

    models\client_session.rb

    class ClientSession < Authlogic::Session::Base
      api_key_param 'app_key'
    end
    

    controllers\application_controller

    before_filter :verify_client
    
    def verify_client
      @client_session = ClientSession.new()
      unless @client_session.save # if client session not successfully created using the api_key and signature, render an error and block the request
        @error = {:description => "Couldn't validate client application."}
        render :template => 'errors/error.xml.builder'
      end
    end
    

    You also need to run a migration to create the clients table. Not all of the fields below are necessary, but they won't hurt.

    class CreateClients < ActiveRecord::Migration
      def self.up
        create_table :clients do |t|
          # human fields
          t.string :name
          t.string :owner
          t.string :owner_email
          t.string :owner_phone
          # login fields
          t.string :api_key, :null => false
          t.string :api_secret, :null => false
          t.string :password_salt
          t.string :persistence_token
          t.string :perishable_token
          # automagical fields (courtesy of authlogic & authlogic_api)
          t.integer :failed_login_count
          t.datetime :last_request_at
          t.integer :request_count
          t.string :last_request_ip
          # automagical fields (courtesy of rails)
          t.timestamps
        end
      end
    
      def self.down
        drop_table :clients
      end
    end
    

提交回复
热议问题