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
- -(void) Auththroughgoogle
- {
- NSString *clientid = google_client_id;
- NSString *clientsecret = Google_client_secret;
- NSString *scope = Google_auth_scope;
- Gtmoauth2viewcontrollertouch *authviewcontroller = [[[Gtmoauth2viewcontrollertouch alloc] InitWithScope:scope Clientid:clientid Clientsecret:clientsecret Keychainitemname:kkeychaingoogleauth delegate:self finishedSelector:@ Selector (viewController:finishedWithAuth:error:)]autorelease];
- NSString *html = @"
- authviewcontroller.initialhtmlstring = html;
- [Self.navigationcontroller Pushviewcontroller:authviewcontroller Animated:yes];
- }
After obtaining the authentication result, obtain the Auth Token from the server side:
Object-c Code
- -(void) Viewcontroller: (Gtmoauth2viewcontrollertouch *) Viewcontroller Finishedwithauth: (gtmoauth2authentication *) Auth Error: (nserror *) error{
- if (Error!=nil) {
- NSLog (@"Auth failed!");
- [Self showalertview:[error localizeddescription]];
- }else{
- NSLog (@"Auth successed!:%@", [Auth Accesstoken]);
- NSString *token = [Authhelper getauthtokenthroughgoogle:[auth Accesstoken];
- if (token! = nil) {
- [[Nsuserdefaults Standarduserdefaults] Setobject:token forkey:app_name];
- [Self gotomainpage];
- }else{
- [Self showalertview:@"Get get the Authorize token"];
- }
- }
- }
Unified iOS client and server-side authentication