Skip to content

Commit

Permalink
Making InvalidClaimException imutable
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemers committed Jan 28, 2020
1 parent 67af5db commit 21445d5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ available in the future. (see below for more details)
* [Custom Clock](#paseto-read-clock-custom)
* [JSON Processor](#json)
* [Custom JSON Processor](#json-custom)
* [Parsing of Custom Claim Types](#json-jackson-custom-types)
* [Learn More](#learn-more)
* [License](#license)

Expand Down Expand Up @@ -723,6 +724,39 @@ Pasetos.parserBuilder()
// ... etc ...
```

<a name="json-jackson-custom-types"></a>
### Parsing of Custom Claim Types

By default JPaseto will only convert simple claim types: String, Instant, Date, Long, Integer, Short and Byte. If you need to deserialize other types you can configure the `JacksonDeserializer` by passing a `Map` of claim names to types in through a constructor. For example:

```java
new JacksonDeserializer(Maps.of("user", User.class).build())
```

This would trigger the value in the `user` claim to be deserialized into the custom type of `User`. Given the claims body of:

```json
{
"issuer": "https://example.com/issuer",
"user": {
"firstName": "Jill",
"lastName": "Coder"
}
}
```

The `User` object could be retrieved from the `user` claim with the following code:

```java
Pasetos.parserBuilder()

.setDeserializer(new JacksonDeserializer(Map.of("user", User.class))) // <-----
.build()
.parse(token)
.getClaims()
.get("user", User.class); // <-----
```

<a name="learn-more"></a>
## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
public class IncorrectClaimException extends InvalidClaimException {

public IncorrectClaimException(Paseto paseto, String message) {
super(paseto, message);
public IncorrectClaimException(Paseto paseto, String claimName, String claimDescription, String message) {
super(paseto, claimName, claimDescription, message);
}

public IncorrectClaimException(Paseto paseto, String message, Throwable cause) {
super(paseto, message, cause);
public IncorrectClaimException(Paseto paseto, String claimName, String claimDescription, String message, Throwable cause) {
super(paseto, claimName, claimDescription, message, cause);
}
}
21 changes: 8 additions & 13 deletions api/src/main/java/dev/paseto/jpaseto/InvalidClaimException.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,26 @@
*/
public class InvalidClaimException extends ClaimPasetoException {

// TODO make imutable
private String claimName;
private Object claimDescription;
private final String claimName;
private final Object claimDescription;

protected InvalidClaimException(Paseto paseto, String message) {
protected InvalidClaimException(Paseto paseto, String claimName, String claimDescription, String message) {
super(paseto, message);
this.claimName = claimName;
this.claimDescription = claimDescription;
}

protected InvalidClaimException(Paseto paseto, String message, Throwable cause) {
protected InvalidClaimException(Paseto paseto, String claimName, String claimDescription, String message, Throwable cause) {
super(paseto, message, cause);
this.claimName = claimName;
this.claimDescription = claimDescription;
}

public String getClaimName() {
return claimName;
}

public void setClaimName(String claimName) {
this.claimName = claimName;
}

public Object getClaimDescription() {
return claimDescription;
}

public void setClaimDescription(Object claimDescription) {
this.claimDescription = claimDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
* @since 0.1
*/
public class MissingClaimException extends InvalidClaimException {
public MissingClaimException(Paseto paseto, String message) {
super(paseto, message);
public MissingClaimException(Paseto paseto, String claimName, String claimDescription, String message) {
super(paseto, claimName, claimDescription, message);
}

public MissingClaimException(Paseto paseto, String message, Throwable cause) {
super(paseto, message, cause);
public MissingClaimException(Paseto paseto, String claimName, String claimDescription, String message, Throwable cause) {
super(paseto, claimName, claimDescription, message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,17 @@ private void validateExpected(Paseto paseto, Map<String, Object> claims, Map<Str
if (actualClaimValue == null) {

String msg = String.format(ClaimPasetoException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE, claimName, description);
invalidClaimException = new MissingClaimException(paseto, msg);
invalidClaimException = new MissingClaimException(paseto, claimName, description, msg);

} else if (!predicate.test(actualClaimValue)) {

String msg = String.format(ClaimPasetoException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE,
claimName, description, actualClaimValue);

invalidClaimException = new IncorrectClaimException(paseto, msg);
invalidClaimException = new IncorrectClaimException(paseto, claimName, description, msg);
}

if (invalidClaimException != null) {
invalidClaimException.setClaimName(claimName);
invalidClaimException.setClaimDescription(description);
throw invalidClaimException;
}
});
Expand Down

0 comments on commit 21445d5

Please sign in to comment.