我正在使用使用 Devise 的 Spree 构建一个 ecomm 服务,它包含来自我们用户的所有数据,但我们仅通过 API 将其用作服务,因此我们有一个应用程序(rails 3.1),客户端/消费者,使用 OmniAuth,有一个自定义策略,通过 Oauth2,将请求重定向到提供者,显示标志是页面,然后让请求通过授权方法,然后整个 oauth2 过程完美运行,直到回调,然后用户登录到我们的消费者。
在这部分之前,一切正常,这是我的策略课:
module OmniAuth
module Strategies
class MyStrategy < OAuth2
def initialize(app, api_key = nil, secret_key = nil, options = {}, &block)
client_options = {
:site => CUSTOM_PROVIDER_URL,
:authorize_url => "#{CUSTOM_PROVIDER_URL}/auth/my_provider/authorize",
:token_url => "#{CUSTOM_PROVIDER_URL}/auth/my_provider/access_token"
}
super(app, :onyx_oauth, api_key, secret_key, client_options, &block)
end
protected
def user_data
response = @access_token.get("/auth/my_provider/user.json");
if response.status.to_i == 200
@data ||= MultiJson.decode(response.body);
else
raise 'Service error'
end
end
def request_phase
options[:scope] ||= "read"
super
end
def user_hash
user_data
end
def auth_hash
OmniAuth::Utils.deep_merge(super, {
'uid' => user_data["uid"],
'user_info' => user_data['user_info'],
'extra' => user_data['extra']
})
end
end
end
end
我想要做的是在客户端应用程序中有一个登录表单,然后使用我的自定义策略,将这些凭据(电子邮件/密码)发送给提供者(我正在考虑通过 HTTP_AUTHORIZATION header ),以记录用户进入提供者跳过了提供者中的登录表单,因此设计对用户进行身份验证后,它通过授权方法并让整个 Oauth2 过程继续进行。
我一直在尝试一些事情,包括在策略中定义一个方法 call(env) 并在调用 super.我还在 request_phase 方法中包含选项哈希中的“HTTP_AUTHORIZATION”键,即使在 :headers 键中也是如此。我最后一次尝试是将其包含在类的客户端对象(OAuth2::Client 实例)中的法拉第连接对象的 header 哈希中。
在调试了几乎整个过程并尝试将其包含在任何地方之后,我的提供者一直没有在 header 中获取那个特定的键,甚至在设计测试每个策略时调试提供者......但毕竟不是运气。
如果有人能告诉我我做错了什么,或者我想要完成的事情不能以我正在做的方式完成,或者是否有更简单的方法,我真的很感激。
在此先感谢,对于很长的帖子/问题感到抱歉,再次感谢您的宝贵时间!
最佳答案
我认为这可能是您正在寻找的:
取自 https://github.com/jackdempsey/omniauth-reddit
module OmniAuth
module Strategies
class Reddit < OmniAuth::Strategies::OAuth2
#class NoAuthorizationCodeError < StandardError; end
option :name, "reddit"
option :authorize_options, [:scope, :duration]
option :client_options, {
site: 'https://oauth.reddit.com',
token_url: 'https://ssl.reddit.com/api/v1/access_token'
}
uid { raw_info['id'] }
info do
{
name: raw_info['name']
}
end
extra do
{'raw_info' => raw_info}
end
def raw_info
@raw_info ||= access_token.get('/api/v1/me').parsed || {}
end
def build_access_token
options.token_params.merge!(:headers => {'Authorization' => basic_auth_header })
super
end
def basic_auth_header
"Basic " + Base64.strict_encode64("#{options[:client_id]}:#{options[:client_secret]}")
end
MOBILE_USER_AGENTS = 'webos|ipod|iphone|mobile'
def request_phase
options[:client_options].authorize_url = mobile_request? ? 'https://ssl.reddit.com/api/v1/authorize.compact' : 'https://ssl.reddit.com/api/v1/authorize'
super
end
def mobile_request?
ua = Rack::Request.new(@env).user_agent.to_s
ua.downcase =~ Regexp.new(MOBILE_USER_AGENTS)
end
end
end
end
关于ruby-on-rails-3 - 如何在消费者中使用omniauth和oauth2注入(inject)http_authorization header ,以便在提供者中进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7963616/