Last active
March 13, 2020 07:18
-
-
Save pbenoit-palo/d9aa7f20e0c20694312341273a98f97d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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