@Test public void getUserExists() throws Exception { UserInfo userInfo = UserInfo.newInstance(USER).build(); UserRole userRole = UserRole.newInstance(TESTAPP, Role.ADMIN).withUserID(USER).build(); UserRole userRole1 = UserRole.newInstance(TESTAPP2, Role.READWRITE).withUserID(USER).build(); UserRoleList userRoleList = new UserRoleList(); userRoleList.addRole(userRole); userRoleList.addRole(userRole1); when(authentication.getUserExists("username@a.b")).thenReturn(userInfo); when(authorization.getUser(AUTHHEADER)).thenReturn(USER); when(authorization.getUserRoleList(USER)).thenReturn(userRoleList); Response response = authenticationResource.getUserExists("username@a.b", AUTHHEADER); assert (userInfo.equals(response.getEntity())); Mockito.verify(authorization).getUser(AUTHHEADER); }
@GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed public Response getUserExists( @PathParam("userEmail") @ApiParam(value = "Email of the user") final String userEmail, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { UserInfo.Username username=authorization.getUser(authorizationHeader); if (this.rateLimitEnabled) { SimpleRateLimiter rateLimiter = getRateLimiter(username.toString()); boolean allowRequest = rateLimiter.tryAcquire(); if (!allowRequest) { return httpHeader.headers(429).build(); } } UserRoleList userRoleList = authorization.getUserRoleList(username); boolean isAdmin = userRoleList.getRoleList().stream().anyMatch((UserRole ur) -> (ur.getRole().equals(Role.SUPERADMIN) || ur.getRole().equals(Role.ADMIN))); if (!isAdmin) { throw new AuthenticationException("Error, user " + username + " is not authorized"); } return httpHeader.headers().entity(authentication.getUserExists(userEmail)).build(); } catch (Exception exception) { LOGGER.error("getUserExists failed for userEmail={} with error:", userEmail, exception); throw exception; } }
AuthenticationResource { @GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed public Response getUserExists( @PathParam("userEmail") @ApiParam(value = "Email of the user") final String userEmail, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { UserInfo.Username username=authorization.getUser(authorizationHeader); if (this.rateLimitEnabled) { SimpleRateLimiter rateLimiter = getRateLimiter(username.toString()); boolean allowRequest = rateLimiter.tryAcquire(); if (!allowRequest) { return httpHeader.headers(429).build(); } } UserRoleList userRoleList = authorization.getUserRoleList(username); boolean isAdmin = userRoleList.getRoleList().stream().anyMatch((UserRole ur) -> (ur.getRole().equals(Role.SUPERADMIN) || ur.getRole().equals(Role.ADMIN))); if (!isAdmin) { throw new AuthenticationException("Error, user " + username + " is not authorized"); } return httpHeader.headers().entity(authentication.getUserExists(userEmail)).build(); } catch (Exception exception) { LOGGER.error("getUserExists failed for userEmail={} with error:", userEmail, exception); throw exception; } } }
AuthenticationResource { @GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed public Response getUserExists( @PathParam("userEmail") @ApiParam(value = "Email of the user") final String userEmail, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { UserInfo.Username username=authorization.getUser(authorizationHeader); if (this.rateLimitEnabled) { SimpleRateLimiter rateLimiter = getRateLimiter(username.toString()); boolean allowRequest = rateLimiter.tryAcquire(); if (!allowRequest) { return httpHeader.headers(429).build(); } } UserRoleList userRoleList = authorization.getUserRoleList(username); boolean isAdmin = userRoleList.getRoleList().stream().anyMatch((UserRole ur) -> (ur.getRole().equals(Role.SUPERADMIN) || ur.getRole().equals(Role.ADMIN))); if (!isAdmin) { throw new AuthenticationException("Error, user " + username + " is not authorized"); } return httpHeader.headers().entity(authentication.getUserExists(userEmail)).build(); } catch (Exception exception) { LOGGER.error("getUserExists failed for userEmail={} with error:", userEmail, exception); throw exception; } } @Inject  AuthenticationResource(final Authentication authentication, final HttpHeader httpHeader,
                           final Authorization authorization,
                           final @Named(RATE_LIMIT_ENABLED) Boolean rateLimitEnabled,
                           final @Named(RATE_HOURLY_LIMIT) Integer rateHourlyLimit); }
