Giter Site home page Giter Site logo

liferay-ios-sdk-oauth's Introduction

Liferay Mobile SDK logo

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.

liferay-ios-sdk-oauth's People

Stargazers

Hector De Diego avatar

Watchers

Bruno Farache avatar

Forkers

allanragec

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.