Java Code Examples for org.brickred.socialauth.Profile#setFirstName()

The following examples show how to use org.brickred.socialauth.Profile#setFirstName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenIdConsumer.java    From socialauth with MIT License 5 votes vote down vote up
/**
 * Parses the user info from request
 * 
 * @param requestParams
 *            request parameters map
 * @return User Profile
 */
public static Profile getUserInfo(final Map<String, String> requestParams) {
	Profile p = new Profile();
	p.setEmail(requestParams.get("openid.ext1.value.email"));
	p.setFirstName(requestParams.get("openid.ext1.value.firstname"));
	p.setLastName(requestParams.get("openid.ext1.value.lastname"));
	p.setCountry(requestParams.get("openid.ext1.value.country"));
	p.setLanguage(requestParams.get("openid.ext1.value.language"));
	p.setValidatedId(requestParams.get("openid.identity"));
	return p;
}
 
Example 2
Source File: InstagramImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	LOG.debug("Obtaining user profile");
	Response response;
	try {
		response = authenticationStrategy.executeFeed(PROFILE_URL);
	} catch (Exception e) {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from " + PROFILE_URL,
				e);
	}

	if (response.getStatus() == 200) {
		String respStr = response
				.getResponseBodyAsString(Constants.ENCODING);
		LOG.debug("Profile JSON string :: " + respStr);
		JSONObject obj = new JSONObject(respStr);
		JSONObject data = obj.getJSONObject("data");
		Profile p = new Profile();
		p.setValidatedId(data.optString("id", null));
		String full_name = data.optString("full_name", null);
		p.setDisplayName(full_name);
		if (full_name != null) {
			String[] names = full_name.split(" ");
			if (names.length > 1) {
				p.setFirstName(names[0]);
				p.setLastName(names[1]);
			} else {
				p.setFirstName(full_name);
			}
		}
		p.setProfileImageURL(data.optString("profile_picture", null));
		p.setProviderId(getProviderId());
		if (config.isSaveRawResponse()) {
			p.setRawResponse(respStr);
		}
		return p;
	} else {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from " + PROFILE_URL
						+ ". Server response " + response.getStatus());
	}
}
 
Example 3
Source File: RunkeeperImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	String presp;
	try {
		Map<String, String> hmap = new HashMap<String, String>();
		hmap.put("Accept", "application/vnd.com.runkeeper.Profile+json");
		Response response = authenticationStrategy.executeFeed(PROFILE_URL,
				MethodType.GET.toString(), null, hmap, null);
		presp = response.getResponseBodyAsString(Constants.ENCODING);
	} catch (Exception e) {
		throw new SocialAuthException("Error while getting profile from "
				+ PROFILE_URL, e);
	}
	try {
		LOG.debug("User Profile : " + presp);
		JSONObject resp = new JSONObject(presp);
		Profile p = new Profile();
		if (resp.has("profile")) {
			String purl = resp.getString("profile");
			String parr[] = purl.split("/");
			p.setValidatedId(parr[parr.length - 1]);
		}
		if (resp.has("name")) {
			p.setFirstName(resp.getString("name"));
			p.setFullName(resp.getString("name"));
		}

		if (resp.has("location")) {
			p.setLocation(resp.getString("location"));
		}
		if (resp.has("birthday")) {
			String bstr = resp.getString("birthday");
			if (bstr != null) {
				if (bstr.matches("[A-Za-z]{3}, \\d{1,2} [A-Za-z]{3} \\d{4} \\d{2}:\\d{2}:\\d{2}")) {
					DateFormat df = new SimpleDateFormat(
							"EEE, dd MMM yyyy hh:mm:ss");
					Date d = df.parse(bstr);
					Calendar c = Calendar.getInstance();
					c.setTime(d);
					BirthDate bd = new BirthDate();
					bd.setDay(c.get(Calendar.DAY_OF_MONTH));
					bd.setYear(c.get(Calendar.YEAR));
					bd.setMonth(c.get(Calendar.MONTH) + 1);
					p.setDob(bd);
				}
			}
		}
		if (resp.has("gender")) {
			p.setGender(resp.getString("gender"));
		}
		if (resp.has("normal_picture")) {
			p.setProfileImageURL(resp.getString("normal_picture"));
		}
		p.setProviderId(getProviderId());
		userProfile = p;
		return p;

	} catch (Exception ex) {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + presp, ex);
	}
}
 
Example 4
Source File: GooglePlusImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	String presp;

	try {
		Response response = authenticationStrategy.executeFeed(PROFILE_URL);
		presp = response.getResponseBodyAsString(Constants.ENCODING);
	} catch (Exception e) {
		throw new SocialAuthException("Error while getting profile from "
				+ PROFILE_URL, e);
	}
	try {
		LOG.debug("User Profile : " + presp);
		JSONObject resp = new JSONObject(presp);
		Profile p = new Profile();
		p.setValidatedId(resp.optString("id", null));
		p.setFullName(resp.optString("name", null));
		p.setFirstName(resp.optString("given_name", null));
		p.setLastName(resp.optString("family_name", null));
		p.setEmail(resp.optString("email", null));
		p.setGender(resp.optString("gender", null));
		p.setProfileImageURL(resp.optString("picture", null));
		if (config.isSaveRawResponse()) {
			p.setRawResponse(presp);
		}
		String locale = resp.optString("locale", (String)null);
		if(locale != null) {
			if (locale.contains("_")) {
				String[] a = locale.split("_");
				p.setLanguage(a[0]);
				p.setCountry(a[1]);
			} else {
				p.setLanguage(locale);
			}
		}

		p.setProviderId(getProviderId());
		userProfile = p;
		return p;

	} catch (Exception ex) {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + presp, ex);
	}
}
 
Example 5
Source File: FourSquareImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	LOG.debug("Obtaining user profile");
	Profile profile = new Profile();
	Response serviceResponse;
	try {
		serviceResponse = authenticationStrategy.executeFeed(PROFILE_URL);
	} catch (Exception e) {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from  " + PROFILE_URL,
				e);
	}
	String res;
	try {
		res = serviceResponse.getResponseBodyAsString(Constants.ENCODING);
	} catch (Exception exc) {
		throw new SocialAuthException("Failed to read response from  "
				+ PROFILE_URL, exc);
	}

	JSONObject jobj = new JSONObject(res);
	JSONObject rObj;
	JSONObject uObj;
	if (jobj.has("response")) {
		rObj = jobj.getJSONObject("response");
	} else {
		throw new SocialAuthException(
				"Failed to parse the user profile json : " + res);
	}
	if (rObj.has("user")) {
		uObj = rObj.getJSONObject("user");
	} else {
		throw new SocialAuthException(
				"Failed to parse the user profile json : " + res);
	}
	profile.setValidatedId(uObj.optString("id", null));
	profile.setFirstName(uObj.optString("firstName", null));
	profile.setLastName(uObj.optString("lastName", null));
	profile.setProfileImageURL(uObj.optString("photo", null));
	profile.setGender(uObj.optString("gender", null));
	profile.setLocation(uObj.optString("homeCity", null));
	if (uObj.has("contact")) {
		JSONObject cobj = uObj.getJSONObject("contact");
		if (cobj.has("email")) {
			profile.setEmail(cobj.optString("email", null));
		}
	}
	profile.setProviderId(getProviderId());
	if (config.isSaveRawResponse()) {
		profile.setRawResponse(res);
	}
	userProfile = profile;
	return profile;
}
 
Example 6
Source File: MySpaceImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	LOG.debug("Obtaining user profile");
	Profile profile = new Profile();

	Response serviceResponse = null;
	try {
		serviceResponse = authenticationStrategy.executeFeed(PROFILE_URL);
	} catch (Exception e) {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from  " + PROFILE_URL,
				e);
	}
	if (serviceResponse.getStatus() != 200) {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from  " + PROFILE_URL
						+ ". Staus :" + serviceResponse.getStatus());
	}

	String result;
	try {
		result = serviceResponse
				.getResponseBodyAsString(Constants.ENCODING);
		LOG.debug("User Profile :" + result);
	} catch (Exception exc) {
		throw new SocialAuthException("Failed to read response from  "
				+ PROFILE_URL, exc);
	}
	JSONObject pObj = new JSONObject();
	JSONObject jobj = new JSONObject(result);
	if (jobj.has("person")) {
		pObj = jobj.getJSONObject("person");
	} else {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + result);
	}
	profile.setDisplayName(pObj.optString("displayName", null));
	profile.setValidatedId(pObj.optString("id", null));
	if (pObj.has("name")) {
		JSONObject nobj = pObj.getJSONObject("name");
		profile.setLastName(nobj.optString("familyName", null));
		profile.setFirstName(nobj.optString("givenName", null));
	}
	profile.setLocation(pObj.optString("location", null));
	profile.setDisplayName(pObj.optString("nickname", null));
	profile.setLanguage(pObj.optString("lang", null));
	profile.setProfileImageURL(pObj.optString("thumbnailUrl", null));
	profile.setProviderId(getProviderId());
	if (config.isSaveRawResponse()) {
		profile.setRawResponse(result);
	}
	userProfile = profile;
	return profile;
}
 
Example 7
Source File: OpenIdImpl.java    From socialauth with MIT License 4 votes vote down vote up
/**
 * Verifies the user when the external provider redirects back to our
 * application.
 * 
 * 
 * @param requestParams
 *            request parameters, received from the provider
 * @return Profile object containing the profile information
 * @throws Exception
 */

@Override
public Profile verifyResponse(final Map<String, String> requestParams)
		throws Exception {
	if (!providerState) {
		throw new ProviderStateException();
	}
	try {
		// extract the parameters from the authentication response
		// (which comes in as a HTTP request from the OpenID provider)
		ParameterList response = new ParameterList(requestParams);

		// extract the receiving URL from the HTTP request
		StringBuffer receivingURL = new StringBuffer();
		receivingURL.append(successUrl);
		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, String> entry : requestParams.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			if (sb.length() > 0) {
				sb.append("&");
			}
			sb.append(key).append("=").append(value);
		}
		receivingURL.append("?").append(sb.toString());

		// verify the response; ConsumerManager needs to be the same
		// (static) instance used to place the authentication request
		VerificationResult verification = manager.verify(
				receivingURL.toString(), response, discovered);

		// examine the verification result and extract the verified
		// identifier
		Identifier verified = verification.getVerifiedId();
		if (verified != null) {
			LOG.debug("Verified Id : " + verified.getIdentifier());
			Profile p = new Profile();
			p.setValidatedId(verified.getIdentifier());
			AuthSuccess authSuccess = (AuthSuccess) verification
					.getAuthResponse();

			if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
				FetchResponse fetchResp = (FetchResponse) authSuccess
						.getExtension(AxMessage.OPENID_NS_AX);

				p.setEmail(fetchResp.getAttributeValue("email"));
				p.setFirstName(fetchResp.getAttributeValue("firstname"));
				p.setLastName(fetchResp.getAttributeValue("lastname"));
				p.setFullName(fetchResp.getAttributeValue("fullname"));

				// also use the ax namespace for compatibility
				if (p.getEmail() == null) {
					p.setEmail(fetchResp.getAttributeValue("emailax"));
				}
				if (p.getFirstName() == null) {
					p.setFirstName(fetchResp
							.getAttributeValue("firstnameax"));
				}
				if (p.getLastName() == null) {
					p.setLastName(fetchResp.getAttributeValue("lastnameax"));
				}
				if (p.getFullName() == null) {
					p.setFullName(fetchResp.getAttributeValue("fullnameax"));
				}

			}
			userProfile = p;
			return p;
		}
	} catch (OpenIDException e) {
		throw e;
	}

	return null;
}
 
Example 8
Source File: FacebookImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile authFacebookLogin() throws Exception {
	String presp;

	try {
		Response response = authenticationStrategy.executeFeed(PROFILE_URL);
		presp = response.getResponseBodyAsString(Constants.ENCODING);
	} catch (Exception e) {
		throw new SocialAuthException("Error while getting profile from "
				+ PROFILE_URL, e);
	}
	try {
		LOG.debug("User Profile : " + presp);
		JSONObject resp = new JSONObject(presp);
		Profile p = new Profile();
		p.setValidatedId(resp.optString("id", null));
		p.setFullName(resp.optString("name", null));
		p.setFirstName(resp.optString("first_name", null));
		p.setLastName(resp.optString("last_name", null));
		p.setEmail(resp.optString("email", null));
		if (resp.has("location")) {
			p.setLocation(resp.getJSONObject("location").optString("name",
					null));
		}
		if (resp.has("birthday")) {
			String bstr = resp.optString("birthday");
			String[] arr = bstr.split("/");
			BirthDate bd = new BirthDate();
			if (arr.length > 0) {
				bd.setMonth(Integer.parseInt(arr[0]));
			}
			if (arr.length > 1) {
				bd.setDay(Integer.parseInt(arr[1]));
			}
			if (arr.length > 2) {
				bd.setYear(Integer.parseInt(arr[2]));
			}
			p.setDob(bd);
		}
		p.setGender(resp.optString("gender", null));
		p.setProfileImageURL(String.format(PROFILE_IMAGE_URL,
				resp.getString("id")));
		String locale = resp.optString("locale", null);
		if (locale != null) {
			String a[] = locale.split("_");
			p.setLanguage(a[0]);
			p.setCountry(a[1]);
		}
		p.setProviderId(getProviderId());
		if (config.isSaveRawResponse()) {
			p.setRawResponse(presp);
		}
		userProfile = p;
		return p;

	} catch (Exception ex) {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + presp, ex);
	}
}