@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws IOException {
final String requestURI = request.getRequestURI();
Authentication userAuth = null;
User user = new User();
if (requestURI.endsWith(GOOGLE_ACTION)) {
String authCode = request.getParameter("code");
if (!StringUtils.isBlank(authCode)) {
String entity = Utils.formatMessage(PAYLOAD,
URLEncoder.encode(authCode, "UTF-8"),
URLEncoder.encode(request.getRequestURL().toString(), "UTF-8"),
Config.GPLUS_APP_ID, Config.GPLUS_SECRET);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost tokenPost = new HttpPost(TOKEN_URL);
tokenPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
tokenPost.setEntity(new StringEntity(entity, "UTF-8"));
CloseableHttpResponse resp1 = httpclient.execute(tokenPost);
ObjectReader jreader = Utils.getJsonReader(Map.class);
if (resp1 != null && resp1.getEntity() != null) {
Map<String, Object> token = jreader.readValue(resp1.getEntity().getContent());
if (token != null && token.containsKey("access_token")) {
// got valid token
HttpGet profileGet = new HttpGet(PROFILE_URL);
profileGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.get("access_token"));
CloseableHttpResponse resp2 = httpclient.execute(profileGet);
HttpEntity respEntity = resp2.getEntity();
String ctype = resp2.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
if (respEntity != null && Utils.isJsonType(ctype)) {
Map<String, Object> profile = jreader.readValue(resp2.getEntity().getContent());
if (profile != null && profile.containsKey("sub")) {
String googleSubId = (String) profile.get("sub");
String pic = (String) profile.get("picture");
String email = (String) profile.get("email");
String name = (String) profile.get("name");
user.setIdentifier(Config.GPLUS_PREFIX.concat(googleSubId));
user = User.readUserForIdentifier(user);
if (user == null) {
//user is new
user = new User();
user.setEmail(StringUtils.isBlank(email) ? "email@domain.com" : email);
user.setName(StringUtils.isBlank(name) ? "No Name" : name);
user.setPassword(new UUID().toString());
user.setIdentifier(Config.GPLUS_PREFIX.concat(googleSubId));
if (user.getPicture() == null) {
if (pic != null) {
if (pic.indexOf("?") > 0) {
// user picture migth contain size parameters - remove them
user.setPicture(pic.substring(0, pic.indexOf("?")));
} else {
user.setPicture(pic);
}
} else {
user.setPicture("http://www.gravatar.com/avatar?d=mm&size=200");
}
}
String id = user.create();
if (id == null) {
throw new AuthenticationServiceException("Authentication failed: cannot create new user.");
}
}
userAuth = new UserAuthentication(user);
}
EntityUtils.consumeQuietly(resp2.getEntity());
}
EntityUtils.consumeQuietly(resp1.getEntity());
}
}
}
}
if (userAuth == null || user == null || user.getIdentifier() == null) {
throw new BadCredentialsException("Bad credentials.");
} else if (!user.isEnabled()) {
throw new LockedException("Account is locked.");
}
return userAuth;
}