标签: rails-api

如何使用rails-api gem查看助手?

我使用rails-apirabl gems来制作api应用程序.我不想在rabl视图中加入很多逻辑,但如果我想把它提取到帮助器中.Rails-api gem不包括helper支持(那没问题)所以我如何在我的json视图中启用帮助器?(我需要包含哪些中间件,哪些模块等)

ruby ruby-on-rails backend ruby-on-rails-3 rails-api

7
推荐指数
1
解决办法
2935
查看次数

Ember.js Ember-Data + AMS的身份验证令牌=> JSON或HTTP标头?

背景:

我有一个Ember.js 1.1.0-beta.1应用程序,它与Rails-API服务器(Rails 4)交换JSON数据.使用Ember-Data 1.0.0-beta.2和Active Model Serializers 0.8.1(AMS)完成JSON数据交换.我正在使用Ember-Data和AMS的默认推荐配置,并且符合JSON-API规范.

在任何给定的RESTful调用中,客户端将当前的身份验证令牌传递给服务器.验证和退出身份验证令牌,并生成新的身份验证令牌并将其发送回客户端.因此,每个RESTful调用都接受请求中的身份验证令牌,并在响应中提供新的身份验证令牌,客户端可以缓存该令牌并用于下一个RESTful调用.

题:

我在哪里将身份验证令牌放在每个请求和响应中?

它应该是请求和响应中每个对象的JSON的一部分吗?如果是这样,令牌放在现有对象的JSON结构中哪里(与身份验证无关)?

或者它们是否应该放在每个请求和响应对象的HTTP标头中?

什么是"Ember Way",人们可能最终期望在新的Ember Guides Cookbook中找到它?

更多背景:

我已经熟悉以下链接:

  • @machty 2 Embercasts:http://www.embercasts.com/episodes/client-side-authentication-part-2
  • @wycats推文:https://twitter.com/wycats/status/376495062709854209
  • @cavneb 3篇博文:http://coderberry.me/blog/2013/07/08/authentication-with-emberjs-part-1
  • @simplabs博客文章:http://log.simplabs.com/post/53016599611/authentication-in-ember-js

......我正在寻找超越这些的答案,并且特定于Ember-Data + AMS.

除了需要通过Ember-Data在响应中将新令牌传递回客户端之外,假设我的客户端代码与GitHub上的@machty Embercast示例类似:https://github.com/embercasts/authentication -Part-2 /斑点/主/公共/ JS/app.js

非常感谢你!

authentication ember.js ember-data rails-api active-model-serializers

7
推荐指数
1
解决办法
1648
查看次数

尽管路由已声明,但使用Rails 5(API标志)并且没有路由错误

这是我的控制器:

class Api::V1::UsersController < ApplicationController
  respond_to :json

  def show
    respond_with User.find(params[:id])
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的 routes.rb

require 'api_constraints'

Rails.application.routes.draw do
  devise_for :users
  # Api definition
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      resources :users, :only => [:show]
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的 lib/api_constraints.rb

class ApiConstraints
  def initialize(options)
    @version = options[:version]
    @default = options[:default]
  end

  def matches?(req)
    @default || req.headers['Accept'].include?("application/vnd.myapp.v#{@version}")
  end
end
Run Code Online (Sandbox Code Playgroud)

我在我的数据库中添加了一条记录,如下所示:

[3] pry(main)> User.all …
Run Code Online (Sandbox Code Playgroud)

routing ruby-on-rails rails-api ruby-on-rails-5

7
推荐指数
1
解决办法
1929
查看次数

具有API和Web视图的Rails 4应用程序中的ActionController :: API

我有一个带有Web视图和JSON API的Rails 4.2.7应用程序。Web视图和API有单独的基本控制器。我希望API基本控制器继承自ActionController::API。但是,在Rails 4中ActionController::API是在rails-apigem中实现的,而我使用的是标准railsgem。

我试图创建一个ActionController::API“克隆”从继承ActionController::Metal和包括所包含所有模块ActionController::API(见源rails-api 在这里rails5 这里)。结果代码为:

class Api::V1::BaseApiController < ActionController::Metal
  include AbstractController::Rendering
  include ActionController::UrlFor
  include ActionController::Redirecting
  # originally ApiRendering
  include ActionController::Rendering
  include ActionController::Renderers::All
  include ActionController::ConditionalGet
  include ActionController::RackDelegation
  # Originally BasicImplicitRender
  include ActionController::ImplicitRender
  include ActionController::StrongParameters
  include ActionController::ForceSSL
  include ActionController::DataStreaming
  # Before callbacks should also be executed as early as possible so
  # also include them at the bottom.
  include AbstractController::Callbacks
  # …
Run Code Online (Sandbox Code Playgroud)

api ruby-on-rails rails-api

7
推荐指数
0
解决办法
607
查看次数

使用OAuth密码凭据授予令牌访问身份验证的优点

我正在使用Rails 5为移动应用程序创建API.目前,我不需要三脚授权,因为API只会由我们自己的移动应用程序使用.

我正在考虑选择以下两个选项之一:

  1. 门卫+设计:我可以使用门卫实现OAuth 2.0,但至少目前我只会使用密码凭据授权.
  2. Rails自己的ActionController::HttpAuthentication::Token模块+设计:这似乎是更简单的方法.

老实说,我看不到令牌访问身份验证方法和OAuth 2.0的密码凭据授权之间的区别.

如何选择一个而不是另一个?还有其他需要考虑的选择吗?

authentication ruby-on-rails oauth oauth-2.0 rails-api

7
推荐指数
1
解决办法
338
查看次数

Rails(3.2.7):覆盖asset_host的image_tag

development.rb:

config.action_controller.asset_host = "assets.myserver.com"
Run Code Online (Sandbox Code Playgroud)

查看脚本:

<%= image_tag('header.jpg') %>
Run Code Online (Sandbox Code Playgroud)

收益率:

<img alt="Header" src="/header.jpg" />
Run Code Online (Sandbox Code Playgroud)

应该:

<img alt="Header" src="http://assets.myserver.com/header.jpg" />
Run Code Online (Sandbox Code Playgroud)

我正在使用rails-api我猜的宝石禁用一些资产和查看渲染的东西.

似乎不应该太难以重新实现(覆盖image_tag)来添加这个非常小的功能.想要这样做似乎有点奇怪.但是,我是新手,想知道如何做到这一点作为学习经验.

问题:

  1. 作为最佳实践,我应该将这个新代码放在文件结构中?
  2. 我应该用新代码命名该文件?
  3. rails如何知道查看新代码而不是查看旧的image_tag函数?

overriding ruby-on-rails-3 asset-pipeline rails-api

6
推荐指数
1
解决办法
1800
查看次数

Rails前端应用程序中的Google/Facebook OAuth2,使用rails-api作为webservice

我有我的前端rails应用程序也使用AngularJs我也有一个api后端也写在Rails(使用rails-api宝石).我已经为这个系统实现了登录系统.现在我想实施login with facebook/google.我通过互联网搜索,到目前为止我找到了这个博客.在这个博客中,我得到了google OAuth 2在Android中使用Rails作为后端实现的想法.但在我的情况下,我想google OAuth 2在我的前端应用程序(Rails + Angular)中实现Rails作为后端.

对于实施的google OAuth 2,前端应用程序需要请求tokengoogle oauth2 authorization server和我对如何做到这一点不知道.

另外,使用rails来请求令牌是好的,或者我可以将Angular用于该部分.为此目的,最佳做法是什么?

ruby-on-rails facebook-oauth angularjs rails-api google-oauth2

6
推荐指数
1
解决办法
1212
查看次数

为什么Rails RSpec响应显示302而不是401?

我已经在这个问题上停留了几天,但我不知道这是怎么回事。几个月前,我开始使用Ruby on Rails,目前正在学习API的身份验证。我已经在其他类似的主题看上去这里那里,但没有他们的帮助。

我的问题是,无论何时我在此代码上运行RSpec(位于spec/api/v1/controllers/posts_controller_spec.rb

require 'rails_helper'

RSpec.describe Api::V1::PostsController, type: :controller do
  let(:my_user) { create(:user) }
  let(:my_topic) { create(:topic) }
  let(:my_post) { create(:post, topic: my_topic, user: my_user) }

  context "unauthenticated user" do

    it "PUT update returns http unauthenticated" do
      put :update, topic_id: my_topic.id, id: my_post.id, post: {title: my_post.title, body: my_post.body}
      expect(response).to have_http_status(401)
    end
    ...
Run Code Online (Sandbox Code Playgroud)

我不断

...
spec/api/v1/controllers/posts_controller_spec.rb -e "unauthenticated user"
Run options: include {:full_description=>/unauthenticated\ user/}
FFF

Failures:

  1) PostsController unauthenticated user PUT update returns http …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails http-status-code-302 rails-api

6
推荐指数
1
解决办法
1476
查看次数

未定义的局部变量或方法`flash'for#<Devise :: OmniauthCallbacksController:0x007fb5d1741e48>

我正在使用Omniauth-facebook和Devise-token-auth构建一个Rails-API,其中Angular和ng-token-auth用于前端.但是当用facebook登录时,我会看到错误:

undefined local variable or method `flash' for #<Devise::OmniauthCallbacksController:0x007fd027a51e10>
Run Code Online (Sandbox Code Playgroud)

似乎omniauth自动使用闪存中间件但是rails-api不包括这个,并且我没有成功禁用使用omniauth的flash.我的配置如下:

application.rb中:

    require File.expand_path('../boot', __FILE__)

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module PathfinderApi
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true


    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, …
Run Code Online (Sandbox Code Playgroud)

middleware ruby-on-rails devise rails-api omniauth-facebook

6
推荐指数
1
解决办法
3570
查看次数

Rails 5 has_secure_token 加密

在 Ruby on Rails has_secure_tokengem/feature 中,它在创建记录时创建一个唯一的标记,并将其以纯文本形式存储在数据库中。如果我使用该令牌授予用户对 API 的访问权限,将该令牌以纯文本形式存储在数据库中是否存在安全风险?

token我希望当该方法将令牌提交到数据库时有一种方法可以对列进行加密has_secure_token,类似于bcrypt将密码加密到数据库中的方式。

我尝试过使用 gems 来attr_encrypted存储令牌的哈希值,但它似乎与has_secure_token. 以下是我的模型当前的设置方式:

class User < ApplicationRecord
  has_secure_token :token
  # attr_encrypted :token, key: :encrypt_token, attribute: 'token'

  # def encrypt_token
  #   SecureRandom.random_bytes(32)
  # end
end
Run Code Online (Sandbox Code Playgroud)

被注释的代码是attr_encrypted已被证明不兼容的代码。如果有人知道是否有一种方法可以安全地加密数据库中的列,同时也使用has_secure_token,我将不胜感激!

如果需要更多信息或者这令人困惑,请告诉我。提前致谢!

ruby encryption ruby-on-rails access-token rails-api

6
推荐指数
1
解决办法
2903
查看次数