@Test public void parseSupportsBasicIsSpelledWithLowercaseB() throws InvalidAuthorizationHeaderException { String encodedAuthString = encodeBase64(VALID_AUTH_STRING); String header = String.format("basic %s", encodedAuthString); BasicAuthorizationHeaderParser.Credentials result = BasicAuthorizationHeaderParser.parse(header); assertThat(result, is(not(nullValue()))); }
public static Credentials parse(String authorizationHeader) throws InvalidAuthorizationHeaderException { if (!authorizationHeader.toLowerCase().startsWith("basic ")) { throw new InvalidAuthorizationHeaderException("Header must start with the word 'Basic' followed by 1 space."); } int indexOfWhitespace = authorizationHeader.indexOf(" "); String rawAuthString = authorizationHeader.substring(indexOfWhitespace + 1); String decodedAuth = null; try { decodedAuth = new String(Base64.getDecoder().decode(rawAuthString.getBytes())); } catch (IllegalArgumentException e) { throw new InvalidAuthorizationHeaderException(e.getMessage()); } if (!decodedAuth.contains(":")) { throw new InvalidAuthorizationHeaderException(String.format( "The username and password should be seperated by a ':' but that character was not found in '%s'.", decodedAuth)); } int indexOfFirstColon = decodedAuth.indexOf(":"); String username = decodedAuth.substring(0, indexOfFirstColon); String password = decodedAuth.substring(indexOfFirstColon + 1); return new Credentials(username, password); }
BasicAuthorizationHeaderParser { public static Credentials parse(String authorizationHeader) throws InvalidAuthorizationHeaderException { if (!authorizationHeader.toLowerCase().startsWith("basic ")) { throw new InvalidAuthorizationHeaderException("Header must start with the word 'Basic' followed by 1 space."); } int indexOfWhitespace = authorizationHeader.indexOf(" "); String rawAuthString = authorizationHeader.substring(indexOfWhitespace + 1); String decodedAuth = null; try { decodedAuth = new String(Base64.getDecoder().decode(rawAuthString.getBytes())); } catch (IllegalArgumentException e) { throw new InvalidAuthorizationHeaderException(e.getMessage()); } if (!decodedAuth.contains(":")) { throw new InvalidAuthorizationHeaderException(String.format( "The username and password should be seperated by a ':' but that character was not found in '%s'.", decodedAuth)); } int indexOfFirstColon = decodedAuth.indexOf(":"); String username = decodedAuth.substring(0, indexOfFirstColon); String password = decodedAuth.substring(indexOfFirstColon + 1); return new Credentials(username, password); } }
BasicAuthorizationHeaderParser { public static Credentials parse(String authorizationHeader) throws InvalidAuthorizationHeaderException { if (!authorizationHeader.toLowerCase().startsWith("basic ")) { throw new InvalidAuthorizationHeaderException("Header must start with the word 'Basic' followed by 1 space."); } int indexOfWhitespace = authorizationHeader.indexOf(" "); String rawAuthString = authorizationHeader.substring(indexOfWhitespace + 1); String decodedAuth = null; try { decodedAuth = new String(Base64.getDecoder().decode(rawAuthString.getBytes())); } catch (IllegalArgumentException e) { throw new InvalidAuthorizationHeaderException(e.getMessage()); } if (!decodedAuth.contains(":")) { throw new InvalidAuthorizationHeaderException(String.format( "The username and password should be seperated by a ':' but that character was not found in '%s'.", decodedAuth)); } int indexOfFirstColon = decodedAuth.indexOf(":"); String username = decodedAuth.substring(0, indexOfFirstColon); String password = decodedAuth.substring(indexOfFirstColon + 1); return new Credentials(username, password); }  }
BasicAuthorizationHeaderParser { public static Credentials parse(String authorizationHeader) throws InvalidAuthorizationHeaderException { if (!authorizationHeader.toLowerCase().startsWith("basic ")) { throw new InvalidAuthorizationHeaderException("Header must start with the word 'Basic' followed by 1 space."); } int indexOfWhitespace = authorizationHeader.indexOf(" "); String rawAuthString = authorizationHeader.substring(indexOfWhitespace + 1); String decodedAuth = null; try { decodedAuth = new String(Base64.getDecoder().decode(rawAuthString.getBytes())); } catch (IllegalArgumentException e) { throw new InvalidAuthorizationHeaderException(e.getMessage()); } if (!decodedAuth.contains(":")) { throw new InvalidAuthorizationHeaderException(String.format( "The username and password should be seperated by a ':' but that character was not found in '%s'.", decodedAuth)); } int indexOfFirstColon = decodedAuth.indexOf(":"); String username = decodedAuth.substring(0, indexOfFirstColon); String password = decodedAuth.substring(indexOfFirstColon + 1); return new Credentials(username, password); }  static Credentials parse(String authorizationHeader); }
BasicAuthorizationHeaderParser { public static Credentials parse(String authorizationHeader) throws InvalidAuthorizationHeaderException { if (!authorizationHeader.toLowerCase().startsWith("basic ")) { throw new InvalidAuthorizationHeaderException("Header must start with the word 'Basic' followed by 1 space."); } int indexOfWhitespace = authorizationHeader.indexOf(" "); String rawAuthString = authorizationHeader.substring(indexOfWhitespace + 1); String decodedAuth = null; try { decodedAuth = new String(Base64.getDecoder().decode(rawAuthString.getBytes())); } catch (IllegalArgumentException e) { throw new InvalidAuthorizationHeaderException(e.getMessage()); } if (!decodedAuth.contains(":")) { throw new InvalidAuthorizationHeaderException(String.format( "The username and password should be seperated by a ':' but that character was not found in '%s'.", decodedAuth)); } int indexOfFirstColon = decodedAuth.indexOf(":"); String username = decodedAuth.substring(0, indexOfFirstColon); String password = decodedAuth.substring(indexOfFirstColon + 1); return new Credentials(username, password); }  static Credentials parse(String authorizationHeader);  }