- 浏览: 2073208 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
http://railscasts.com/episodes/241-simple-omniauth
OmniAuth is a recently released library from Intridea that gives you drop-in Rack middleware to authenticate with just about anything. In this guest post we’re going to walk you through how to use OmniAuth and Rails 3 to allow multiple-provider authentication in your app.
Enter OmniAuth
The first step is to add OmniAuth to your Gemfile:
gem 'omniauth'
Now we need to create an initializer to make use of the OmniAuth middleware. Easy enough, in config/intitializers/omniauth.rb add:
Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET' provider :facebook, 'APP_ID', 'APP_SECRET' provider :linked_in, 'CONSUMER_KEY', 'CONSUMER_SECRET' end
You’ve actually already done quite a lot. Try running your application with rails server and navigating to /auth/twitter, /auth/facebook, or /auth/linkedin. You should (assuming you’ve set up applications with the respective providers correctly) be redirected to the appropriate site and asked to login.
Handling the Callback
Upon confirmation, you should be redirected back and get a routing error from Rails from /auth/yourprovider/callback. So let’s add a route! In
#config/routes.rb add:Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET' provider :facebook, 'APP_ID', 'APP_SECRET' provider :linked_in, 'CONSUMER_KEY', 'CONSUMER_SECRET' end
match '/auth/:provider/callback', :to =》 'sessions#create'
But of course, this points to a non-existent controller, so let’s create that as well:
rails g controller sessions
Now in our sessions_controller.rb lets add a bit of code:
class SessionsController request.env['rack.auth'].inspect end end
If you start up the app again and go through the auth process, you should now see a hash that includes a whole lot of information about the user instead of a routing error. We’re on our way!
Authorizations and Users
One of the nice things about OmniAuth is that it doesn’t assume how you want to handle the authentication information, it just goes through the hard parts for you. We want users to be able to log in using one or many external services, so we’re actually going to separate users from authorizations. Let’s create simple Authorization and User models now.
rails g model authorization provider:string uid:string user_id:integer rails g model user name:string
This creates the models we need with appropriate migrations. Notice that the User model doesn’t need to contain any information about authentication providers because we’ll model that through a relationship to the Authorization model. Set up your models like so:
class User :provider end
Here we’re modeling very simple relationships and making sure that the Authorization has both a provider (e.g. “twitter” or “facebook”) and a uid (i.e. the external service ID). Next up, we’ll wire these models into our controller to create a real sign in process.
Signing Up/In
One of the nice things about external authentication is you can collapse the sign up and sign in process into a single step. What we’ll do here is:
- When a user signs in, look for existing Authorizations for that external account.
- Create a user if no authorization is found.
- Add an authorization to an existing user if the user is already logged in.
Let’s work backwards for this functionality by adding the code we want to have to the controller. Modify the create action in SessionsController to look like this:
def create auth = request.env['rack.auth'] unless @auth = Authorization.find_from_hash(auth) # Create a new user or add an auth to existing user, depending on # whether there is already a user signed in. @auth = Authorization.create_from_hash(auth, current_user) end # Log the authorizing user in. self.current_user = @auth.user render :text => "Welcome, #{current_user.name}"; end
Now let’s implement some of these methods. First up, adding some class methods to Authorization:
# in authorization.rb def self.find_from_hash(hash) find_by_provider_and_uid(hash['provider'], hash['uid']) end def self.create_from_hash(hash, user = nil) user ||= User.create_from_hash!(hash) Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider']) end
Now we need to add the method referenced above to the User class:
# in user.rb def self.create_from_hash!(hash) create(:name => hash['user_info']['name']) end
Finally, we need to add some helpers to ApplicationController to handle user state:
class ApplicationController
Voila! Now a user can sign in using any of their accounts and a User will automatically be fetched or created. This is merely a small jumping off point, but from here it would be trivial to do any of the following:
- Show the user a list of services that they’re connected to and let them connect to additional ones (creating Authorizations) or delete existing connections.
- Provide a sign out (just delete the :user_id from the session.)
- Connect to APIs of the authenticating services (add token and secret to Authorization, and store them from the auth key of the rack.auth hash).
We hope that you’ll consider dropping OmniAuth into your Rails Rumble application (it also works really easily with Sinatra) and this post has given you a good foundation for fast and easy multi-provider authentication in Rails 3 with OmniAuth.
发表评论
-
brew service restart
2013-07-06 22:56 1446brew services restart memcached ... -
git如何合并 多个commit
2013-07-02 20:42 9369需要先搞明白rebase 然后,进这个界面 pick b ... -
rvm create gemset
2013-07-01 09:00 1280rvm ruby-1.9.3-p429 do rvm gems ... -
关于devise结合github通过omniauth登录
2013-06-24 04:47 4156最近写了个github帐户登录Demo: https://gi ... -
cdata 和 xml xmlParseEntityRef: no name 错误
2013-05-04 00:24 5000Problem: An XML parser returns ... -
一目了然 rails html xml encode decode
2013-05-03 23:37 31171.9.2p320 :001 > require ' ... -
使用scope 链接多个where条件
2013-05-02 09:17 2606scope :by_category, (lamb ... -
在rspec里使用 route path
2013-05-01 20:09 1008Rspec.configure do |config| ... -
select_tag default value & options
2013-04-10 21:40 2189#If you are using select_tag ... -
Jquery array remove
2013-04-10 21:38 4535Array.prototype.remove = fu ... -
ruby readline的两种写法
2013-04-09 10:21 901f = File.read('public/file.cs ... -
关于encoding utf-8
2013-04-04 20:55 4087一 gem解决方案 https://github.com/m- ... -
我见过最清楚的解释class_eval 和 instance_eval
2013-04-02 07:06 3326忘了,看一次就能回忆起来 class A # def ... -
multiple provider oauth
2013-04-01 11:13 1296Allowing users to login with mu ... -
Ruby Jquery 地图,地理信息相关资源
2013-03-22 20:32 935Railscast Geocorder Geocorde ... -
load migrate file and load
2013-03-22 05:52 996Dir[Rails.root.join('db','mig ... -
Brew update problem
2013-03-22 05:48 1340引用 MBA:~ fortin$ brew update er ... -
Jquery sort table number
2013-03-19 01:01 1140So here is what the column is s ... -
update_all
2013-03-13 02:09 1340Article.limit(2).update_all [&q ... -
接着上面的母子表单
2013-03-12 11:45 869Creating a new Rails proj ...
相关推荐
Electron OAuth Github软件包。 如何使用它 ? 该软件包可帮助获得来自Github的OAuth授权。 它使用此处描述的web application flow : : 包提供2个功能: module.exports = { getAuthorizationCode, ...
而OAuth2.0是一种授权框架,广泛用于安全地访问Web服务,例如GitHub。在本项目中,我们关注的是如何将Gerrit与GitHub集成,并利用OAuth2.0进行身份验证。 首先,让我们深入了解Gerrit的OAuth2.0支持。在Gerrit 2.11...
nextjs-oauth-github 通过NextAuth登录GitHub
Django Github OAuth 使用 Github 的 OAuth 对 Django Web 应用程序中的用户进行身份验证的后端。安装通过设置工具 python setup.py install通过 PyPi pip install django-github-oauth用法在您的project/settings....
spring-oauth2-github 将Spring Boot与oauth2结合使用的演示目录入门要求 :spouting_whale:用法在开发模式下运行应用服务器: $ docker-compose up -d 停止开发应用服务器: $ docker-compose stop todos贡献错误,...
Spring Boot 集成 Spring Security 的 OAuth2 实现 GitHub 登录网站的示例 本篇文章主要介绍了 Spring Boot 集成 Spring Security 的 OAuth2 实现 GitHub 登录网站的示例,非常具有实用价值,需要的朋友可以参考下...
OAuth2 是一个授权框架,广泛应用于各种Web应用和API服务,包括GitHub。在这个场景中,我们探讨的是如何将你的应用程序与GitHub的OAuth2服务集成,以便用户可以通过他们的GitHub账户安全地登录你的应用。 首先,...
其中,GitHub作为全球知名的代码托管平台,提供了基于OAuth2.0协议的第三方登录服务。OAuth2.0是一个授权框架,允许应用在不获取用户密码的情况下获取有限的访问权限。 **OAuth2.0简介** OAuth2.0是一种广泛采用的...
本项目是基于OAuth2.0协议实现的第三方登录功能,包括GitHub和QQ的登录接口集成,同时也附带了完整的源代码。 OAuth2.0是一个授权框架,它允许第三方应用在用户的授权下获取其在特定服务上的部分数据,但不会暴露...
由于项目需要,同事要做twitter和github的第三方登陆,也就是oauth 认证, 受他委托,我特意给他写了这个实例, 代码可直接运行, 仅供参考。需要的拿走
这是一个Verdaccio插件,为浏览器和命令行提供GitHub OAuth集成。 产品特点 UI集成,具有完整的登录和注销功能。 单击登录按钮后,用户将重定向到GitHub并返回工作会话。 更新了使用情况信息和适用于安装命令的...
SpringBoot整合OAuth2和Gateway实现网关登录授权验证是一个复杂而关键的过程,它涉及到现代微服务架构中的安全性设计。OAuth2是一种授权框架,用于保护API并允许第三方应用访问受保护的资源,而Spring Gateway作为...
而OAuth2和JWT(JSON Web Token)是两种广泛用于身份验证和授权的技术。本篇文章将深入探讨如何在Spring Cloud项目中整合OAuth2和JWT,以及与MyBatis的集成。 首先,OAuth2是一个开放标准,主要用于授权。它允许第...
Apache Shiro是一个强大的Java安全框架,它提供了身份验证、授权、会话管理和加密等功能,而OAuth2则是一种开放标准,用于授权第三方应用访问用户资源。将Shiro与OAuth2集成,可以实现更灵活的安全控制,特别是在...
2. **OAuth 2**: OAuth 2 更加简单,常用于Google、Facebook、GitHub等平台的API授权。它主要关注的是授权而不是认证,允许用户授权第三方应用访问他们存储在特定服务上的数据,而无需共享他们的登录凭据。OAuth 2的...
资源为在eclipse开发环境中使用Java搭建OAuth Server和OAuth Client 参考代码为http://code.google.com/p/oauth/ OAuth Server为遵守OAuth 1.0协议的OAuth认证服务器 OAuth Client分为Desktop版本和Webapp版本
请注意,由于OAuth 1.0的安全性要求,每次请求的nonce和timestamp都必须是唯一的,因此在实际操作中,nonce和timestamp的生成应在每次请求时动态进行。 总结来说,实现JMeter中的OAuth 1.0认证需要对OAuth协议有...
Spring Security OAuth 是一个用于保护RESTful Web服务的框架,它为OAuth 1.0a和OAuth 2.0协议提供了全面的支持。在这个源码中,我们可能会看到如何将Spring Security与OAuth结合,以构建安全的Web应用程序和服务。...
下载项目压缩包,解压,里面两个maven项目:oauthserver和oauthclient01,分别对应oauth服务端和客户端。 服务端对应的数据库sql文件在源码压缩包里可以看到。 两个项目分别用8082端口(服务端端口)和8081端口...