Commit 0f997969 by chenith

LinkedIn login for applicant portal.

parent b46f18a0
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package performa.form;
import javax.servlet.http.HttpServletRequest;
import oneit.appservices.config.ConfigMgr;
import oneit.security.oauth.form.BaseOAuthLoginFP;
import oneit.servlets.forms.*;
import oneit.utils.CollectionUtils;
/**
*
* @author Pradip Sabhadiya
*/
public class LinkedInOAuthLoginFP extends BaseOAuthLoginFP
{
public static final String LINKEDIN_CLIENTID_ATTR_NAME = "linkedin.clientId";
public static final String LINKEDIN_CLIENTSECRET_ATTR_NAME = "linkedin.clientSecret";
public static final String LINKEDIN_APP_OAUTH_URL = "https://www.linkedin.com/oauth/v2/authorization";
public static final String LINKEDIN_ACCESS_TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken";
public static final String LINKEDIN_PROFILE_FROM_TOKEN_URL = "https://api.linkedin.com/v1/people/~";
@Override
public String getOAuthLoginURL(SubmissionDetails submission, String token)
{
String appId = ConfigMgr.getKeyfileString(LINKEDIN_CLIENTID_ATTR_NAME);
String callbackURL = getCallbackURL(submission.getRequest());
return LINKEDIN_APP_OAUTH_URL
+ HTTPRequestDetails.getParamStringURL(CollectionUtils.mapEntry("client_id", appId)
.mapEntry("redirect_uri", callbackURL)
.mapEntry("scope", "r_basicprofile")
.mapEntry("response_type", "code")
.mapEntry("state", token).toMap());
}
public static String getCallbackURL(HttpServletRequest request)
{
String urlFirstBit = request.getScheme () + "://" + request.getServerName () + ":" + request.getServerPort ();
return HTTPRequestDetails.getFullURL(urlFirstBit + request.getContextPath() + "/linkedinCallback", new String[0]);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package performa.form;
import java.io.*;
import java.net.*;
import javax.servlet.http.HttpServletRequest;
import oneit.appservices.config.ConfigMgr;
import oneit.components.ParticipantInitialisationContext;
import oneit.logging.*;
import oneit.security.oauth.decorator.OAuthCallbackDecorator;
import oneit.security.oauth.utils.BaseOAuthLoginHandler;
import oneit.servlets.forms.HTTPRequestDetails;
import oneit.utils.*;
import org.json.*;
/**
*
* @author Pradip Sabhadiya
*/
public class LinkedInOAuthLoginHandler extends BaseOAuthLoginHandler
{
protected static final LoggingArea LOG = LoggingArea.createLoggingArea("BaseOAuthLoginHandler");
@Override
public void validateRequest(HttpServletRequest request)
{
LogMgr.log(LOG, LogLevel.DEBUG3, "Validation LinkedIn callback request");
String state = request.getParameter("state");
String token = (String) request.getSession().getAttribute(OAuthCallbackDecorator.TOKEN_ATTRIB_NAME);
Debug.assertion(CollectionUtils.equals(state, token), "Unothorized access to callback url.");
}
@Override
public OAuthLoginInfo getProfileInfo(HttpServletRequest request) throws Exception
{
LogMgr.log(LOG, LogLevel.DEBUG3, "Getting profile data from callback");
String accessToken = getAccessToken(request);
try
{
String profileURL = LinkedInOAuthLoginFP.LINKEDIN_PROFILE_FROM_TOKEN_URL
+ HTTPRequestDetails.getParamStringURL(CollectionUtils.mapEntry("oauth2_access_token", accessToken).mapEntry("format", "json").toMap());
JSONObject json = executeURL(profileURL);
LogMgr.log(LOG, LogLevel.PROCESSING1, "Profile Data found ", json);
OAuthLoginInfo loginInfo = new OAuthLoginInfo();
loginInfo.setId(json.getString("id"));
loginInfo.setFirstName(json.getString("firstName"));
if (json.has("email"))
{
loginInfo.setEmail(json.getString("email"));
}
if (json.has("lastName"))
{
loginInfo.setLastName(json.getString("lastName"));
}
return loginInfo;
}
catch (Exception e)
{
LogMgr.log(LOG, LogLevel.SYSTEMERROR1, "Exception occured in getProfileInfo");
throw new NestedException(e, "ERROR while getting user data.");
}
}
public String getAccessToken(HttpServletRequest request) throws Exception
{
String code = request.getParameter("code");
String appId = ConfigMgr.getKeyfileString(LinkedInOAuthLoginFP.LINKEDIN_CLIENTID_ATTR_NAME);
String appSecret = ConfigMgr.getKeyfileString(LinkedInOAuthLoginFP.LINKEDIN_CLIENTSECRET_ATTR_NAME);
String callbackURL = LinkedInOAuthLoginFP.getCallbackURL(request);
@SuppressWarnings("unchecked")
String accessTokenURL = LinkedInOAuthLoginFP.LINKEDIN_ACCESS_TOKEN_URL
+ HTTPRequestDetails.getParamStringURL(CollectionUtils.mapEntry("grant_type", "authorization_code")
.mapEntry("code", code)
.mapEntry("redirect_uri", callbackURL)
.mapEntry("client_id", appId)
.mapEntry("client_secret", appSecret).toMap());
JSONObject data = executeURL(accessTokenURL, "POST");
if(!data.has("access_token"))
{
throw new RuntimeException("ERROR: Access Token Invalid: " + data);
}
return data.getString("access_token");
}
public void init(ParticipantInitialisationContext context) throws InitialisationException
{
context.setObject(this);
}
public static JSONObject executeURL(String urlString)
{
return executeURL(urlString, null);
}
public static JSONObject executeURL(String urlString, String method)
{
LogMgr.log(LoggingArea.ALL, LogLevel.DEBUG3, "Executing url ", urlString);
try
{
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
if(StringUtils.subBlanks(method) != null && method.toUpperCase().equals("POST"))
{
urlConnection.setDoOutput(true);
}
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
return new JSONObject(IOUtils.readerToString(in));
}
catch (IOException e)
{
LogMgr.log(LoggingArea.ALL, LogLevel.SYSTEMERROR1, "Error in Executing url ", urlString, e);
throw new NestedException(e);
}
catch(JSONException je)
{
LogMgr.log(LoggingArea.ALL, LogLevel.SYSTEMERROR1, "Error in Parsing Json Data", urlString, je);
throw new NestedException(je);
}
}
}
......@@ -124,10 +124,10 @@
<NODE name="helper" factory="Named" nodename="SetupUserHelper"/>
<NODE name="serviceName" factory="String" value="GOOGLE"/>
</Handler>
<!-- <Handler name="/linkedinCallback" factory="Participant" class="performa.form.LinkedInOAuthLoginHandler">
<Handler name="/linkedinCallback" factory="Participant" class="performa.form.LinkedInOAuthLoginHandler">
<NODE name="helper" factory="Named" nodename="SetupUserHelper"/>
<NODE name="serviceName" factory="String" value="LINKEDIN"/>
</Handler>-->
</Handler>
</DECORATOR>
</NODE>
......
......@@ -15,7 +15,7 @@
</FORM>
<FORM name="*.facebookOAuthLogin" factory="Participant" class="oneit.security.oauth.form.FacebookOAuthLoginFP"/>
<FORM name="*.googleOAuthLogin" factory="Participant" class="oneit.security.oauth.form.GoogleOAuthLoginFP"/>
<!--<FORM name="*.linkedinOAuthLogin" factory="Participant" class="performa.form.LinkedInOAuthLoginFP"/>-->
<FORM name="*.linkedinOAuthLogin" factory="Participant" class="performa.form.LinkedInOAuthLoginFP"/>
<FORM name="*.forgotPassword" factory="Participant" class="performa.form.ForgotPasswordFP">
<ResetCodeEmailer factory="Participant" class="oneit.email.ConfigurableArticleTemplateEmailer" templateShortcut="ResetCodeEmail"/>
</FORM>
......
Add followings to keyfile.properties
linkedin.clientId=81eohoovrfda10
linkedin.clientSecret=jXDHcrXYDirPygCf
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment