Liferay-OAuth 1.2.1

Liferay-OAuth 1.2.1

TestsTested
LangLanguage Obj-CObjective C
License Custom
ReleasedLast Release Mar 2016

Maintained by Bruno Farache.



  • By
  • Bruno Farache

Liferay iOS SDK OAuth Library

Setup

The OAuth Provider portlet must be installed on your Liferay Portal in order to enable authentication with OAuth. This portlet is currently available for EE customers only.

Read its documentation to generate the consumer key and secret for your app, they will be used by the Mobile SDK to sign your requests and we will refer to them in this document.

This library is a CocoaPods subspec of the Mobile SDK for iOS. This library becomes available once you install the Mobile SDK as a dependency. See the Mobile SDK documentation to learn how to include it as a dependency to your project.

Use

Create a LRSession instance passing a LROAuth instance:

LROAuth *auth = [[LROAuth alloc] initWithConsumerKey:consumerKey
    consumerSecret:consumerSecret token:token tokenSecret:tokenSecret];
LRSession *session = [[LRSession alloc]
    initWithServer:@"http://localhost:8080" authentication:auth];

LRGroupService_v62 *service = [[LRGroupService_v62 alloc]
    initWithSession:session];

NSArray *sites = [service getUserSites:&error];

As you can see, you need to pass consumerKey and consumerSecret to the LROAuth constructor. These parameters are tied to your app and, as mentioned earlier, must be generated by the OAuth Provider portlet.

token and tokenSecret are the tokens required by the OAuth 1.0a protocol. They are used to identify the user once he has granted permission to your app. In order to obtain them, the user needs to authenticate through the OAuth flow, that is, your app must open a web browser showing the portal's login page and user needs to login and grant permission to your app.

You can implement that part yourself, using any available iOS OAuth 1.0a library. Alternatively, we provide helper classes to obtain token and tokenSecret. See the sections bellow.

External Browser

The instructions bellow give you an idea of the required steps to authenticate using an external browser. It's very important that you read and run the sample app.

This will open the user's favorite mobile browser and the authentication flow will happen there as opposed to inside the app.

From your ViewController you must start the OAuthActivity, passing a OAuthConfig instance as an intent extra:

OAuthConfig config = new OAuthConfig(server, consumerKey, consumerSecret);
Intent intent = new Intent(this, OAuthActivity.class);
intent.putExtra(OAuthActivity.EXTRA_OAUTH_CONFIG, config);
startActivityForResult(intent, 1);

If everything goes fine, an external web browser will open Liferay's login page and will ask the credentials to the user.

Once user has sucessfully authenticated and granted permission to your app, the onActivityResult method in your Activity will be called. Likewise, if the user hasn't granted permission or something went wrong, onActivityResult will be also called:

@Override
    public void onActivityResult(int request, int result, Intent intent) {
        if (result == RESULT_OK) {
            OAuthConfig config = (OAuthConfig)intent.getSerializableExtra(
                OAuthActivity.EXTRA_OAUTH_CONFIG);

            String consumerKey = config.getConsumerKey();
            String consumerSecret = config.getConsumerSecret();
            String token = config.getToken();
            String tokenSecret = config.getTokenSecret();

            // Create an OAuth instance with these values and pass to
            // the SessionImpl constructor
        }
        else if (result == RESULT_CANCELED) {
            Exception exception = (Exception)intent.getSerializableExtra(
                OAuthActivity.EXTRA_EXCEPTION);

            exception.printStackTrace();
        }
    }

Check the result parameter to see if authentication was sucessful or not. If successful, get the OAuthConfig extra from the intent, it provides all 4 values required by SessionImpl to authenticate against Liferay's remote services. Read the use section above to learn how use the Mobile SDK's services with these values.

In case of failure, the intent will contain a Exception extra with the error cause.