Skip to content

Instantly share code, notes, and snippets.

@pbenoit-palo

pbenoit-palo/findUser.java

Last active Mar 13, 2020
Embed
What would you like to do?
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