Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge dropwizard-java8 addon into mainline
dropwizard-java8 used to provide support for Java 8 classes like `Optional<T>` and the Java Date and Time API (JSR-310) for Dropwizard. Given that the baseline for Dropwizard is now Java 8, those additions can be maintained in the mainline repository. The only backward-incompatible change is in the `io.dropwizard.auth.Authenticator` interface which is now using `java.util.Optional` instead of the equivalent class from Google Guava. Closes #962
- Loading branch information
Showing
128 changed files
with
4,657 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 29 additions & 40 deletions
69
dropwizard-auth/src/test/java/io/dropwizard/auth/util/AuthUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,69 +1,58 @@ | |||
package io.dropwizard.auth.util; | package io.dropwizard.auth.util; | ||
|
|
||
import com.google.common.base.Optional; | import io.dropwizard.auth.AuthenticationException; | ||
import io.dropwizard.auth.*; | import io.dropwizard.auth.Authenticator; | ||
import io.dropwizard.auth.Authorizer; | |||
import io.dropwizard.auth.PrincipalImpl; | |||
import io.dropwizard.auth.basic.BasicCredentials; | import io.dropwizard.auth.basic.BasicCredentials; | ||
|
|
||
import java.security.Principal; | import java.security.Principal; | ||
import java.util.List; | import java.util.List; | ||
import java.util.Optional; | |||
|
|
||
public class AuthUtil { | public class AuthUtil { | ||
|
|
||
public static Authenticator<BasicCredentials, Principal> getBasicAuthenticator(final List<String> validUsers) { | public static Authenticator<BasicCredentials, Principal> getBasicAuthenticator(final List<String> validUsers) { | ||
return new Authenticator<BasicCredentials, Principal>() { | return credentials -> { | ||
@Override | if (validUsers.contains(credentials.getUsername()) && "secret".equals(credentials.getPassword())) { | ||
public Optional<Principal> authenticate(BasicCredentials credentials) throws AuthenticationException { | return Optional.<Principal>of(new PrincipalImpl(credentials.getUsername())); | ||
if (validUsers.contains(credentials.getUsername()) && "secret".equals(credentials.getPassword())) { | |||
return Optional.<Principal>of(new PrincipalImpl(credentials.getUsername())); | |||
} | |||
if ("bad-guy".equals(credentials.getUsername())) { | |||
throw new AuthenticationException("CRAP"); | |||
} | |||
return Optional.absent(); | |||
} | } | ||
if ("bad-guy".equals(credentials.getUsername())) { | |||
throw new AuthenticationException("CRAP"); | |||
} | |||
return Optional.empty(); | |||
}; | }; | ||
} | } | ||
|
|
||
public static Authenticator<String, Principal> getSingleUserOAuthAuthenticator(final String presented, | public static Authenticator<String, Principal> getSingleUserOAuthAuthenticator(final String presented, | ||
final String returned) { | final String returned) { | ||
return new Authenticator<String, Principal>() { | return user -> { | ||
@Override | if (presented.equals(user)) { | ||
public Optional<Principal> authenticate(String user) throws AuthenticationException { | return Optional.<Principal>of(new PrincipalImpl(returned)); | ||
if (presented.equals(user)) { | } | ||
return Optional.<Principal>of(new PrincipalImpl(returned)); | if ("bad-guy".equals(user)) { | ||
} | throw new AuthenticationException("CRAP"); | ||
if ("bad-guy".equals(user)) { | |||
throw new AuthenticationException("CRAP"); | |||
} | |||
return Optional.absent(); | |||
} | } | ||
return Optional.empty(); | |||
}; | }; | ||
} | } | ||
|
|
||
public static Authenticator<String, Principal> getMultiplyUsersOAuthAuthenticator(final List<String> validUsers) { | public static Authenticator<String, Principal> getMultiplyUsersOAuthAuthenticator(final List<String> validUsers) { | ||
return new Authenticator<String, Principal>() { | return credentials -> { | ||
@Override | if (validUsers.contains(credentials)) { | ||
public Optional<Principal> authenticate(String credentials) throws AuthenticationException { | return Optional.<Principal>of(new PrincipalImpl(credentials)); | ||
if (validUsers.contains(credentials)) { | } | ||
return Optional.<Principal>of(new PrincipalImpl(credentials)); | if ("bad-guy".equals(credentials)) { | ||
} | throw new AuthenticationException("CRAP"); | ||
if (credentials.equals("bad-guy")) { | |||
throw new AuthenticationException("CRAP"); | |||
} | |||
return Optional.absent(); | |||
} | } | ||
return Optional.empty(); | |||
}; | }; | ||
} | } | ||
|
|
||
public static Authorizer<Principal> getTestAuthorizer(final String validUser, | public static Authorizer<Principal> getTestAuthorizer(final String validUser, | ||
final String validRole) { | final String validRole) { | ||
return new Authorizer<Principal>() { | return (principal, role) -> principal != null | ||
@Override | && validUser.equals(principal.getName()) | ||
public boolean authorize(Principal principal, String role) { | && validRole.equals(role); | ||
return principal != null | |||
&& validUser.equals(principal.getName()) | |||
&& validRole.equals(role); | |||
} | |||
}; | |||
} | } | ||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
dropwizard-example/src/main/java/com/example/helloworld/auth/ExampleAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,17 +1,18 @@ | |||
package com.example.helloworld.auth; | package com.example.helloworld.auth; | ||
|
|
||
import com.example.helloworld.core.User; | import com.example.helloworld.core.User; | ||
import com.google.common.base.Optional; | |||
import io.dropwizard.auth.AuthenticationException; | import io.dropwizard.auth.AuthenticationException; | ||
import io.dropwizard.auth.Authenticator; | import io.dropwizard.auth.Authenticator; | ||
import io.dropwizard.auth.basic.BasicCredentials; | import io.dropwizard.auth.basic.BasicCredentials; | ||
|
|
||
import java.util.Optional; | |||
|
|||
public class ExampleAuthenticator implements Authenticator<BasicCredentials, User> { | public class ExampleAuthenticator implements Authenticator<BasicCredentials, User> { | ||
@Override | @Override | ||
public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { | public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { | ||
if ("secret".equals(credentials.getPassword())) { | if ("secret".equals(credentials.getPassword())) { | ||
return Optional.of(new User(credentials.getUsername())); | return Optional.of(new User(credentials.getUsername())); | ||
} | } | ||
return Optional.absent(); | return Optional.empty(); | ||
} | } | ||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.