AuthenticationResource { @GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed public Response getUserExists( @PathParam("userEmail") @ApiParam(value = "Email of the user") final String userEmail, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { UserInfo.Username username=authorization.getUser(authorizationHeader); if (this.rateLimitEnabled) { SimpleRateLimiter rateLimiter = getRateLimiter(username.toString()); boolean allowRequest = rateLimiter.tryAcquire(); if (!allowRequest) { return httpHeader.headers(429).build(); } } UserRoleList userRoleList = authorization.getUserRoleList(username); boolean isAdmin = userRoleList.getRoleList().stream().anyMatch((UserRole ur) -> (ur.getRole().equals(Role.SUPERADMIN) || ur.getRole().equals(Role.ADMIN))); if (!isAdmin) { throw new AuthenticationException("Error, user " + username + " is not authorized"); } return httpHeader.headers().entity(authentication.getUserExists(userEmail)).build(); } catch (Exception exception) { LOGGER.error("getUserExists failed for userEmail={} with error:", userEmail, exception); throw exception; } } @Inject  AuthenticationResource(final Authentication authentication, final HttpHeader httpHeader,
                           final Authorization authorization,
                           final @Named(RATE_LIMIT_ENABLED) Boolean rateLimitEnabled,
                           final @Named(RATE_HOURLY_LIMIT) Integer rateHourlyLimit); @POST @Path("/login") @Consumes(APPLICATION_FORM_URLENCODED) @Produces(APPLICATION_JSON) @ApiOperation(value = "Log a user in") @Timed Response logUserIn(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader,

            @FormParam("grant_type")
            @DefaultValue("client_credentials")
            @ApiParam(value = "please enter client_credentials in this field")
            final String grantType); @GET @Path("/verifyToken") @Produces(APPLICATION_JSON) @ApiOperation(value = "Verify user's authorization") @Timed Response verifyToken(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String tokenHeader); @GET @Path("/logout") @Produces(APPLICATION_JSON) @ApiOperation(value = "Log a user out") @Timed Response logUserOut(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String tokenHeader); @GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed Response getUserExists(
            @PathParam("userEmail")
            @ApiParam(value = "Email of the user")
            final String userEmail,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); }
AuthenticationResource { @GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed public Response getUserExists( @PathParam("userEmail") @ApiParam(value = "Email of the user") final String userEmail, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { UserInfo.Username username=authorization.getUser(authorizationHeader); if (this.rateLimitEnabled) { SimpleRateLimiter rateLimiter = getRateLimiter(username.toString()); boolean allowRequest = rateLimiter.tryAcquire(); if (!allowRequest) { return httpHeader.headers(429).build(); } } UserRoleList userRoleList = authorization.getUserRoleList(username); boolean isAdmin = userRoleList.getRoleList().stream().anyMatch((UserRole ur) -> (ur.getRole().equals(Role.SUPERADMIN) || ur.getRole().equals(Role.ADMIN))); if (!isAdmin) { throw new AuthenticationException("Error, user " + username + " is not authorized"); } return httpHeader.headers().entity(authentication.getUserExists(userEmail)).build(); } catch (Exception exception) { LOGGER.error("getUserExists failed for userEmail={} with error:", userEmail, exception); throw exception; } } @Inject  AuthenticationResource(final Authentication authentication, final HttpHeader httpHeader,
                           final Authorization authorization,
                           final @Named(RATE_LIMIT_ENABLED) Boolean rateLimitEnabled,
                           final @Named(RATE_HOURLY_LIMIT) Integer rateHourlyLimit); @POST @Path("/login") @Consumes(APPLICATION_FORM_URLENCODED) @Produces(APPLICATION_JSON) @ApiOperation(value = "Log a user in") @Timed Response logUserIn(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader,

            @FormParam("grant_type")
            @DefaultValue("client_credentials")
            @ApiParam(value = "please enter client_credentials in this field")
            final String grantType); @GET @Path("/verifyToken") @Produces(APPLICATION_JSON) @ApiOperation(value = "Verify user's authorization") @Timed Response verifyToken(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String tokenHeader); @GET @Path("/logout") @Produces(APPLICATION_JSON) @ApiOperation(value = "Log a user out") @Timed Response logUserOut(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String tokenHeader); @GET @Path("/users/{userEmail}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Check if user exists using user's email") @Timed Response getUserExists(
            @PathParam("userEmail")
            @ApiParam(value = "Email of the user")
            final String userEmail,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader);  }