How to authenticate iOS/iPhone users with remote web application and re-use authentication ticket in future requests to the same web application?
I am building an iOS application and I need to be able to make authenticated requests to a Rails 3 application for various bits of data. The Rails 3 application is using omniauth and URLs like https://myapp.com/auth/facebook to, for example, authenticate users via facebook … and once authenticated, stores the authentication in a secured cookie named “auth.””
What I want to know is how to authenticate my users from the iOS/iPhone application, persist the authentication token and send it along with future requests to the Rails application?
Using ASIHTTPRequest I’m thinking of doing something like this:
Open a UIWebview, loading with a URL from my web application specific for the provider they want to authenticate with (e.g. myapp.com/auth/facebook for facebook or myapp.com/auth/yahoo for yahoo, etc….).
On success, somehow parse out and store the authentication cookie in the iOS application without displaying the webpage folks usually see when authenticating via the website … and instead closing the UIWebView and navigating back to another UIVewController in the iOS application.
Somehow include the persisted authentication token with future web requests to the Rails application.
I also want to allow users to allow the iOS application to store this information locally so they don’t have to re-login to the remote application if they choose too.
Is this approach appropriate? Is there a better way? And of course, how to actually implement the above?
Thanks – wg
4 Solutions Collect From Internet About “How to authenticate iOS/iPhone users with remote web application and re-use authentication ticket in future requests to the same web application?”
Using OAuth is pretty easy (well, easy is not the word…), but I made an iOS application and a java server that use OAUth as identity schema and, following the full cycle, finally I adquired a token that identifies this user and (as only can be accessed using signed requests) can be safely stored in the phone (I use just the standardUserDefaults to store it). Only your application (using the secret) can sign the requests.
I don’t know if this serves to you…
Ah! After the identification via web, the browser redirect a special url (registered for my application) and the url opens my application including the token in its parameters, so it is easy to retrieve the token after the identification phase in handleOpenURL.
Capture this request using a
UIWebViewDelegateobject which implements the following protocol method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
From here you have the
NSURLRequestobject. You can extract the headers of the request to
NSDictionarywhich will contain the authentication cookie details, token, etc. using the following method of
- (NSDictionary *)allHTTPHeaderFields
For my app this is what I’m doing.
My app is using devise with omniauth for login and user stuff.
Devise by itself can generate a unique token, with the flag token_authenticatable.
So on my login request, if the login is successful I reply with a JSON representation of my user and my user token. I save all that on the phone memory.
Then on every request I add the param auth_token=MY_USER_TOKEN.
And that’s about it.
I had just a problem with the Facebook auth, because I’m using the Ios facebook SDK, so I forward the FB token to my app, verify it, and then just return the same devise auth_token for all following requests.
Ok here we go, I dont know the exact setup of your web service and all that, but what you can do is store the authentication token on the device using SQLite or Core Data, I am currently working on a app that requires authentication, and what I do is store the username and password locally on the device in the SQLite db using Core Data to interact with the db, then whenever I make an API calls I use the stored username and password for the authentication on the server side using gets, but I believe it is saver using post, as long as the web server has great security I don’t believe there is any security risks. In what I understand about what you are building I would authenticate the user say on first launch and have the user be able to change log in credentials at a later stage, but after authentication I would send back an authentication token to the device and store that in the db and then whenever I need to authenticate with the web service I would send the auth token with a post request to the server. Does this make sense?
- Calling dealloc in init?
- UIWebView webCore crashes in iOS7.1 (example: www.latimes.com)
- iOS: Google Maps API – markerInfoWindow vs markerInfoContents
- How to pass a NSDictionary with postNotificationName:object:
- Remove Gray UITableView Index Bar
- Draw hole on UIBlurEffect
- Horizantal slider with tick marks on iOS
- How to show resized sidebar using SWRevealViewController?
- How to open Settings programmatically like in Facebook app?
- Back button don't appear in navigationController
- How to get the address of a latitude and longitude in an iphone application
- Convert html file to PDF Document in iOS using Cocoa-Touch
- Render water-paint in iOS
- How to add Streetview function in my MKMapView?
- How to get user details using twitter api v1.1 (Twitter error 215)