I have a ruby on rails application that currently supports facebook authentication with devise / omniauth.
An iOS client is being built that needs to support facebook authentication also.
My question is, how do I use the access token server side to get the user's email outside of an omniauth callback?
My understanding is that omniauth provides some middleware that on a facebook callback writes an auth hash containing all information to request.env['omniauth.auth']
See: https://github.com/mkdynamic/omniauth-facebook
With an iOS client the flow is a little different, I think:
The user signs into facebook on the client.
The user agrees to give the app access.
The client app posts access_token to the rails API.
Rails app validates access token with facebook and retrieves users details.
It is step 4 that I'm not sure how to do.
Essentially once I have an access token how do I get an auth hash manually when I'm not in an omniauth callback?
Thanks for any help.
Try to use gem "Koala"
Example:
#graph = Koala::Facebook::API.new(oauth_access_token)
profile = #graph.get_object("me")
After much trial and error found the following way to get the user's email using the Koala gem:
#graph = Koala::Facebook::API.new(TOKEN, APP_SECRET)
#graph.get_object('me', :fields => ['email'])
Related
I have a Rest API written in Ruby on Rails which is having a regular SignUp and SignIn via a username and password. The SignUp and SignIn flows are currently handled through "devise" and "simple_token_authentication" Ruby Gems.
Now I need to integrate Facebook and Google Login to the existing SignUp and SignIn flows. Therefore, I'm looking for a recommendation to implement this easily.
I have already done some research and found, that I could use Koala for Facebook integration and I personally feel like Koala is a heavy Gem only to handle Facebook Login.
Would you recommend using it or does anyone else have an alternative approach. Or even writing a custom code to validate the Facebook access token in the server end sending back the application specific authentication token back to the client would be better.
PS : Is there a possibility of using the Omniauthable module in Devise to authenticate Facebook and Google users via the REST API.
in the working project on rails 4 is used omniauth gem for social authorization, connected some social networks
and the appropriate gems are used
omniauth-facebook
omniauth-instagram
omniauth-google-oauth2
now there was a task to add api for mobile authorization, where the scheme is somewhat different:
client starts oauth flow w/ Facebook (using login button etc)
client gets access token and posts back to server
server looks up user via (FB/Instagram/Google) api call w/ token
server does lookup/create of user based on uid & provider
user is logged in if a user association lookup is successful
Help me please how to use received access token from mobile in omniauth and not duplicate the functionality
I solved the problem through monkey patching build_access_token method, in this method i check the presence params[:access_token] parameter
I use rails as backend for ios applications. So far i have been using devise as it looks flexible and comfortable to use with less effort . And i have read about omniauth and that too looks easy to integrate with devise. My question is, consider my ios app requires authentication and the following are the different methods that i should be able to allow user to do
Login using email and password
Login using Facebook account
Login using Twitter account
Login using email can be handled by the devise itself but how about login using Facebook and twitter? Actually in one of my project i came up with the following approach which has all three of these login process. The ios app authenticates the user from the device(not devise) itself and sends the user information like username, email etc whatever required along with auth type so i save this a separate user with username that is sent and one of the field as password. And the next time he sends me these details i allow him to login to the app. But now i realised this is not the best way to do. I read about FBgraph which can be used to verify the access token validity, so should i get the token from user and then verify it and get the profile information and save it in user model and give them the token.
Also i have another doubt which is, For login using email and password i allow user to login through email and password and then for the each requests the user sends me the username and password. Is this is alright or do i have to create a token in login request and send the token as response and then the user can send the token for all the other request he makes.
Sorry if it is confusing but to tell you shortly i need to know what should i do if i have all these three login process. Any help is greatly appreciated. Thankyou
There are couple things to consider when dealing with external applications like on other devices:
You should use an API to communicate with your Rails server
Your server should send an authentication token after the first user authentication using his email and password. It is not a good idea to send user's email and password for each requests.
Devise
Devise is great for authentication both in-app and for remote applications using the token_authenticatable hook. This will allow any registered user to have a unique secret token to use in order to be authenticated on your server.
More information here
OAuth2
OAuth2 is becoming the standard way to authenticate on remote services giving the user the possibility to use his Facebook account to login for example.
This would be the easier way to allow your users to authenticate using their Facebook or Twitter account (note that twitter will not give you the user's email address).
Devise can handle OAuth2 clients.
Finally, you could also create your own OAuth consumer to authenticate users using your service. Doorkeeper is a great gem to protect your API endpoints and to allow users to use OAuth2.
Conclusion
Devise is great for authentication. Using their token module coupled with OAuth2 integration could do the trick in your case.
Here is the OmniAuth wiki page from Devise
Here is the Simple Token Authentication wiki page from Devise
I have an API and a Web app working with Rails and Devise gem for authentication.
With Devise and Omniauth I can make the Signup with facebook and Login with Facebook through the web site.
I have my API protected with token_authenticable and a TokensController, so when you want to interact with my API first you have to pass to the TokensController your username and password and it will give you a valid token, my problem is what happen when the user is sign up with facebook, Shall I pass to the TokensController the username and the facebook token to generate the token? are there some standard way to do this?
Thanks for the comments.
In my opinion you can pick whatever you want, as long as it fits your criteria:
it's secret
it's retrievable for you
it has a minimum security (not just '123pw')
So you have two similar options:
Use the Facebook Token as 'password'
Use the password generated by Devise
Normally you generate a password when creating a Facebook user with Devise. I guess you have code like :password => Devise.friendly_token[0,20] in your User.create-call?
The encrypted password is normally 60 characters, the Facebook token can have up to 255 characters. So I personally would pick the Devise generated password. It's long enough to be safe and you don't depend on an API to retrieve it.
Thanks for the comments; After thinking which one would be the best option, Here is what I finally did:
Note: I separate the code in client and server side(API)
Client Side:
Get the Facebook token and the Facebook user ID for a certain user.
Call the API method Create method from TokensController, with the parameters I got above.
Server side:
I receive the Facebook token and the Facebook User ID.
Using the Facebook Token I get the Facebook User ID from the API.
I validate that the receive UID from the request and the Facebook UID are the same.
If the UID are the same I generate a random token for my app and I return it in the response.
Note: All the communication is done via HTTPS.
I am using Rails with omniauth plugin to authenticate my application via LinkedIn. Currently, I store the linkedin token which omniauth returns if the user successfully authorize the application:
oauth_token"=>"9dd623ad-1e21-2cec-9ede-107e1f8e9e18"
I am also using linkedin plugin to get user's Linkedin information. The problem is; the plugin requires two tokens like the following:
client.authorize_from_request(rtoken, rsecret, pin)
c.authorize_from_access("OU812", "8675309")
How can I use the omniauth's token in linkedin plugin? Any idea?
OMNIAUTH is for Authentication only. AFAIK you wont be able to use the API with that oauth token alone. Youll still need to send the user off to linked in to authorize API access by doing something like
client.request_token(:oauth_callback =>'your callback url')
See example here:
http://pivotallabs.com/users/will/blog/articles/1096-linkedin-gem-for-a-web-app
Update:
I was able to reuse access token and access secret that I received upon Omniauth callback from LinkedIn and pass it on to client.authorize_from_access and got access to LinkedIn API that way. Your mileage may vary as I had to customize linked_in gem to fit my workflow a bit, but once Omniauth gets access token and secret you no longer need to call client.authorize_from_request