}
return result;
}
private void loginViaOAuth2(Map<String, String> params, long serverId) throws IOException, NoSuchAlgorithmException {
AdminUserDao userDao = getBean(AdminUserDao.class);
IUserManager userManager = getBean(IUserManager.class);
ConfigurationDao configurationDao = getBean(ConfigurationDao.class);
String login = params.get("login");
String email = params.get("email");
String lastname = params.get("lastname");
String firstname = params.get("firstname");
if (firstname == null) firstname = "";
if (lastname == null) lastname = "";
User user = userDao.getUserByName(login);
// generate random password
byte[] rawPass = new byte[16];
Random rnd = new Random();
for (int i = 0; i < 16; i++) {
rawPass[i] = (byte) (97 + rnd.nextInt(25));
}
String pass = new String(rawPass);
// check if the user already exists and register new one if it's needed
if (user == null) {
Integer defaultlangId = Integer.valueOf(configurationDao.getConfValue("default_lang_id", String.class, "1"));
String defaultTimezone = configurationDao.getConfValue("default.timezone", String.class, "");
Long res = userManager.registerUserNoEmail(login, pass, lastname, firstname, email, null, null,
null, null, null, 0, null, defaultlangId, null, false, true, defaultTimezone);
if (res == null || res < 0) {
throw new RuntimeException("Couldn't register new oauth user");
}
user = userDao.get(res);
user.setExternalUserType("oauth2." + serverId);
userDao.update(user, null);
} else { // just change password
// check user type before changing password, it must be match oauthServerId
if (!("oauth2." + serverId).equals(user.getExternalUserType())) {
log.error("User already registered!");
return;
}
user = userDao.update(user, pass, -1);
}
if (WebSession.get().signIn(login, pass, null)) {
setResponsePage(Application.get().getHomePage());
} else {