Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't manually read JSON values, deserialize into a Java object #66

Closed
IDragonfire opened this issue May 2, 2017 · 0 comments
Closed
Assignees

Comments

@IDragonfire
Copy link
Contributor

IDragonfire commented May 2, 2017

https://github.com/FAForever/faf-java-api/blob/v0.5.0/src/main/java/com/faforever/api/clan/ClanService.java#L117

@SneakyThrows
  // TODO @dragonfire don't manually read JSON values, deserialize into a Java object?
  public void acceptPlayerInvitationToken(String stringToken, Authentication authentication) {
    Jwt token = jwtService.decodeAndVerify(stringToken);
    JsonNode data = objectMapper.readTree(token.getClaims());

    if (data.get(JwtKeys.EXPIRE_IN).asLong() < System.currentTimeMillis()) {
      throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
    }

    Player player = playerService.getPlayer(authentication);
    Clan clan = clanRepository.findOne(data.get(JwtKeys.CLAN).get(JwtKeys.CLAN_ID).asInt());

    if (clan == null) {
      throw new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS));
    }

    Player newMember = playerRepository.findOne(data.get(JwtKeys.NEW_MEMBER_ID).asInt());
    if (newMember == null) {
      throw new ProgrammingError("ClanMember does not exist: " + data.get(JwtKeys.NEW_MEMBER_ID).asInt());
    }

    if (player.getId() != newMember.getId()) {
      throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
    }
    if (newMember.getClan() != null) {
      throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
    }

    ClanMembership membership = new ClanMembership();
    membership.setClan(clan);
    membership.setPlayer(newMember);
    clanMembershipRepository.save(membership);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant