- 浏览: 2048797 次
- 性别:
- 来自: 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 1373brew services restart memcached ... -
git如何合并 多个commit
2013-07-02 20:42 9294需要先搞明白rebase 然后,进这个界面 pick b ... -
rvm create gemset
2013-07-01 09:00 1213rvm ruby-1.9.3-p429 do rvm gems ... -
关于devise结合github通过omniauth登录
2013-06-24 04:47 4099最近写了个github帐户登录Demo: https://gi ... -
cdata 和 xml xmlParseEntityRef: no name 错误
2013-05-04 00:24 4934Problem: An XML parser returns ... -
一目了然 rails html xml encode decode
2013-05-03 23:37 30461.9.2p320 :001 > require ' ... -
使用scope 链接多个where条件
2013-05-02 09:17 2560scope :by_category, (lamb ... -
在rspec里使用 route path
2013-05-01 20:09 954Rspec.configure do |config| ... -
select_tag default value & options
2013-04-10 21:40 2155#If you are using select_tag ... -
Jquery array remove
2013-04-10 21:38 4483Array.prototype.remove = fu ... -
ruby readline的两种写法
2013-04-09 10:21 861f = File.read('public/file.cs ... -
关于encoding utf-8
2013-04-04 20:55 4041一 gem解决方案 https://github.com/m- ... -
我见过最清楚的解释class_eval 和 instance_eval
2013-04-02 07:06 3277忘了,看一次就能回忆起来 class A # def ... -
multiple provider oauth
2013-04-01 11:13 1229Allowing users to login with mu ... -
Ruby Jquery 地图,地理信息相关资源
2013-03-22 20:32 895Railscast Geocorder Geocorde ... -
load migrate file and load
2013-03-22 05:52 948Dir[Rails.root.join('db','mig ... -
Brew update problem
2013-03-22 05:48 1284引用 MBA:~ fortin$ brew update er ... -
Jquery sort table number
2013-03-19 01:01 1100So here is what the column is s ... -
update_all
2013-03-13 02:09 1295Article.limit(2).update_all [&q ... -
接着上面的母子表单
2013-03-12 11:45 835Creating a new Rails proj ...
相关推荐
Electron OAuth Github软件包。 如何使用它 ? 该软件包可帮助获得来自Github的OAuth授权。 它使用此处描述的web application flow : : 包提供2个功能: module.exports = { getAuthorizationCode, ...
nextjs-oauth-github 通过NextAuth登录GitHub
Django Github OAuth 使用 Github 的 OAuth 对 Django Web 应用程序中的用户进行身份验证的后端。安装通过设置工具 python setup.py install通过 PyPi pip install django-github-oauth用法在您的project/settings....
Gerrit 2.11支持OAuth2.0验证方式。GitHub 集成的OAuthprovider,可配置到gerrit使用
由于项目需要,同事要做twitter和github的第三方登陆,也就是oauth 认证, 受他委托,我特意给他写了这个实例, 代码可直接运行, 仅供参考。需要的拿走
这是一个Verdaccio插件,为浏览器和命令行提供GitHub OAuth集成。 产品特点 UI集成,具有完整的登录和注销功能。 单击登录按钮后,用户将重定向到GitHub并返回工作会话。 更新了使用情况信息和适用于安装命令的...
资源为在eclipse开发环境中使用Java搭建OAuth Server和OAuth Client 参考代码为http://code.google.com/p/oauth/ OAuth Server为遵守OAuth 1.0协议的OAuth认证服务器 OAuth Client分为Desktop版本和Webapp版本
本篇文章主要介绍了spring-boot集成spring-security的oauth2实现github登录网站的示例,非常具有实用价值,需要的朋友可以参考下
java Web第三方登录实现(基于OAuth2.0,包含Github和QQ登录,附源码)
React-OAuth 在React中实现OAuth。GitHub和WakaTime OAuth在这里实现。 该存储库由。
下载项目压缩包,解压,里面两个maven项目:oauthserver和oauthclient01,分别对应oauth服务端和客户端。 服务端对应的数据库sql文件在源码压缩包里可以看到。 两个项目分别用8082端口(服务端端口)和8081端口...
部署了一套非常全的OAuth2.0的例子 其中包括客户端和服务器端,专门为了OAuth2.0初学者提供了一个学习的资料 可以参考微博地址:http://blog.csdn.net/jbjwpzyl3611421/article/details/51130030
Github Oauth桥该webapp允许与github Oauth建立桥梁。 事实是github Oauth不能与alexa技能Oauth一起使用,因此此桥允许将alexa Oauth请求转换为github Oauth可以理解的请求。这个怎么运作 ? 很简单,您可以在正文中...
上的应用程序以及关联的客户端ID和客户端密钥。 首次使用说明 go get -d github.com/IntuitDeveloper/OAuth2-Go 通过从应用程序的键部分复制来填充config.json文件值(clientId,clientSecret)。 运行代码 一旦...
springcloud整合oauth2和jwt实现权限认证,整合mybaits
一个简单的 Github OAuth 测试客户端
spring-oauth2-github 将Spring Boot与oauth2结合使用的演示目录入门要求 :spouting_whale:用法在开发模式下运行应用服务器: $ docker-compose up -d 停止开发应用服务器: $ docker-compose stop todos贡献错误,...
将应用程序注册到github 应用程序URL: 回调URL: 上面是springboot生成的端点,“ github”是application.properties中的注册ID。启动和测试
含有详细文档+开放Demo实例。使用C#语言开发,MVC框架调用Github第三方登录OAuth2.0接口。 详细介绍相关开发步骤信息。
github-oauth-提示 轻松创建GitHub OAuth令牌。 完全支持两因素身份验证。 入门 使用以下命令安装模块: npm install github-oauth-prompt var oauth = require ( 'github-oauth-prompt' ) ; oauth ( { name : '...