Using Authlogic_API for Rails Rest API Access

Using authlogic_api for Rails REST API access

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

How to implement API authentication for Rails app with Authlogic

Perhaps you could use the single access token stuff that's in authlogic already?

http://rubydoc.info/gems/authlogic/Authlogic/Session/Params

How do you authenticate user generated apps for your app?

This may be what I need:

http://github.com/phurni/authlogic_api

Rails plugin for API Key + Secret Key signing

Check out this plugin for AuthLogic:

http://github.com/phurni/authlogic_api

I think that does what you are looking for.

Rails4 + Authlogic + rspec

Per https://stackoverflow.com/a/5803121, a request spec is just a thin wrapper around ActionDispatch::IntegrationTest. As such, there is no direct access to the session, unlike a normal controller spec.

Due to this, it isn't directly possible to log a user in directly with AuthLogic, which does rely on the session and cookies:

It first authenticates, then it sets up the proper session values and cookies to persist the session.

For request/integration/api/feature specs, a request directly to the login path will be necessary to set the proper session / cookies behind the scenes. The integration session will then be sent back (just like a normal web request) with the proper values.

To make life easier you can add a helper method, which you can include for request/integration/api/feature specs:

# spec/support/auth_logic_helpers.rb
module Authlogic
module TestHelper
# You can call this anything you want, I chose this name as it was similar
# to how AuthLogic calls it's objects and methods
def create_user_session(user)
# Assuming you have this defined in your routes, otherwise just use:
# '/your_login_path'
post user_session_path, login: user.login, password: user.password
end
end
end

# Make this available to just the request and feature specs
RSpec.configure do |config|
config.include Authlogic::TestHelper, type: :request
config.include Authlogic::TestHelper, type: :feature
end

How to retrieve a URL's image from the API and display it in my view

you can't display the image using the link directly because the server doesn't allow it, also known as hotlinking prevention.

you have to download the image in your backend and then display it using your own link.

controller:

require "open-uri"

File.open('public/cover.jpg', 'wb') do |fo|
fo.write open("https://t.nhentai.net/galleries/1302012/cover.jpg").read
end

view:

<img src="cover.jpg"/>


Related Topics



Leave a reply



Submit