Rails 中的 Google Apps 和开放 ID 身份验证 - 安全
我正在移动一个应用程序,使其仅使用 Google 联合登录 (OpenID)(我们对所有内容都使用 Google 应用程序,并且认为在那里结合用户管理会更容易)。虽然我可以成功登录并创建用户,但我现在的想法是安全性......
当用户登录时,我只有一个“登录”按钮 - 没有其他。站点域被硬编码(其中 SITE_DOMAIN 出现在下面),并且用户被重定向到典型的 Google 登录页面。
这是代码:
def create
open_id_authentication
end
protected
def open_id_authentication
openid_url = 'https://www.google.com/accounts/o8/site-xrds?hd=SITE_DOMAIN'
authenticate_with_open_id(openid_url,
:required => ['http://axschema.org/contact/email',
'http://axschema.org/namePerson/first',
'http://axschema.org/namePerson/last']) do |result, identity_url, registration|
case result.status
when :missing
failed_login "Sorry, the OpenID server couldn't be found"
when :invalid
failed_login "Sorry, but this does not appear to be a valid OpenID"
when :canceled
failed_login "OpenID verification was canceled"
when :failed
failed_login "Sorry, the OpenID verification failed"
when :successful
if @current_user = User.find_by_id_url(identity_url)
if @current_user.login_from(request.env['REMOTE_ADDR'])
successful_login
else
failed_login "Your OpenID profile registration failed: " + @current_user.errors.full_messages.to_sentence
end
else
ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
@current_user = User.login_create(ax_response, identity_url, request.env['REMOTE_ADDR'])
successful_login
end
end
end
end
成功登录后,我只需将用户保存到会话中...
session[:current_user] = @current_user
...并在应用程序控制器中使用简单的 current_user 方法...
def current_user
return session[:current_user] if defined?(session[:current_user])
end
我主要关心的是安全性。 OpenIDAuthentication 使用内存存储,总的来说,这似乎太容易实现了(在阅读了大量文档之后)。基本测试表明这工作正常,但我很紧张。 :)
有什么想法吗?
我正在使用 open_id_authentication 插件和基本的 ruby openid gem(带有用于谷歌应用程序的 ruby-openid-apps-discovery gem)
I'm moving an app to use only Google Federated Login (OpenID) for an application (we use google apps for everything and feel it would be easier to combine user management there). While I can successfully login and create users, my thoughts are now on security...
When a user logs in I only have a "Log In" button - nothing else. The site domain is hard coded in (where SITE_DOMAIN appears below) and the user is redirected to the typical google login page.
Here is the code:
def create
open_id_authentication
end
protected
def open_id_authentication
openid_url = 'https://www.google.com/accounts/o8/site-xrds?hd=SITE_DOMAIN'
authenticate_with_open_id(openid_url,
:required => ['http://axschema.org/contact/email',
'http://axschema.org/namePerson/first',
'http://axschema.org/namePerson/last']) do |result, identity_url, registration|
case result.status
when :missing
failed_login "Sorry, the OpenID server couldn't be found"
when :invalid
failed_login "Sorry, but this does not appear to be a valid OpenID"
when :canceled
failed_login "OpenID verification was canceled"
when :failed
failed_login "Sorry, the OpenID verification failed"
when :successful
if @current_user = User.find_by_id_url(identity_url)
if @current_user.login_from(request.env['REMOTE_ADDR'])
successful_login
else
failed_login "Your OpenID profile registration failed: " + @current_user.errors.full_messages.to_sentence
end
else
ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
@current_user = User.login_create(ax_response, identity_url, request.env['REMOTE_ADDR'])
successful_login
end
end
end
end
Upon successful login I simply save the user into a session...
session[:current_user] = @current_user
...and use a simple current_user method in the Application controller...
def current_user
return session[:current_user] if defined?(session[:current_user])
end
My main concern is regarding security. OpenIDAuthentication is using the in-memory store and overall this seemed a bit too easy to implement (after reading thru tons of documentation). Basic tests show this works fine, but I'm nervous. :)
Any thoughts?
I am using the open_id_authentication plugin and the basic ruby openid gem (with ruby-openid-apps-discovery gem for google apps)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多亏了 omniauth,现在变得更加容易。
This is now much easier thanks to omniauth.