@Test public void returnsFailureWhenUsernameIsNull() throws Exception { String header = "Basic OmFzZGFkc2FzZA=="; Try<Credentials> credentialsTry = SecurityUtils.credentialsFromAuthHeader(header); assertThat(credentialsTry.isSuccess(), is(false)); }
public static Try<Credentials> credentialsFromAuthHeader(String authHeader) { if (!authHeader.contains("Basic ")) { return loginFailure(); } else { String credsPart = authHeader.replaceFirst("Basic ", ""); String decoded = new String(Base64.decodeBase64(credsPart)); if (!decoded.contains(":")) { return loginFailure(); } else { String[] usernamePassword = decoded.split(":"); if (usernamePassword.length == 2 && !isNullOrEmpty(usernamePassword[0]) && !isNullOrEmpty(usernamePassword[1])) { Credentials credentials = ImmutableCredentials.builder() .withUsername(usernamePassword[0]) .withPassword(usernamePassword[1]) .build(); return Try.success(credentials); } else { return loginFailure(); } } } }
SecurityUtils { public static Try<Credentials> credentialsFromAuthHeader(String authHeader) { if (!authHeader.contains("Basic ")) { return loginFailure(); } else { String credsPart = authHeader.replaceFirst("Basic ", ""); String decoded = new String(Base64.decodeBase64(credsPart)); if (!decoded.contains(":")) { return loginFailure(); } else { String[] usernamePassword = decoded.split(":"); if (usernamePassword.length == 2 && !isNullOrEmpty(usernamePassword[0]) && !isNullOrEmpty(usernamePassword[1])) { Credentials credentials = ImmutableCredentials.builder() .withUsername(usernamePassword[0]) .withPassword(usernamePassword[1]) .build(); return Try.success(credentials); } else { return loginFailure(); } } } } }
SecurityUtils { public static Try<Credentials> credentialsFromAuthHeader(String authHeader) { if (!authHeader.contains("Basic ")) { return loginFailure(); } else { String credsPart = authHeader.replaceFirst("Basic ", ""); String decoded = new String(Base64.decodeBase64(credsPart)); if (!decoded.contains(":")) { return loginFailure(); } else { String[] usernamePassword = decoded.split(":"); if (usernamePassword.length == 2 && !isNullOrEmpty(usernamePassword[0]) && !isNullOrEmpty(usernamePassword[1])) { Credentials credentials = ImmutableCredentials.builder() .withUsername(usernamePassword[0]) .withPassword(usernamePassword[1]) .build(); return Try.success(credentials); } else { return loginFailure(); } } } }  }
SecurityUtils { public static Try<Credentials> credentialsFromAuthHeader(String authHeader) { if (!authHeader.contains("Basic ")) { return loginFailure(); } else { String credsPart = authHeader.replaceFirst("Basic ", ""); String decoded = new String(Base64.decodeBase64(credsPart)); if (!decoded.contains(":")) { return loginFailure(); } else { String[] usernamePassword = decoded.split(":"); if (usernamePassword.length == 2 && !isNullOrEmpty(usernamePassword[0]) && !isNullOrEmpty(usernamePassword[1])) { Credentials credentials = ImmutableCredentials.builder() .withUsername(usernamePassword[0]) .withPassword(usernamePassword[1]) .build(); return Try.success(credentials); } else { return loginFailure(); } } } }  static Try<Credentials> credentialsFromAuthHeader(String authHeader); }
SecurityUtils { public static Try<Credentials> credentialsFromAuthHeader(String authHeader) { if (!authHeader.contains("Basic ")) { return loginFailure(); } else { String credsPart = authHeader.replaceFirst("Basic ", ""); String decoded = new String(Base64.decodeBase64(credsPart)); if (!decoded.contains(":")) { return loginFailure(); } else { String[] usernamePassword = decoded.split(":"); if (usernamePassword.length == 2 && !isNullOrEmpty(usernamePassword[0]) && !isNullOrEmpty(usernamePassword[1])) { Credentials credentials = ImmutableCredentials.builder() .withUsername(usernamePassword[0]) .withPassword(usernamePassword[1]) .build(); return Try.success(credentials); } else { return loginFailure(); } } } }  static Try<Credentials> credentialsFromAuthHeader(String authHeader);  }