背景:
我有一个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中找到它?
更多背景:
我已经熟悉以下链接:
......我正在寻找超越这些的答案,并且特定于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
这是我的控制器:
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) 我有一个带有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) 我正在使用Rails 5为移动应用程序创建API.目前,我不需要三脚授权,因为API只会由我们自己的移动应用程序使用.
我正在考虑选择以下两个选项之一:
ActionController::HttpAuthentication::Token模块+设计:这似乎是更简单的方法.老实说,我看不到令牌访问身份验证方法和OAuth 2.0的密码凭据授权之间的区别.
如何选择一个而不是另一个?还有其他需要考虑的选择吗?
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)来添加这个非常小的功能.想要这样做似乎有点奇怪.但是,我是新手,想知道如何做到这一点作为学习经验.
问题:
我有我的前端rails应用程序也使用AngularJs我也有一个api后端也写在Rails(使用rails-api宝石).我已经为这个系统实现了登录系统.现在我想实施login with facebook/google.我通过互联网搜索,到目前为止我找到了这个博客.在这个博客中,我得到了google OAuth 2在Android中使用Rails作为后端实现的想法.但在我的情况下,我想google OAuth 2在我的前端应用程序(Rails + Angular)中实现Rails作为后端.
对于实施的google OAuth 2,前端应用程序需要请求token来google oauth2 authorization server和我对如何做到这一点不知道.
另外,使用rails来请求令牌是好的,或者我可以将Angular用于该部分.为此目的,最佳做法是什么?
ruby-on-rails facebook-oauth angularjs rails-api google-oauth2
我已经在这个问题上停留了几天,但我不知道这是怎么回事。几个月前,我开始使用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) 我正在使用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) 在 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,我将不胜感激!
如果需要更多信息或者这令人困惑,请告诉我。提前致谢!
rails-api ×10
ruby ×2
access-token ×1
angularjs ×1
api ×1
backend ×1
devise ×1
ember-data ×1
ember.js ×1
encryption ×1
middleware ×1
oauth ×1
oauth-2.0 ×1
overriding ×1
routing ×1
rspec ×1