use of io.micronaut.http.MutableHttpResponse in project micronaut-graphql by micronaut-projects.
the class LoginDataFetcher method get.
@Override
public LoginPayload get(DataFetchingEnvironment environment) throws Exception {
GraphQLContext graphQLContext = environment.getContext();
if (LOGIN_RATE_LIMIT_REMAINING <= 0) {
addRateLimitHeaders(graphQLContext);
resetRateLimit();
return LoginPayload.ofError("Rate Limit Exceeded");
}
HttpRequest httpRequest = graphQLContext.get("httpRequest");
MutableHttpResponse<String> httpResponse = graphQLContext.get("httpResponse");
String username = environment.getArgument("username");
String password = environment.getArgument("password");
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
LOGIN_RATE_LIMIT_REMAINING--;
Flux<AuthenticationResponse> authenticationResponseFlowable = Flux.from(authenticator.authenticate(httpRequest, usernamePasswordCredentials));
return authenticationResponseFlowable.map(authenticationResponse -> {
addRateLimitHeaders(graphQLContext);
if (authenticationResponse.isAuthenticated()) {
eventPublisher.publishEvent(new LoginSuccessfulEvent(authenticationResponse));
Optional<Cookie> jwtCookie = accessTokenCookie(Authentication.build(username), httpRequest);
jwtCookie.ifPresent(httpResponse::cookie);
User user = userRepository.findByUsername(username).orElse(null);
return LoginPayload.ofUser(user);
} else {
eventPublisher.publishEvent(new LoginFailedEvent(authenticationResponse));
return LoginPayload.ofError(authenticationResponse.getMessage().orElse(null));
}
}).blockFirst();
}
Aggregations