Htpasswd { @LogExceptions public boolean authorized(@NotNull final String user, @NotNull final String password) throws IOException { final ConcurrentMap<String, String> users = this.fetch(); return users.containsKey(user) && Htpasswd.matches(users.get(user), password); } Htpasswd(@NotNull final Host hst); @Override String toString(); @LogExceptions boolean authorized(@NotNull final String user,
        @NotNull final String password);  }
@Test public void understandsApacheNativeHashValues() throws Exception { MatcherAssert.assertThat( new Htpasswd( this.host("foo:$apr1$1/yqU0TM$fx36ZuZIapXW39ivIA5AR.") ).authorized("foo", "test"), Matchers.is(true) ); }
@Test public void understandsShaHashValues() throws Exception { MatcherAssert.assertThat( new Htpasswd( this.host("john:{SHA}6qagQQ8seo0bw69C/mNKhYbSf34=") ).authorized("john", "victory"), Matchers.is(true) ); MatcherAssert.assertThat( new Htpasswd( this.host("susi:{SHA}05jkyU4N/+ADjjOghbccdO5zKHE=") ).authorized("susi", "a7a6s-"), Matchers.is(true) ); MatcherAssert.assertThat( new Htpasswd( this.host("william:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=") ).authorized("william", "invalid-pwd"), Matchers.is(false) ); }
@Test public void understandsPlainTextHashValues() throws Exception { MatcherAssert.assertThat( new Htpasswd( this.host("erik:super-secret-password-\u0433") ).authorized("erik", "super-secret-password-\u0433"), Matchers.is(true) ); MatcherAssert.assertThat( new Htpasswd( this.host("nick:secret-password-\u0433") ).authorized("nick", "incorrect-password"), Matchers.is(false) ); }
@Test public void understandsCryptHashValues() throws Exception { MatcherAssert.assertThat( new Htpasswd( this.host("alex:QS3Wb6MddltY2") ).authorized("alex", "fire"), Matchers.is(true) ); }
@Test public void ignoresBrokenLines() throws Exception { MatcherAssert.assertThat( new Htpasswd( this.host("bobby:") ).authorized("bobby", "some-pwd-9"), Matchers.is(false) ); }
@Test public void worksWithDefaultHost() throws Exception { final Htpasswd htpasswd = new Htpasswd( new DefaultHost( new BucketMocker().mock(), new Host.CloudWatch() { @Override public AmazonCloudWatchClient get() { return Mockito.mock(AmazonCloudWatchClient.class); } } ) ); MatcherAssert.assertThat( htpasswd.authorized("jacky", "pwd"), Matchers.is(false) ); }