Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a sample to work with drop wizard 0.9 auth framework
- Loading branch information
Showing
10 changed files
with
168 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
# dropwizard-immutable-bean | ||
example of an error with jackson | ||
|
||
I had a problem with Jackson and immutable beans. | ||
I had a problem with Jackson and immutable beans. | ||
|
||
Full discussion can be followed at https://mail.google.com/mail/u/0/?zx=khfzn67sm1i#label/dropwizard/150e3ec1e93ad69d | ||
|
||
It is solved. See the code sample. | ||
|
||
|
||
# Auth sample | ||
|
||
Have a look at the classes under the auth package. | ||
|
||
To check auth | ||
`curl http://localhost:9000` expected response is `Can't touch this...` | ||
`curl http://localhost:9000 --user abc:abc` expected response is `You got permission!` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ca.rasul.auth; | ||
|
||
import ca.rasul.models.User; | ||
import com.google.common.base.Optional; | ||
import io.dropwizard.auth.AuthenticationException; | ||
import io.dropwizard.auth.Authenticator; | ||
import io.dropwizard.auth.basic.BasicCredentials; | ||
|
||
/** | ||
* @author Nasir Rasul {@literal nasir@rasul.ca} | ||
* */ | ||
public class BasicAuthenticator<C, P> implements Authenticator<BasicCredentials, User> { | ||
@Override | ||
public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { | ||
//do no authentication yet. Let all users through | ||
return Optional.fromNullable(new User(credentials.getUsername(), credentials.getPassword())); | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
src/main/java/ca/rasul/auth/UnauthorizedResourceHandler.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ca.rasul.auth; | ||
|
||
import io.dropwizard.auth.UnauthorizedHandler; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
/** | ||
* @author Nasir Rasul {@literal nasir@rasul.ca} | ||
*/ | ||
public class UnAuthorizedResourceHandler implements UnauthorizedHandler { | ||
|
||
@Context | ||
private HttpServletRequest request; | ||
|
||
@Override | ||
public Response buildResponse(String prefix, String realm) { | ||
Response.Status unauthorized = Response.Status.UNAUTHORIZED; | ||
return Response.status(unauthorized).type(MediaType.APPLICATION_JSON_TYPE).entity("Can't touch this...").build(); | ||
} | ||
|
||
@Context | ||
public void setRequest(HttpServletRequest request) { | ||
this.request = request; | ||
} | ||
} | ||
|
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ca.rasul.auth; | ||
|
||
import ca.rasul.models.User; | ||
import io.dropwizard.auth.Authorizer; | ||
|
||
/** | ||
* @author Nasir Rasul {@literal nasir@rasul.ca} | ||
*/ | ||
public class UserAuthorizer<P> implements Authorizer<User>{ | ||
/** | ||
* Decides if access is granted for the given principal in the given role. | ||
* | ||
* @param principal a {@link Principal} object, representing a user | ||
* @param role a user role | ||
* @return {@code true}, if the access is granted, {@code false otherwise} | ||
*/ | ||
@Override | ||
public boolean authorize(User principal, String role) { | ||
return true; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/ca/rasul/modes/Message.java → src/main/java/ca/rasul/models/Message.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 | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ca.rasul.modes; | ||
package ca.rasul.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
|
||
|
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ca.rasul.models; | ||
|
||
import javax.security.auth.Subject; | ||
import java.security.Principal; | ||
|
||
/** | ||
* @author Nasir Rasul {@literal nasir@rasul.ca} | ||
*/ | ||
public class User implements Principal { | ||
private final String username; | ||
private final String password; | ||
|
||
public User(final String username, final String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
/** | ||
* Returns the name of this principal. | ||
* | ||
* @return the name of this principal. | ||
*/ | ||
@Override | ||
public String getName() { | ||
return username; | ||
} | ||
|
||
/** | ||
* Returns true if the specified subject is implied by this principal. | ||
* <p> | ||
* <p>The default implementation of this method returns true if | ||
* {@code subject} is non-null and contains at least one principal that | ||
* is equal to this principal. | ||
* <p> | ||
* <p>Subclasses may override this with a different implementation, if | ||
* necessary. | ||
* | ||
* @param subject the {@code Subject} | ||
* @return true if {@code subject} is non-null and is | ||
* implied by this principal, or false otherwise. | ||
* @since 1.8 | ||
*/ | ||
@Override | ||
public boolean implies(final Subject subject) { | ||
return true; | ||
} | ||
} |
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