Skip to content

Instantly share code, notes, and snippets.

@pbenoit-palo
Last active March 13, 2020 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbenoit-palo/d9aa7f20e0c20694312341273a98f97d to your computer and use it in GitHub Desktop.
Save pbenoit-palo/d9aa7f20e0c20694312341273a98f97d to your computer and use it in GitHub Desktop.
public class FindUserByPhoneHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
private static final DynamoDBUserDao userDao = DynamoDBUserDao.instance();
@Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
Tuple2<Jws<Claims>, ApiGatewayResponse> jwsParsed = JwtValidator.validateJwt(input, "admin|siteadmin|useradmin|agent|individual|global-admin");
if (jwsParsed._2 != null) {
return jwsParsed._2;
}
String login = getPathParameter("mobile", input);
Optional<User> user;
if (JwtValidator.containsRoles(jwsParsed._1, "individual|agent")) {
String loginFromJwt = jwsParsed._1.getBody().getIssuer();
if (loginFromJwt.compareTo(login) != 0) {
return error(ApiErrorMessage.ApiError.API_BAD_PARAMETER, "login", login,
"The user with phone [" + login + "] can only get info about self profile.");
}
user = userDao.findUserByPhone(loginFromJwt);
} else {
user = userDao.findUserByPhone(login);
}
if (!user.isPresent()) {
return error(ApiErrorMessage.ApiError.API_NOT_FOUND, "login", login,
"The user with phone [" + login + "] does not exist.");
}
user.get().setPassword(null);
return success(200, user.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment