@Test void givenRequest_whenApplyToRequest_thenSetsAuthorizationBasic() throws IRRPassTicketGenerationException { PassTicketService passTicketService = mock(PassTicketService.class); httpBasicPassTicketScheme = new HttpBasicPassTicketScheme(passTicketService, authConfigurationProperties); Calendar calendar = Calendar.getInstance(); Authentication authentication = new Authentication(AuthenticationScheme.HTTP_BASIC_PASSTICKET, "APPLID"); QueryResponse queryResponse = new QueryResponse("domain", USERNAME, calendar.getTime(), calendar.getTime(), QueryResponse.Source.ZOWE); HttpRequest httpRequest = new HttpGet("/test/request"); RequestContext requestContext = new RequestContext(); RequestContext.testSetCurrentContext(requestContext); doReturn("HI").when(passTicketService).generate(ArgumentMatchers.any(), ArgumentMatchers.any()); AuthenticationCommand ac = httpBasicPassTicketScheme.createCommand(authentication, () -> queryResponse); ac.applyToRequest(httpRequest); assertThat(httpRequest.getHeaders(HttpHeaders.AUTHORIZATION).length, is(not(0))); assertThat(httpRequest.getHeaders(HttpHeaders.AUTHORIZATION), hasItemInArray(hasToString( "Authorization: Basic VVNFUk5BTUU6SEk=" ))); }
@Override public AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier) { final long before = System.currentTimeMillis(); final QueryResponse token = tokenSupplier.get(); if (token == null) { return AuthenticationCommand.EMPTY; } final String applId = authentication.getApplid(); final String userId = token.getUserId(); String passTicket; try { passTicket = passTicketService.generate(userId, applId); } catch (IRRPassTicketGenerationException e) { throw new PassTicketException( String.format("Could not generate PassTicket for user ID %s and APPLID %s", userId, applId), e ); } final String encoded = Base64.getEncoder() .encodeToString((userId + ":" + passTicket).getBytes(StandardCharsets.UTF_8)); final String value = "Basic " + encoded; final long expiredAt = Math.min(before + authConfigurationProperties.getPassTicket().getTimeout() * 1000, token.getExpiration().getTime()); return new PassTicketCommand(value, cookieName, expiredAt); }
HttpBasicPassTicketScheme implements AbstractAuthenticationScheme { @Override public AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier) { final long before = System.currentTimeMillis(); final QueryResponse token = tokenSupplier.get(); if (token == null) { return AuthenticationCommand.EMPTY; } final String applId = authentication.getApplid(); final String userId = token.getUserId(); String passTicket; try { passTicket = passTicketService.generate(userId, applId); } catch (IRRPassTicketGenerationException e) { throw new PassTicketException( String.format("Could not generate PassTicket for user ID %s and APPLID %s", userId, applId), e ); } final String encoded = Base64.getEncoder() .encodeToString((userId + ":" + passTicket).getBytes(StandardCharsets.UTF_8)); final String value = "Basic " + encoded; final long expiredAt = Math.min(before + authConfigurationProperties.getPassTicket().getTimeout() * 1000, token.getExpiration().getTime()); return new PassTicketCommand(value, cookieName, expiredAt); } }
HttpBasicPassTicketScheme implements AbstractAuthenticationScheme { @Override public AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier) { final long before = System.currentTimeMillis(); final QueryResponse token = tokenSupplier.get(); if (token == null) { return AuthenticationCommand.EMPTY; } final String applId = authentication.getApplid(); final String userId = token.getUserId(); String passTicket; try { passTicket = passTicketService.generate(userId, applId); } catch (IRRPassTicketGenerationException e) { throw new PassTicketException( String.format("Could not generate PassTicket for user ID %s and APPLID %s", userId, applId), e ); } final String encoded = Base64.getEncoder() .encodeToString((userId + ":" + passTicket).getBytes(StandardCharsets.UTF_8)); final String value = "Basic " + encoded; final long expiredAt = Math.min(before + authConfigurationProperties.getPassTicket().getTimeout() * 1000, token.getExpiration().getTime()); return new PassTicketCommand(value, cookieName, expiredAt); } HttpBasicPassTicketScheme(
        PassTicketService passTicketService,
        AuthConfigurationProperties authConfigurationProperties
    ); }
HttpBasicPassTicketScheme implements AbstractAuthenticationScheme { @Override public AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier) { final long before = System.currentTimeMillis(); final QueryResponse token = tokenSupplier.get(); if (token == null) { return AuthenticationCommand.EMPTY; } final String applId = authentication.getApplid(); final String userId = token.getUserId(); String passTicket; try { passTicket = passTicketService.generate(userId, applId); } catch (IRRPassTicketGenerationException e) { throw new PassTicketException( String.format("Could not generate PassTicket for user ID %s and APPLID %s", userId, applId), e ); } final String encoded = Base64.getEncoder() .encodeToString((userId + ":" + passTicket).getBytes(StandardCharsets.UTF_8)); final String value = "Basic " + encoded; final long expiredAt = Math.min(before + authConfigurationProperties.getPassTicket().getTimeout() * 1000, token.getExpiration().getTime()); return new PassTicketCommand(value, cookieName, expiredAt); } HttpBasicPassTicketScheme(
        PassTicketService passTicketService,
        AuthConfigurationProperties authConfigurationProperties
    ); @Override AuthenticationScheme getScheme(); @Override AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier); }
HttpBasicPassTicketScheme implements AbstractAuthenticationScheme { @Override public AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier) { final long before = System.currentTimeMillis(); final QueryResponse token = tokenSupplier.get(); if (token == null) { return AuthenticationCommand.EMPTY; } final String applId = authentication.getApplid(); final String userId = token.getUserId(); String passTicket; try { passTicket = passTicketService.generate(userId, applId); } catch (IRRPassTicketGenerationException e) { throw new PassTicketException( String.format("Could not generate PassTicket for user ID %s and APPLID %s", userId, applId), e ); } final String encoded = Base64.getEncoder() .encodeToString((userId + ":" + passTicket).getBytes(StandardCharsets.UTF_8)); final String value = "Basic " + encoded; final long expiredAt = Math.min(before + authConfigurationProperties.getPassTicket().getTimeout() * 1000, token.getExpiration().getTime()); return new PassTicketCommand(value, cookieName, expiredAt); } HttpBasicPassTicketScheme(
        PassTicketService passTicketService,
        AuthConfigurationProperties authConfigurationProperties
    ); @Override AuthenticationScheme getScheme(); @Override AuthenticationCommand createCommand(Authentication authentication, Supplier<QueryResponse> tokenSupplier);  }