@Test public void shouldThrowIllegalStateExceptionIfOrgTenantAndUsernameCanNotBeSeparatedFromPrinciple() { Authentication authenticationMock = mock(Authentication.class); when(authenticationMock.getPrincipal()).thenReturn("HelloUser"); thrown.expect(IllegalStateException.class); thrown.expectMessage("Authentication principle[HelloUser] should contain org, tenant and username."); authTokenBuilder.createAccessToken(authenticationMock); }
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);  }