Unified iOS client and server-side authentication

Source: Internet
Author: User

Recently the company's colleagues in the spare time to engage in an internal class about.me (https://about.me/) site ocelots, want to be a very foreign flavor attention, after the introduction of the company with customers, directly login to the site, talk about who when, on the open the person's homepage, photos, Experience, hobbies and so on are all clear. I began to think that if it was such a tool, no mobile end much regret, because we at any time, any occasion there is a need to show the customer team members.

After setting up the project framework, the first requirement was unified certification, Ocelots used Google Oauth2 and Mozilla persona as the portal. The structure of the authentication method is as follows (only Google Oauth2 for example, Mozilla Persona principle is the same):


Google Oauth2 also provides support for iOS systems, so it is easy to implement an authentication mechanism that is identical to ocelots on the Ocelots_ios client, but the challenge is how to unify the authentication mechanism for both.
By studying the authentication mechanism of Google Oauth2, I found that the authentication mechanism is as follows:


Therefore, we can unify the authentication mechanism of Ocelots_ios client and Ocelots side through the following steps,
1. Register the Application ID for Ocelots_ios and ensure that the authorization scope of the Ocelots_ios and ocelots applications is consistent.
2, for ocelots user binding a Authorize_token, through the token can be obtained to the account in the system of all the information.
3, according to the following way to achieve the Ocelots_ios client authentication mechanism:

The code is as follows:
Use the Google Oauth2 plugin to call the Google Authentication interface:

Object-c Code
  1. -(void) Auththroughgoogle
  2. {
  3. NSString *clientid = google_client_id;
  4. NSString *clientsecret = Google_client_secret;
  5. NSString *scope = Google_auth_scope;
  6. Gtmoauth2viewcontrollertouch *authviewcontroller = [[[Gtmoauth2viewcontrollertouch alloc] InitWithScope:scope Clientid:clientid Clientsecret:clientsecret Keychainitemname:kkeychaingoogleauth delegate:self finishedSelector:@ Selector (viewController:finishedWithAuth:error:)]autorelease];
  7. NSString *html = @"
  8. authviewcontroller.initialhtmlstring = html;
  9. [Self.navigationcontroller Pushviewcontroller:authviewcontroller Animated:yes];
  10. }



After obtaining the authentication result, obtain the Auth Token from the server side:

Object-c Code
  1. -(void) Viewcontroller: (Gtmoauth2viewcontrollertouch *) Viewcontroller Finishedwithauth: (gtmoauth2authentication *) Auth Error: (nserror *) error{
  2. if (Error!=nil) {
  3. NSLog (@"Auth failed!");
  4. [Self showalertview:[error localizeddescription]];
  5. }else{
  6. NSLog (@"Auth successed!:%@", [Auth Accesstoken]);
  7. NSString *token = [Authhelper getauthtokenthroughgoogle:[auth Accesstoken];
  8. if (token! = nil) {
  9. [[Nsuserdefaults Standarduserdefaults] Setobject:token forkey:app_name];
  10. [Self gotomainpage];
  11. }else{
  12. [Self showalertview:@"Get get the Authorize token"];
  13. }
  14. }
  15. }

Unified iOS client and server-side authentication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.