1
votes

I implemented the OAuth2 authentication using Apache OLTU libraries. It works but I handle manually the request token from RedirectURL.

  1. step:

    request = OAuthClientRequest.authorizationProvider(OAuthProviderType.GOOGLE)//authorizationProvider(OAuthProviderType.GOOGLE) .setState(OAuth.OAUTH_STATE) .setResponseType(OAuth.OAUTH_CODE) .setRedirectURI("http://localhost:8080") .setClientId(clientId) .setScope("https://www.googleapis.com/auth/drive") .buildQueryMessage();

  2. Step:

    OAuthClientRequest oAuthClientRequest = OAuthClientRequest.tokenProvider(OAuthProviderType.GOOGLE) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI("http://localhost:8080") .setCode(requestCode).buildBodyMessage();

Between the two-step, I need to handle automatically the extraction of the code. How can I implement in code this step?

I'm wont it not in a servlet, but in a Portlet.

1

1 Answers

1
votes

Question "How can I implement in code this step?"

Requirement "I'm wont it not in a servlet, but in a Portlet."

Answer:

(1) For your reference I add the source code into your source code (using Apache OLTU libraries) to "handle automatically the extraction of the OAuth authorization code."

// 1. Step
OAuthClientRequest request = OAuthClientRequest.authorizationProvider(OAuthProviderType.GOOGLE)//authorizationProvider(OAuthProviderType.GOOGLE) .setState(OAuth.OAUTH_STATE) .setResponseType(OAuth.OAUTH_CODE) .setRedirectURI("http://localhost:8080") .setClientId(clientId) .setScope("https://www.googleapis.com/auth/drive") .buildQueryMessage();


// Create the response wrapper
OAuthAuthzResponse oar = null;
oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);

// Get Authorization Code
String requestCode = oar.getCode();


// 2. Step 
OAuthClientRequest oAuthClientRequest = OAuthClientRequest.tokenProvider(OAuthProviderType.GOOGLE) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI("http://localhost:8080") .setCode(requestCode).buildBodyMessage();

(2) More details can be referred to the following sample code

"demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/RedirectController.java"

from Apache Oltu OAuth 2.0 Client and Provider at GitHub repository, which is a fork of Apache Oltu with Pull Request #10 for two new commits "Add provider demo and README".

The provider demo application ("demos/provider-demo" from Apache Oltu OAuth 2.0 Client and Provider at GitHub repository) allows you to run a standalone OAuth 2.0 server to test and debug your OAuth2 authentication client (implemented by you in a Portlet).