简体   繁体   English

使用Twitter反向身份验证在Rails后端中创建用户

[英]Create User in rails backend using Twitter Reverse Auth

I am building an iOS app with a backend / companion website written on Rails. 我正在构建一个用Rails编写的后端/伴侣网站的iOS应用。

I have set up devise and omniauth-twitter as outlined in the rails cast 我已经按照rails cast中的概述设置了devise和omniauth-twitter
http://railscasts.com/episodes/235-devise-and-omniauth-revised http://railscasts.com/episodes/235-devise-and-omniauth-revised

I have also performed twitter reverse auth to obtain a oauth token on the devise as outlined here 我还执行了twitter反向身份验证以在设备上获取oauth令牌,如此处概述
https://dev.twitter.com/docs/ios/using-reverse-auth https://dev.twitter.com/docs/ios/using-reverse-auth

Now what I want to do is to send the token to the server and create a devise User in my DB. 现在,我要做的就是将令牌发送到服务器,并在数据库中创建一个devise用户。
What is the ideal way to create the user? 创建用户的理想方法是什么? Is this scenario supported out of the box with my current setup or do I have to write a little custom code? 我当前的设置是否开箱即用地支持此方案,还是必须编写一些自定义代码?

I have been researching the same thing for a few days. 我研究同一件事已经有几天了。

I believe omniauth-twitter does not work with Twitter reverse authentication. 我相信omniauth-twitter不适用于Twitter反向身份验证。

I think the solution is to send the oauth_token and oauth_token_secret you received from the reverse authentication process on iOS, to a custom endpoint in your application. 我认为解决方案是将您从iOS上的反向身份验证过程中收到的oauth_tokenoauth_token_secret发送到应用程序中的自定义终结点。 Note, I am in the process of implementing this myself; 请注意,我正在自己实施此程序; it should work, but it's not tested yet. 它应该可以工作,但是尚未经过测试。

There you would use the twitter API to to verify the credentials. 在那里,您将使用twitter API来验证凭据。 This is the actual authentication step from the rails app's perspective. 从rails应用程序的角度来看,这是实际的身份验证步骤。 Something like this (untested code): 这样的东西(未经测试的代码):

  consumer = OAuth::Consumer.new(TWITTER_APP_ID,
                                 TWITTER_APP_SECRET,
                                 { site: 'https://api.twitter.com' })
  access_token_token = ::OAuth::AccessToken.new(consumer,
                                                params[:oauth_token],
                                                params[:oauth_token_secret])
  MultiJson.load(access_token.get('/1.1/account/verify_credentials.json?include_entities=false&skip_status=true')).body

(This is the same thing that the omniauth-twitter gem does after it gets the access token) So from here on you would need to: (这与omniauth-twitter gem在获得访问令牌后omniauth-twitter的相同),因此从这里开始,您需要:

  • Handle non-200 response from the above request 处理上述要求的非200回应
  • Read the user ID from the returned hash, search your database for an existing user with an existing twitter user ID. 从返回的哈希中读取用户ID,在数据库中搜索具有现有twitter用户ID的现有用户。 If one exists, sign him in. If not, create a new user and sign him in. 如果存在,请登录。如果不存在,请创建一个新用户并登录。

Here's some more info: 这是更多信息:

  • My first idea was to hit the twitter callback provider by omniauth-twitter directly. 我的第一个想法是直接通过omniauth-twitter twitter回调提供程序。 But in that case you would need to pass along 2 parameters: the oauth_token which is the request token you obtain from the reverse auth on iOS, and oauth_verifier parameter which I have no idea how to get on iOS (Using the standard web flow where the user is redirected back to your callback, this parameter is being passed from twitter). 但是在那种情况下,您需要传递2个参数: oauth_token这是您从iOS上的反向身份验证获取的请求令牌)和oauth_verifier参数,我不知道如何在iOS上获得该参数(使用标准网络流,用户被重定向回您的回调,此参数从Twitter传递)。

  • A downside with reverse auth seems to be that you need to embed the App secret in your application binary. 反向身份验证的缺点似乎是您需要将应用程序秘密嵌入到应用程序二进制文件中。 It looks like a good idea to perform this step server-side (I saw the idea here: https://github.com/drudge/passport-twitter-token/#performing-twitter-reverse-auth-step-1-server-side ) 在服务器端执行此步骤似乎是一个好主意(我在这里看到了主意: https : //github.com/drudge/passport-twitter-token/#performing-twitter-reverse-auth-step-1-server边

I just created a UIWebview when the user clicks on the 'Sign up through twitter' button on in your ios which then follows the same process as my rails app. 当用户单击ios中的“通过Twitter注册”按钮时,我刚刚创建了一个UIWebview,然后按照与我的Rails应用程序相同的过程进行操作。

Once the user has signed up through twitter, all handled by the server, you can then redirect back into your ios app signed up and logged in thus not having to handle all the tokens and requests in the app. 一旦用户通过Twitter进行了注册(服务器处理了所有操作),您就可以重定向回已注册并登录的ios应用程序,从而不必处理应用程序中的所有令牌和请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM