Search in sources :

Example 11 with User

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")));
}
Also used : User(com.faforever.api.data.domain.User) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 12 with User

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);
}
Also used : User(com.faforever.api.data.domain.User) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException)

Example 13 with User

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);
}
Also used : User(com.faforever.api.data.domain.User) ArrayList(java.util.ArrayList) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 14 with User

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);
}
Also used : User(com.faforever.api.data.domain.User) GlobalRating(com.faforever.api.data.domain.GlobalRating) Ladder1v1Rating(com.faforever.api.data.domain.Ladder1v1Rating) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with User

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);
}
Also used : User(com.faforever.api.data.domain.User) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Aggregations

User (com.faforever.api.data.domain.User)28 Test (org.junit.Test)22 OffsetDateTime (java.time.OffsetDateTime)4 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)3 ApiException (com.faforever.api.error.ApiException)3 Error (com.faforever.api.error.Error)3 SteamLinkResult (com.faforever.api.user.UserService.SteamLinkResult)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 SneakyThrows (lombok.SneakyThrows)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 WithAnonymousUser (org.springframework.security.test.context.support.WithAnonymousUser)3 GlobalRating (com.faforever.api.data.domain.GlobalRating)2 Ladder1v1Rating (com.faforever.api.data.domain.Ladder1v1Rating)2 ArrayList (java.util.ArrayList)2 HttpHeaders (org.springframework.http.HttpHeaders)2 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)2 FafApiProperties (com.faforever.api.config.FafApiProperties)1 NameRecord (com.faforever.api.data.domain.NameRecord)1 Before (org.junit.Before)1