Search in sources :

Example 1 with MutableHttpResponse

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();
}
Also used : HttpRequest(io.micronaut.http.HttpRequest) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials) AccessRefreshToken(io.micronaut.security.token.jwt.render.AccessRefreshToken) Cookie(io.micronaut.http.cookie.Cookie) User(example.domain.User) AccessTokenConfiguration(io.micronaut.security.token.jwt.generator.AccessTokenConfiguration) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) Authentication(io.micronaut.security.authentication.Authentication) Singleton(jakarta.inject.Singleton) Random(java.util.Random) CookieConfiguration(io.micronaut.http.cookie.CookieConfiguration) AccessRefreshTokenGenerator(io.micronaut.security.token.jwt.generator.AccessRefreshTokenGenerator) UserRepository(example.repository.UserRepository) Authenticator(io.micronaut.security.authentication.Authenticator) Flux(reactor.core.publisher.Flux) LoginSuccessfulEvent(io.micronaut.security.event.LoginSuccessfulEvent) GraphQLContext(graphql.GraphQLContext) DataFetcher(graphql.schema.DataFetcher) TemporalAmount(java.time.temporal.TemporalAmount) Optional(java.util.Optional) HttpRequest(io.micronaut.http.HttpRequest) LoginFailedEvent(io.micronaut.security.event.LoginFailedEvent) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) User(example.domain.User) Optional(java.util.Optional) GraphQLContext(graphql.GraphQLContext) LoginSuccessfulEvent(io.micronaut.security.event.LoginSuccessfulEvent) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) LoginFailedEvent(io.micronaut.security.event.LoginFailedEvent) UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials)

Aggregations

User (example.domain.User)1 UserRepository (example.repository.UserRepository)1 GraphQLContext (graphql.GraphQLContext)1 DataFetcher (graphql.schema.DataFetcher)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 ApplicationEventPublisher (io.micronaut.context.event.ApplicationEventPublisher)1 HttpRequest (io.micronaut.http.HttpRequest)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Cookie (io.micronaut.http.cookie.Cookie)1 CookieConfiguration (io.micronaut.http.cookie.CookieConfiguration)1 Authentication (io.micronaut.security.authentication.Authentication)1 AuthenticationResponse (io.micronaut.security.authentication.AuthenticationResponse)1 Authenticator (io.micronaut.security.authentication.Authenticator)1 UsernamePasswordCredentials (io.micronaut.security.authentication.UsernamePasswordCredentials)1 LoginFailedEvent (io.micronaut.security.event.LoginFailedEvent)1 LoginSuccessfulEvent (io.micronaut.security.event.LoginSuccessfulEvent)1 AccessRefreshTokenGenerator (io.micronaut.security.token.jwt.generator.AccessRefreshTokenGenerator)1 AccessTokenConfiguration (io.micronaut.security.token.jwt.generator.AccessTokenConfiguration)1 AccessRefreshToken (io.micronaut.security.token.jwt.render.AccessRefreshToken)1 Singleton (jakarta.inject.Singleton)1