@Test public void shouldReturnAccessTokenOnSuccessfulAuthentication() throws NoSuchAlgorithmException, JSONException { ArgumentCaptor<Claims> claimsCaptor = ArgumentCaptor.forClass(Claims.class); ArgumentCaptor<String> issuerCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Date> issueDateCaptor = ArgumentCaptor.forClass(Date.class); ArgumentCaptor<Date> expirationDateCaptor = ArgumentCaptor.forClass(Date.class); ArgumentCaptor<PrivateKey> privateKeyCaptor = ArgumentCaptor.forClass(PrivateKey.class); Authentication authenticationMock = mock(Authentication.class); when(authenticationMock.getPrincipal()).thenReturn("HelloOrg" + Character.LINE_SEPARATOR + "HelloTenant" + Character.LINE_SEPARATOR + "HelloUser"); Collection authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_HELLO")); when(authenticationMock.getAuthorities()).thenReturn(authorities); LocalDateTime localDateTime = LocalDateTime.now(); when(dateUtilMock.getCurrentLocalDateTime()).thenReturn(localDateTime); when(authJwtPropertiesMock.getTokenExpirationTimeInSecond()).thenReturn(5); when(authJwtPropertiesMock.getTokenIssuer()).thenReturn("ME!"); when(authJwtPropertiesMock.getTokenSigningPrivateKeyPath()).thenReturn("privateKeyPath"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(4096); PrivateKey privateKey = keyGen.genKeyPair().getPrivate(); when(encryptionKeyUtilMock.loadPrivateKey("privateKeyPath")).thenReturn(privateKey); when(jwtUtilMock.getJwts(any(), any(), any(), any(), any())).thenReturn("mockJwtToken"); String accessToken = authTokenBuilder.createAccessToken(authenticationMock); assertEquals("mockJwtToken", accessToken); verify(jwtUtilMock).getJwts(claimsCaptor.capture(), issuerCaptor.capture(), issueDateCaptor.capture(), expirationDateCaptor.capture(), privateKeyCaptor.capture()); assertEquals("HelloUser", claimsCaptor.getValue().getSubject()); assertEquals("HelloOrg", claimsCaptor.getValue().get("org")); assertEquals("HelloTenant", claimsCaptor.getValue().get("tenant")); assertEquals("[ROLE_HELLO]", claimsCaptor.getValue().get("scopes").toString()); assertEquals("ME!", issuerCaptor.getValue()); assertEquals(localDateTime.atZone(ZoneId.of("UTC")).toEpochSecond(), issueDateCaptor.getValue().getTime() / 1000); assertEquals(localDateTime.atZone(ZoneId.of("UTC")).toEpochSecond() + 5, expirationDateCaptor.getValue().getTime() / 1000); assertEquals(privateKey, privateKeyCaptor.getValue()); }
public String createAccessToken(Authentication authentication) { String principal = (String) authentication.getPrincipal(); if (StringUtils.isBlank(principal)) { throw new IllegalStateException("Authentication principle can not be null or empty."); } String[] orgTenantUsername = principal.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new IllegalStateException( String.format("Authentication principle[%s] should contain org, tenant and username.", principal)); } String org = orgTenantUsername[0]; String tenant = orgTenantUsername[1]; String username = orgTenantUsername[2]; List<GrantedAuthority> authorities = new ArrayList<>(authentication.getAuthorities()); if (StringUtils.isBlank(org)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain org.", principal)); } if (StringUtils.isBlank(tenant)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain tenant.", principal)); } if (StringUtils.isBlank(username)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain username.", principal)); } if (authorities == null || authorities.isEmpty()) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain authorities.", principal)); } Claims claims = Jwts.claims(); claims.setSubject(username); claims.put("org", org); claims.put("tenant", tenant); claims.put("scopes", authorities.stream().map(s -> s.toString()).collect(Collectors.toList())); LocalDateTime currentTime = dateUtil.getCurrentLocalDateTime(); Date issueDate = Date.from(currentTime.toInstant(ZoneOffset.UTC)); Date expiration = Date.from(currentTime.plusSeconds( authJwtProperties.getTokenExpirationTimeInSecond()).toInstant(ZoneOffset.UTC)); PrivateKey privateKey = encryptionKeyUtil.loadPrivateKey(authJwtProperties.getTokenSigningPrivateKeyPath()); return jwtUtil.getJwts(claims, authJwtProperties.getTokenIssuer(), issueDate, expiration, privateKey); }
AuthTokenBuilder { public String createAccessToken(Authentication authentication) { String principal = (String) authentication.getPrincipal(); if (StringUtils.isBlank(principal)) { throw new IllegalStateException("Authentication principle can not be null or empty."); } String[] orgTenantUsername = principal.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new IllegalStateException( String.format("Authentication principle[%s] should contain org, tenant and username.", principal)); } String org = orgTenantUsername[0]; String tenant = orgTenantUsername[1]; String username = orgTenantUsername[2]; List<GrantedAuthority> authorities = new ArrayList<>(authentication.getAuthorities()); if (StringUtils.isBlank(org)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain org.", principal)); } if (StringUtils.isBlank(tenant)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain tenant.", principal)); } if (StringUtils.isBlank(username)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain username.", principal)); } if (authorities == null || authorities.isEmpty()) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain authorities.", principal)); } Claims claims = Jwts.claims(); claims.setSubject(username); claims.put("org", org); claims.put("tenant", tenant); claims.put("scopes", authorities.stream().map(s -> s.toString()).collect(Collectors.toList())); LocalDateTime currentTime = dateUtil.getCurrentLocalDateTime(); Date issueDate = Date.from(currentTime.toInstant(ZoneOffset.UTC)); Date expiration = Date.from(currentTime.plusSeconds( authJwtProperties.getTokenExpirationTimeInSecond()).toInstant(ZoneOffset.UTC)); PrivateKey privateKey = encryptionKeyUtil.loadPrivateKey(authJwtProperties.getTokenSigningPrivateKeyPath()); return jwtUtil.getJwts(claims, authJwtProperties.getTokenIssuer(), issueDate, expiration, privateKey); } }
AuthTokenBuilder { public String createAccessToken(Authentication authentication) { String principal = (String) authentication.getPrincipal(); if (StringUtils.isBlank(principal)) { throw new IllegalStateException("Authentication principle can not be null or empty."); } String[] orgTenantUsername = principal.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new IllegalStateException( String.format("Authentication principle[%s] should contain org, tenant and username.", principal)); } String org = orgTenantUsername[0]; String tenant = orgTenantUsername[1]; String username = orgTenantUsername[2]; List<GrantedAuthority> authorities = new ArrayList<>(authentication.getAuthorities()); if (StringUtils.isBlank(org)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain org.", principal)); } if (StringUtils.isBlank(tenant)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain tenant.", principal)); } if (StringUtils.isBlank(username)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain username.", principal)); } if (authorities == null || authorities.isEmpty()) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain authorities.", principal)); } Claims claims = Jwts.claims(); claims.setSubject(username); claims.put("org", org); claims.put("tenant", tenant); claims.put("scopes", authorities.stream().map(s -> s.toString()).collect(Collectors.toList())); LocalDateTime currentTime = dateUtil.getCurrentLocalDateTime(); Date issueDate = Date.from(currentTime.toInstant(ZoneOffset.UTC)); Date expiration = Date.from(currentTime.plusSeconds( authJwtProperties.getTokenExpirationTimeInSecond()).toInstant(ZoneOffset.UTC)); PrivateKey privateKey = encryptionKeyUtil.loadPrivateKey(authJwtProperties.getTokenSigningPrivateKeyPath()); return jwtUtil.getJwts(claims, authJwtProperties.getTokenIssuer(), issueDate, expiration, privateKey); }  }
AuthTokenBuilder { public String createAccessToken(Authentication authentication) { String principal = (String) authentication.getPrincipal(); if (StringUtils.isBlank(principal)) { throw new IllegalStateException("Authentication principle can not be null or empty."); } String[] orgTenantUsername = principal.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new IllegalStateException( String.format("Authentication principle[%s] should contain org, tenant and username.", principal)); } String org = orgTenantUsername[0]; String tenant = orgTenantUsername[1]; String username = orgTenantUsername[2]; List<GrantedAuthority> authorities = new ArrayList<>(authentication.getAuthorities()); if (StringUtils.isBlank(org)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain org.", principal)); } if (StringUtils.isBlank(tenant)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain tenant.", principal)); } if (StringUtils.isBlank(username)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain username.", principal)); } if (authorities == null || authorities.isEmpty()) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain authorities.", principal)); } Claims claims = Jwts.claims(); claims.setSubject(username); claims.put("org", org); claims.put("tenant", tenant); claims.put("scopes", authorities.stream().map(s -> s.toString()).collect(Collectors.toList())); LocalDateTime currentTime = dateUtil.getCurrentLocalDateTime(); Date issueDate = Date.from(currentTime.toInstant(ZoneOffset.UTC)); Date expiration = Date.from(currentTime.plusSeconds( authJwtProperties.getTokenExpirationTimeInSecond()).toInstant(ZoneOffset.UTC)); PrivateKey privateKey = encryptionKeyUtil.loadPrivateKey(authJwtProperties.getTokenSigningPrivateKeyPath()); return jwtUtil.getJwts(claims, authJwtProperties.getTokenIssuer(), issueDate, expiration, privateKey); }  String createAccessToken(Authentication authentication); }
AuthTokenBuilder { public String createAccessToken(Authentication authentication) { String principal = (String) authentication.getPrincipal(); if (StringUtils.isBlank(principal)) { throw new IllegalStateException("Authentication principle can not be null or empty."); } String[] orgTenantUsername = principal.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new IllegalStateException( String.format("Authentication principle[%s] should contain org, tenant and username.", principal)); } String org = orgTenantUsername[0]; String tenant = orgTenantUsername[1]; String username = orgTenantUsername[2]; List<GrantedAuthority> authorities = new ArrayList<>(authentication.getAuthorities()); if (StringUtils.isBlank(org)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain org.", principal)); } if (StringUtils.isBlank(tenant)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain tenant.", principal)); } if (StringUtils.isBlank(username)) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain username.", principal)); } if (authorities == null || authorities.isEmpty()) { throw new IllegalArgumentException( String.format("Authentication principle[%s] does not contain authorities.", principal)); } Claims claims = Jwts.claims(); claims.setSubject(username); claims.put("org", org); claims.put("tenant", tenant); claims.put("scopes", authorities.stream().map(s -> s.toString()).collect(Collectors.toList())); LocalDateTime currentTime = dateUtil.getCurrentLocalDateTime(); Date issueDate = Date.from(currentTime.toInstant(ZoneOffset.UTC)); Date expiration = Date.from(currentTime.plusSeconds( authJwtProperties.getTokenExpirationTimeInSecond()).toInstant(ZoneOffset.UTC)); PrivateKey privateKey = encryptionKeyUtil.loadPrivateKey(authJwtProperties.getTokenSigningPrivateKeyPath()); return jwtUtil.getJwts(claims, authJwtProperties.getTokenIssuer(), issueDate, expiration, privateKey); }  String createAccessToken(Authentication authentication);  }