use of com.faforever.api.data.domain.User in project faf-java-api by FAForever.
the class UserServiceTest method buildSteamLinkUrl.
@Test
@SuppressWarnings("unchecked")
public void buildSteamLinkUrl() {
when(steamService.buildLoginUrl(any())).thenReturn("steamLoginUrl");
User user = createUser(TEST_USERID, TEST_USERNAME, TEST_CURRENT_PASSWORD, TEST_CURRENT_EMAIL);
String url = instance.buildSteamLinkUrl(user, "http://example.com/callback");
ArgumentCaptor<String> loginUrlCaptor = ArgumentCaptor.forClass(String.class);
verify(steamService).buildLoginUrl(loginUrlCaptor.capture());
ArgumentCaptor<Map<String, String>> attributesCaptor = ArgumentCaptor.forClass(Map.class);
verify(fafTokenService).createToken(eq(FafTokenType.LINK_TO_STEAM), eq(Duration.ofHours(1)), attributesCaptor.capture());
assertThat(url, is("steamLoginUrl"));
assertThat(loginUrlCaptor.getValue(), is(TOKEN_VALUE));
assertThat(attributesCaptor.getValue(), is(ImmutableMap.of(KEY_USER_ID, String.valueOf(user.getId()), "callbackUrl", "http://example.com/callback")));
}
use of com.faforever.api.data.domain.User in project faf-java-api by FAForever.
the class UserService method resetPassword.
void resetPassword(String identifier, String newPassword) {
log.debug("Password reset requested for user-identifier: {}", identifier);
User user = userRepository.findOneByLoginIgnoreCase(identifier).orElseGet(() -> userRepository.findOneByEmailIgnoreCase(identifier).orElseThrow(() -> new ApiException(new Error(ErrorCode.UNKNOWN_IDENTIFIER))));
String token = fafTokenService.createToken(FafTokenType.PASSWORD_RESET, Duration.ofSeconds(properties.getRegistration().getLinkExpirationSeconds()), ImmutableMap.of(KEY_USER_ID, String.valueOf(user.getId()), KEY_PASSWORD, newPassword));
String passwordResetUrl = String.format(properties.getPasswordReset().getPasswordResetUrlFormat(), token);
emailService.sendPasswordResetMail(user.getLogin(), user.getEmail(), passwordResetUrl);
}
use of com.faforever.api.data.domain.User in project faf-java-api by FAForever.
the class UserService method linkToSteam.
@SneakyThrows
public SteamLinkResult linkToSteam(String token, String steamId) {
log.debug("linkToSteam requested for steamId '{}' with token: {}", steamId, token);
List<Error> errors = new ArrayList<>();
Map<String, String> attributes = fafTokenService.resolveToken(FafTokenType.LINK_TO_STEAM, token);
User user = userRepository.findById(Integer.parseInt(attributes.get(KEY_USER_ID))).orElseThrow(() -> new ApiException(new Error(TOKEN_INVALID)));
if (!steamService.ownsForgedAlliance(steamId)) {
errors.add(new Error(ErrorCode.STEAM_LINK_NO_FA_GAME));
}
Optional<User> userWithSameSteamIdOptional = userRepository.findOneBySteamIdIgnoreCase(steamId);
userWithSameSteamIdOptional.ifPresent(userWithSameId -> errors.add(new Error(ErrorCode.STEAM_ID_ALREADY_LINKED, userWithSameId.getLogin())));
if (errors.isEmpty()) {
user.setSteamId(steamId);
userRepository.save(user);
}
String callbackUrl = attributes.get(KEY_STEAM_LINK_CALLBACK_URL);
return new SteamLinkResult(callbackUrl, errors);
}
use of com.faforever.api.data.domain.User in project faf-java-api by FAForever.
the class UserService method activate.
/**
* Creates a new user based on the information in the activation token.
*
* @param token the JWT in the format: <pre>
* {
* "action": "activate",
* "expiry": "2011-12-03T10:15:30Z",
* "
* }
* </pre>
*/
@SneakyThrows
@SuppressWarnings("unchecked")
@Transactional
void activate(String token, String ipAddress) {
Map<String, String> claims = fafTokenService.resolveToken(FafTokenType.REGISTRATION, token);
String username = claims.get(KEY_USERNAME);
String email = claims.get(KEY_EMAIL);
String password = claims.get(KEY_PASSWORD);
User user = new User();
user.setPassword(password);
user.setEmail(email);
user.setLogin(username);
user.setRecentIpAddress(ipAddress);
user = userRepository.save(user);
// @Deprecated
// TODO: Move this db activity to the server (upcert instead of update) */
// >>>
double mean = properties.getRating().getDefaultMean();
double deviation = properties.getRating().getDefaultDeviation();
globalRatingRepository.save((GlobalRating) new GlobalRating().setId(user.getId()).setMean(mean).setDeviation(deviation));
ladder1v1RatingRepository.save((Ladder1v1Rating) new Ladder1v1Rating().setId(user.getId()).setMean(mean).setDeviation(deviation));
// <<<
log.debug("User has been activated: {}", user);
createOrUpdateMauticContact(user, ipAddress);
}
use of com.faforever.api.data.domain.User in project faf-java-api by FAForever.
the class UserService method claimPasswordResetToken.
@SneakyThrows
void claimPasswordResetToken(String token) {
log.debug("Trying to reset password with token: {}", token);
Map<String, String> claims = fafTokenService.resolveToken(FafTokenType.PASSWORD_RESET, token);
int userId = Integer.parseInt(claims.get(KEY_USER_ID));
String newPassword = claims.get(KEY_PASSWORD);
User user = userRepository.findById(userId).orElseThrow(() -> new ApiException(new Error(TOKEN_INVALID)));
setPassword(user, newPassword);
}
Aggregations